/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.Audit
 *  com.l7tech.message.HeadersKnob
 *  com.l7tech.policy.assertion.Assertion
 *  com.l7tech.policy.assertion.CustomAssertionHolder
 *  com.l7tech.policy.assertion.EncapsulatedAssertion
 *  com.l7tech.policy.assertion.SetVariableAssertion
 *  com.l7tech.policy.assertion.SslAssertion
 *  io.opentelemetry.api.GlobalOpenTelemetry
 *  io.opentelemetry.api.baggage.Baggage
 *  io.opentelemetry.api.baggage.BaggageEntry
 *  io.opentelemetry.api.common.Attributes
 *  io.opentelemetry.api.common.AttributesBuilder
 *  io.opentelemetry.api.metrics.DoubleCounter
 *  io.opentelemetry.api.metrics.DoubleUpDownCounter
 *  io.opentelemetry.api.metrics.LongHistogram
 *  io.opentelemetry.api.metrics.Meter
 *  io.opentelemetry.api.metrics.ObservableLongGauge
 *  io.opentelemetry.api.trace.Span
 *  io.opentelemetry.context.Context
 *  io.opentelemetry.context.propagation.TextMapGetter
 */
package com.l7tech.server.otel;

import com.l7tech.gateway.common.audit.Audit;
import com.l7tech.message.HeadersKnob;
import com.l7tech.policy.assertion.Assertion;
import com.l7tech.policy.assertion.CustomAssertionHolder;
import com.l7tech.policy.assertion.EncapsulatedAssertion;
import com.l7tech.policy.assertion.SetVariableAssertion;
import com.l7tech.policy.assertion.SslAssertion;
import com.l7tech.server.audit.Auditor;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.otel.OtelBaggageChange;
import com.l7tech.server.otel.OtelContext;
import com.l7tech.server.otel.OtelContextFactory;
import com.l7tech.server.otel.c;
import com.l7tech.server.otel.util.OtelBaggageUtil;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.baggage.BaggageEntry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.metrics.DoubleCounter;
import io.opentelemetry.api.metrics.DoubleUpDownCounter;
import io.opentelemetry.api.metrics.LongHistogram;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.ObservableLongGauge;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapGetter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

public class OtelMetricContextImpl
implements OtelContext {
    public static final String OTEL_BAGGAGE_DELETE_CV;
    public static final String OTEL_BAGGAGE_UPDATE_CV;
    public static final Set<String> OTEL_BAGGAGE_KEYWORDS;
    public static final String OTEL_BAGGAGE_KEYS_CV;
    public static final String DEFAULT_BAGGAGE_METADATA_DELIMITER = ";";
    public static final String BAGGAGE_KEYS_WILDCARD = "*";
    private static final Map<String, String> a;
    private static final Map<String, String> b;
    private final Meter c;
    private final String d;
    private final String e;
    private static final Logger f;
    Map<String, BaggageEntry> g = new HashMap<String, BaggageEntry>();
    Baggage h;
    List<OtelBaggageChange> i = new LinkedList<OtelBaggageChange>();
    private final Auditor j;
    static final TextMapGetter<HeadersKnob> k;
    private static final String[] D;

    public OtelMetricContextImpl(String string, String string2, Auditor auditor) {
        this.c = GlobalOpenTelemetry.meterBuilder((String)D[18]).setInstrumentationVersion(OTEL_SCOPE_VERSION).build();
        this.d = string != null ? string : "";
        this.e = string2 != null ? string2 : "";
        this.j = auditor;
    }

    @Override
    public Span startAssertionSpan(Assertion assertion, PolicyEnforcementContext policyEnforcementContext) {
        this.a(assertion, policyEnforcementContext);
        return OtelContext.super.startAssertionSpan(assertion, policyEnforcementContext);
    }

    @Override
    public Span startServiceSpan(PolicyEnforcementContext policyEnforcementContext) {
        Context context = OtelContextFactory.getTextMapPropagator().extract(Context.current(), (Object)policyEnforcementContext.getRequest().getHeadersKnob(), k);
        this.h = Baggage.fromContext((Context)context);
        this.g.putAll(this.h.asMap());
        OtelBaggageUtil.setBaggageContextVariable(policyEnforcementContext, this.g);
        return OtelContext.super.startServiceSpan(policyEnforcementContext);
    }

    @Override
    public void recordCounter(String string, String string2, String string3, long l2, Attributes attributes) {
        AttributesBuilder attributesBuilder = this.a(attributes);
        DoubleCounter doubleCounter = this.c.counterBuilder(this.d + string).setUnit(string3).setDescription(string2).ofDoubles().build();
        doubleCounter.add((double)l2, attributesBuilder.build());
        f.log(Level.FINEST, D[1] + doubleCounter.toString());
    }

    @Override
    public void recordHistogram(String string, String string2, String string3, long l2, Attributes attributes) {
        AttributesBuilder attributesBuilder = this.a(attributes);
        LongHistogram longHistogram = this.c.histogramBuilder(this.d + string).setUnit(string3).setDescription(string2).ofLongs().build();
        longHistogram.record(l2, attributesBuilder.build());
        f.log(Level.FINEST, D[12] + longHistogram.toString());
    }

    @Override
    public void recordGauge(String string, String string2, String string3, long l2, Attributes attributes) {
        AttributesBuilder attributesBuilder = this.a(attributes);
        ObservableLongGauge observableLongGauge = this.c.gaugeBuilder(this.d + string).setUnit(string3).setDescription(string2).ofLongs().buildWithCallback(observableLongMeasurement -> observableLongMeasurement.record(l2, attributesBuilder.build()));
        f.log(Level.FINEST, D[17] + observableLongGauge.toString());
    }

    @Override
    public void recordUpDownCounter(String string, String string2, String string3, double d10, Attributes attributes) {
        AttributesBuilder attributesBuilder = this.a(attributes);
        DoubleUpDownCounter doubleUpDownCounter = this.c.upDownCounterBuilder(this.d + string).setUnit(string3).setDescription(string2).ofDoubles().build();
        doubleUpDownCounter.add(d10, attributesBuilder.build());
        f.log(Level.FINEST, D[0] + doubleUpDownCounter.toString());
    }

    @Override
    public <T> Callable<T> wrap(Callable<T> callable) {
        return Context.current().wrap(callable);
    }

    private AttributesBuilder a(Attributes attributes) {
        AttributesBuilder attributesBuilder = Attributes.builder();
        attributes.forEach((attributeKey, object) -> attributesBuilder.put(this.e + String.valueOf(attributeKey), String.valueOf(object)));
        return attributesBuilder;
    }

    void a(Assertion assertion, PolicyEnforcementContext policyEnforcementContext) {
        if (assertion instanceof SetVariableAssertion) {
            String string = ((SetVariableAssertion)assertion).getVariableToSet();
            String string2 = ((SetVariableAssertion)assertion).expression();
            if (string2.isBlank()) {
                return;
            }
            f.fine(() -> D[16].formatted(this.getAssertionFeatureSetName(assertion), string));
            String[] stringArray = D;
            if (stringArray[6].equalsIgnoreCase(string)) {
                String[] stringArray2;
                String string3 = Pattern.quote(",");
                for (String string4 : stringArray2 = string2.split(string3)) {
                    stringArray = D;
                    f.fine(stringArray[15] + string4);
                    this.i.add(new OtelBaggageChange(OtelBaggageChange.BaggageChangeType.DELETE, string4.trim()));
                }
            } else {
                stringArray = D;
                if (stringArray[8].equalsIgnoreCase(string)) {
                    f.fine(stringArray[11] + string2);
                    this.i.addAll(OtelBaggageUtil.createBaggageChangeList(policyEnforcementContext, string2, (Audit)this.j));
                }
            }
        }
    }

    @Override
    public Baggage createBaggage() {
        return OtelBaggageUtil.createBaggage(this.g, this.i);
    }

    protected String generateShortNameCacheKey(Assertion assertion) {
        if (assertion instanceof EncapsulatedAssertion || assertion instanceof CustomAssertionHolder) {
            return null;
        }
        if (assertion instanceof SslAssertion) {
            boolean bl2 = ((SslAssertion)assertion).isRequireClientAuthentication();
            return D[10] + bl2;
        }
        return assertion.getClass().getName();
    }

    protected String getAssertionShortName(Assertion assertion) {
        String string2 = this.generateShortNameCacheKey(assertion);
        if (string2 == null) {
            return (String)assertion.meta().get(D[9]);
        }
        return a.computeIfAbsent(string2, string -> (String)assertion.meta().get(D[7]));
    }

    protected String getAssertionFeatureSetName(Assertion assertion) {
        String string2 = this.generateShortNameCacheKey(assertion);
        if (string2 == null) {
            return assertion.getFeatureSetName();
        }
        return b.computeIfAbsent(string2, string -> assertion.getFeatureSetName());
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[19];
                var3_1 = 0;
                var2_2 = "J\rC^\u0011\u0019WA\u0016Q\\r\u0003Rk\rC@\u0011\u0001Bq\u000bOQ\u0011\bBg\fA\b\u0011\u0019J\rC^\u0011\u000fHp\u0017R\u0012\\\tSw\u0010E\u0012U\tEp\u001e\u001c\u0012\u0006p\tBSE\t\u0013j\rC^\u001f\u000eFb\u001eGUTBRu\u001dGFT\u0013j\rC^\u001f\u000eFb\u001eGUTBC`\u0015CFT\u0011j\rC^\u001f\u000eFb\u001eGUTBL`\u0000U\u0013j\rC^\u001f\u000eFb\u001eGUTBC`\u0015CFT\tv\u0011I@E\"Fh\u001c\u0013j\rC^\u001f\u000eFb\u001eGUTBRu\u001dGFT\tv\u0011I@E\"Fh\u001c\rV\nJsB\u001fBw\rO]_V\u0014P\tBSE\t\u0007G\u0018AUP\u000bB%2CK\u000bL\u001dJ\rC^\u0011\u0004Nv\rIUC\rJ%\u0014CFC\u0005D%\u001dCPD\u000b\u001d%\u0004n\u001c_A\u0006a\u001cJWE\t\u0014A\u001cJWE\t\u0007G\u0018AUP\u000bB%2CK\u000bL\u001aU\u000bIQT\u001fTl\u0017A\u0012p\u001fT`\u000bR[^\u0002\u0007 \n\u001c\u0017B";
                var4_3 = "J\rC^\u0011\u0019WA\u0016Q\\r\u0003Rk\rC@\u0011\u0001Bq\u000bOQ\u0011\bBg\fA\b\u0011\u0019J\rC^\u0011\u000fHp\u0017R\u0012\\\tSw\u0010E\u0012U\tEp\u001e\u001c\u0012\u0006p\tBSE\t\u0013j\rC^\u001f\u000eFb\u001eGUTBRu\u001dGFT\u0013j\rC^\u001f\u000eFb\u001eGUTBC`\u0015CFT\u0011j\rC^\u001f\u000eFb\u001eGUTBL`\u0000U\u0013j\rC^\u001f\u000eFb\u001eGUTBC`\u0015CFT\tv\u0011I@E\"Fh\u001c\u0013j\rC^\u001f\u000eFb\u001eGUTBRu\u001dGFT\tv\u0011I@E\"Fh\u001c\rV\nJsB\u001fBw\rO]_V\u0014P\tBSE\t\u0007G\u0018AUP\u000bB%2CK\u000bL\u001dJ\rC^\u0011\u0004Nv\rIUC\rJ%\u0014CFC\u0005D%\u001dCPD\u000b\u001d%\u0004n\u001c_A\u0006a\u001cJWE\t\u0014A\u001cJWE\t\u0007G\u0018AUP\u000bB%2CK\u000bL\u001aU\u000bIQT\u001fTl\u0017A\u0012p\u001fT`\u000bR[^\u0002\u0007 \n\u001c\u0017B".length();
                var1_4 = 33;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 124;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = OtelMetricContextImpl.a(v0, OtelMetricContextImpl.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "I\u000e@]\u0012\bEs\u001d@sG\u0006Hb\u001fW\u0011_\nPt\u0013F\u0011V\nFs\u001d\u001f\u0011\u0012e\u0015H\u001f^\u000e]c\b\u0012\u001fU\u000ePc\rDH";
                    var4_3 = "I\u000e@]\u0012\bEs\u001d@sG\u0006Hb\u001fW\u0011_\nPt\u0013F\u0011V\nFs\u001d\u001f\u0011\u0012e\u0015H\u001f^\u000e]c\b\u0012\u001fU\u000ePc\rDH".length();
                    var1_4 = 32;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 127;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = OtelMetricContextImpl.a(v0, OtelMetricContextImpl.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        OtelMetricContextImpl.D = var5;
        OtelMetricContextImpl.OTEL_BAGGAGE_UPDATE_CV = OtelMetricContextImpl.D[3];
        OtelMetricContextImpl.OTEL_BAGGAGE_DELETE_CV = OtelMetricContextImpl.D[4];
        OtelMetricContextImpl.OTEL_BAGGAGE_KEYS_CV = OtelMetricContextImpl.D[5];
        var0_6 = OtelMetricContextImpl.D;
        OtelMetricContextImpl.OTEL_BAGGAGE_KEYWORDS = Set.of(var0_6[13], var0_6[2], var0_6[14]);
        OtelMetricContextImpl.a = new ConcurrentHashMap<String, String>();
        OtelMetricContextImpl.b = new ConcurrentHashMap<String, String>();
        OtelMetricContextImpl.f = Logger.getLogger(OtelMetricContextImpl.class.getName());
        OtelMetricContextImpl.k = new c();
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x5B);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 121;
                    case 1 -> 5;
                    case 2 -> 90;
                    case 3 -> 78;
                    case 4 -> 77;
                    case 5 -> 16;
                    default -> 91;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

