/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.message.HttpRequestKnob
 */
package com.l7tech.server.otel;

import com.l7tech.message.HttpRequestKnob;
import com.l7tech.server.otel.OtelContextFactory;
import com.l7tech.server.otel.OtelTraceConfig;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class OtelTraceSamplingConfig {
    private final OtelTraceConfig a;
    private final List<Pattern> b;
    private final List<Pattern> c;
    private final List<Pattern> d;
    private final List<Pattern> e;
    private final Set<String> f = new HashSet<String>();
    private final Set<Pattern> g = new HashSet<Pattern>();
    private static final Logger h;
    private static final String[] i;

    public OtelTraceSamplingConfig(OtelTraceConfig otelTraceConfig) {
        this.a = otelTraceConfig;
        if (otelTraceConfig.getAssertions() != null) {
            this.b = otelTraceConfig.getAssertions().getExclude().stream().map(string -> Pattern.compile(string, 2)).collect(Collectors.toList());
            this.c = otelTraceConfig.getAssertions().getInclude().stream().map(string -> Pattern.compile(string, 2)).collect(Collectors.toList());
        } else {
            this.b = Collections.emptyList();
            this.c = Collections.emptyList();
        }
        if (otelTraceConfig.getContextVariables() != null) {
            this.d = otelTraceConfig.getContextVariables().getExclude().stream().map(string -> Pattern.compile(string, 2)).collect(Collectors.toList());
            this.e = otelTraceConfig.getContextVariables().getInclude().stream().map(string -> Pattern.compile(string, 2)).collect(Collectors.toList());
        } else {
            this.d = Collections.emptyList();
            this.e = Collections.emptyList();
        }
        this.a();
    }

    public OtelTraceConfig getOtelTraceConfig() {
        return this.a;
    }

    public List<Pattern> getExcludeAssertions() {
        return this.b;
    }

    public List<Pattern> getIncludeAssertions() {
        return this.c;
    }

    public List<Pattern> getExcludeCtxVars() {
        return this.d;
    }

    public List<Pattern> getIncludeCtxVars() {
        return this.e;
    }

    private void a() {
        for (OtelTraceConfig.Service service : this.a.getServices()) {
            String string = service.getGoid();
            String string2 = service.getResolutionPath();
            if (string2 != null && !string2.isEmpty()) {
                Pattern pattern = Pattern.compile(string2, 2);
                this.g.add(pattern);
                continue;
            }
            if (string == null || string.isEmpty()) continue;
            this.f.add(string);
        }
    }

    public boolean isTraceEnabledForService(String string, HttpRequestKnob httpRequestKnob) {
        String[] stringArray;
        String string2;
        try {
            if (!this.a.getTraceSelectiveServices().booleanValue()) {
                return true;
            }
        }
        catch (IOException iOException) {
            throw OtelTraceSamplingConfig.a(iOException);
        }
        String string3 = this.a.getServiceQueryParamSelective();
        if (string3 != null) {
            try {
                string2 = httpRequestKnob.getParameter(string3);
                if (string2 == null) {
                    string2 = httpRequestKnob.getHeaderSingleValue(string3);
                }
                if (!Boolean.parseBoolean(string2)) {
                    return false;
                }
            }
            catch (IOException iOException) {
                stringArray = i;
                h.warning(stringArray[1] + iOException.getMessage());
            }
        }
        if ((string2 = this.a.getClientIp()) != null) {
            try {
                stringArray = i;
                String string4 = httpRequestKnob.getHeaderSingleValue(stringArray[0]);
                if (string4 == null) {
                    string4 = httpRequestKnob.getRemoteAddress();
                    try {
                        String string5 = string4 = string4.contains(",") ? string4.split(",")[0] : string4;
                    }
                    catch (IOException iOException) {
                        throw OtelTraceSamplingConfig.a(iOException);
                    }
                }
                if (!string2.equalsIgnoreCase(string4)) {
                    return false;
                }
            }
            catch (IOException iOException) {
                h.warning(i[2] + iOException.getMessage());
            }
        }
        return this.isServiceIncluded(string, httpRequestKnob.getRequestUri());
    }

    public boolean isServiceIncluded(String string, String string2) {
        boolean bl2 = this.f.contains(string);
        if (!bl2) {
            for (Pattern pattern : this.g) {
                if (!pattern.matcher(string2).find()) continue;
                bl2 = true;
                break;
            }
        }
        return bl2;
    }

    public String getTraceNamePattern() {
        return this.a.getTraceNamePattern();
    }

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[3];
        int n2 = 0;
        String string = "s\f \u0015cm\u0011ye#\u001e\u001c|\u001fy4du\u00036\u000b\u001apnS\u00145C\u001a9E\u0001\u0014?EH9NW\u000f4V\u001a!^D\u0014#\u001eR5JE\u0003(\u0011J1Y@\u000b?E_\"\u000b\u001bF@du\u00036\u000b\u001a\u0015YS\t(\u0011S>\u000bS\u0003.CS5]H\b=\u0011Y<BD\b.\u0011s \u000b@\u0002>C_#X\u0001\u0000(^WpcU\u0012*\u0011h5ZT\u0003)E\u001aj\u000b";
        int n3 = "s\f \u0015cm\u0011ye#\u001e\u001c|\u001fy4du\u00036\u000b\u001apnS\u00145C\u001a9E\u0001\u0014?EH9NW\u000f4V\u001a!^D\u0014#\u001eR5JE\u0003(\u0011J1Y@\u000b?E_\"\u000b\u001bF@du\u00036\u000b\u001a\u0015YS\t(\u0011S>\u000bS\u0003.CS5]H\b=\u0011Y<BD\b.\u0011s \u000b@\u0002>C_#X\u0001\u0000(^WpcU\u0012*\u0011h5ZT\u0003)E\u001aj\u000b".length();
        int n4 = 15;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = OtelTraceSamplingConfig.a(96, OtelTraceSamplingConfig.a(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                i = stringArray;
                h = Logger.getLogger(OtelContextFactory.class.getName());
                return;
            }
            n4 = string.charAt(n5);
        }
    }

    private static IOException a(IOException iOException) {
        return iOException;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x30);
        }
        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 -> 75;
                    case 1 -> 65;
                    case 2 -> 6;
                    case 3 -> 58;
                    case 4 -> 81;
                    case 5 -> 90;
                    default -> 48;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

