/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.AuditDetail
 *  com.l7tech.gateway.common.audit.AuditRecord
 *  com.l7tech.util.Config
 *  com.l7tech.util.Functions$Nullary
 *  com.l7tech.util.Option
 *  com.l7tech.util.RunnableCallable
 *  org.jetbrains.annotations.NotNull
 *  org.jetbrains.annotations.Nullable
 */
package com.l7tech.server.audit;

import com.l7tech.gateway.common.audit.AuditDetail;
import com.l7tech.gateway.common.audit.AuditRecord;
import com.l7tech.server.DefaultKey;
import com.l7tech.server.audit.AuditContext;
import com.l7tech.server.audit.AuditContextImpl;
import com.l7tech.server.audit.AuditFilterPolicyManager;
import com.l7tech.server.audit.AuditLogListener;
import com.l7tech.server.audit.AuditPolicyEvaluator;
import com.l7tech.server.audit.AuditRecordManager;
import com.l7tech.server.audit.LogOnlyAuditContext;
import com.l7tech.server.audit.m;
import com.l7tech.util.Config;
import com.l7tech.util.Functions;
import com.l7tech.util.Option;
import com.l7tech.util.RunnableCallable;
import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/*
 * Exception performing whole class analysis ignored.
 */
public class AuditContextFactory {
    protected static final ThreadLocal<AuditContext> currentAuditContext;
    private static final AtomicReference<AuditLogListener> a;
    private final AuditLogListener b;
    private Config c;
    private AuditRecordManager d;
    private String e;
    private AuditPolicyEvaluator f;
    private AuditFilterPolicyManager g;
    private DefaultKey h;
    private boolean i = false;
    private static final String[] j;

    public AuditContextFactory(AuditLogListener auditLogListener) {
        this.b = auditLogListener;
        if (auditLogListener != null) {
            try {
                if (!a.compareAndSet(null, auditLogListener)) {
                    throw new IllegalStateException(j[7]);
                }
            }
            catch (IllegalStateException illegalStateException) {
                throw AuditContextFactory.a(illegalStateException);
            }
        }
    }

    void a(Config config, AuditRecordManager auditRecordManager, String string, AuditPolicyEvaluator auditPolicyEvaluator, AuditFilterPolicyManager auditFilterPolicyManager, DefaultKey defaultKey) {
        this.c = config;
        this.d = auditRecordManager;
        this.e = string;
        this.f = auditPolicyEvaluator;
        this.g = auditFilterPolicyManager;
        this.h = defaultKey;
        this.i = true;
    }

    @NotNull
    public static AuditContext getCurrent() {
        AuditContext auditContext;
        AuditContext auditContext2 = currentAuditContext.get();
        if (auditContext2 == null) {
            auditContext2 = AuditContextFactory.createLogOnlyAuditContext();
        }
        try {
            auditContext = auditContext2;
            if (auditContext == null) {
                AuditContextFactory.a(0);
            }
        }
        catch (IllegalStateException illegalStateException) {
            throw AuditContextFactory.a(illegalStateException);
        }
        return auditContext;
    }

    public static LogOnlyAuditContext createLogOnlyAuditContext() {
        return new LogOnlyAuditContext(a.get());
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static <T> T doWithCustomAuditContext(@NotNull AuditContext auditContext, @NotNull Callable<T> callable) throws Exception {
        try {
            if (auditContext == null) {
                AuditContextFactory.a(1);
            }
        }
        catch (Exception exception) {
            throw AuditContextFactory.a(exception);
        }
        try {
            if (callable == null) {
                AuditContextFactory.a(2);
            }
        }
        catch (Exception exception) {
            throw AuditContextFactory.a(exception);
        }
        AuditContext auditContext2 = currentAuditContext.get();
        currentAuditContext.set(auditContext);
        try {
            T t2 = callable.call();
            return t2;
        }
        finally {
            currentAuditContext.set(auditContext2);
        }
    }

    public void emitAuditRecord(AuditRecord auditRecord) {
        this.emitAuditRecord(auditRecord, false);
    }

    public void emitAuditRecord(AuditRecord auditRecord, boolean bl2) {
        this.emitAuditRecordWithDetails(auditRecord, bl2, null, null);
    }

    public void emitAuditRecordWithDetails(AuditRecord auditRecord, boolean bl2, @Nullable Object object, @Nullable Collection<AuditDetail> collection) {
        Option option;
        block15: {
            AuditContext auditContext;
            block16: {
                block14: {
                    AuditContextImpl auditContextImpl;
                    auditContext = this.newContext();
                    try {
                        auditContextImpl = auditContext instanceof AuditContextImpl ? (AuditContextImpl)auditContext : null;
                    }
                    catch (IllegalStateException illegalStateException) {
                        throw AuditContextFactory.a(illegalStateException);
                    }
                    option = Option.optional((Object)auditContextImpl);
                    try {
                        try {
                            if (!option.isSome()) break block14;
                            ((AuditContextImpl)option.some()).a(auditRecord);
                            if (!bl2) break block14;
                        }
                        catch (IllegalStateException illegalStateException) {
                            throw AuditContextFactory.a(illegalStateException);
                        }
                        ((AuditContextImpl)option.some()).setUpdate(true);
                    }
                    catch (IllegalStateException illegalStateException) {
                        throw AuditContextFactory.a(illegalStateException);
                    }
                }
                try {
                    try {
                        if (collection == null || collection.isEmpty()) break block15;
                    }
                    catch (IllegalStateException illegalStateException) {
                        throw AuditContextFactory.a(illegalStateException);
                    }
                    if (object != null) break block16;
                }
                catch (IllegalStateException illegalStateException) {
                    throw AuditContextFactory.a(illegalStateException);
                }
                object = this;
            }
            for (AuditDetail auditDetail : collection) {
                auditContext.addDetail(auditDetail, object);
            }
        }
        try {
            if (option.isSome()) {
                ((AuditContextImpl)option.some()).a();
            }
        }
        catch (IllegalStateException illegalStateException) {
            throw AuditContextFactory.a(illegalStateException);
        }
    }

    public void doWithNewAuditContext(@NotNull AuditRecord auditRecord, @NotNull Runnable runnable) {
        try {
            if (auditRecord == null) {
                AuditContextFactory.a(3);
            }
        }
        catch (Exception exception) {
            throw AuditContextFactory.a(exception);
        }
        try {
            if (runnable == null) {
                AuditContextFactory.a(4);
            }
        }
        catch (Exception exception) {
            throw AuditContextFactory.a(exception);
        }
        try {
            this.doWithNewAuditContext(auditRecord, (Callable)new RunnableCallable(runnable));
        }
        catch (Exception exception) {
            throw new RuntimeException(exception);
        }
    }

    public <T> T doWithNewAuditContext(@NotNull AuditRecord auditRecord, @NotNull Callable<T> callable) throws Exception {
        try {
            if (auditRecord == null) {
                AuditContextFactory.a(5);
            }
        }
        catch (Exception exception) {
            throw AuditContextFactory.a(exception);
        }
        try {
            if (callable == null) {
                AuditContextFactory.a(6);
            }
        }
        catch (Exception exception) {
            throw AuditContextFactory.a(exception);
        }
        return this.doWithNewAuditContext(callable, new m(this, auditRecord));
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public <T> T doWithNewAuditContext(@NotNull Callable<T> callable, @NotNull Functions.Nullary<AuditRecord> nullary) throws Exception {
        AuditContextImpl auditContextImpl;
        try {
            if (callable == null) {
                AuditContextFactory.a(7);
            }
        }
        catch (Exception exception) {
            throw AuditContextFactory.a(exception);
        }
        try {
            if (nullary == null) {
                AuditContextFactory.a(8);
            }
        }
        catch (Exception exception) {
            throw AuditContextFactory.a(exception);
        }
        AuditContext auditContext = currentAuditContext.get();
        AuditContext auditContext2 = this.newContext();
        try {
            auditContextImpl = auditContext2 instanceof AuditContextImpl ? (AuditContextImpl)auditContext2 : null;
        }
        catch (Exception exception) {
            throw AuditContextFactory.a(exception);
        }
        Option option = Option.optional((Object)auditContextImpl);
        currentAuditContext.set(auditContext2);
        try {
            T t2 = callable.call();
            return t2;
        }
        finally {
            try {
                if (option.isSome()) {
                    ((AuditContextImpl)option.some()).a((AuditRecord)nullary.call());
                    ((AuditContextImpl)option.some()).a();
                }
            }
            finally {
                currentAuditContext.set(auditContext);
            }
        }
    }

    protected AuditContext newContext() {
        try {
            if (!this.i) {
                return AuditContextFactory.createLogOnlyAuditContext();
            }
        }
        catch (IllegalStateException illegalStateException) {
            throw AuditContextFactory.a(illegalStateException);
        }
        return new AuditContextImpl(this.c, this.d, this.f, this.g, this.e, this.h, this.b);
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[13];
                var3_1 = 0;
                var2_2 = "7\u001eh\u0013 Ix\u0002Li\t?\fV8\u0003{(8@zV\u001cn\u0014,As\u0002\t}Fj\teQL`\u0000m\teXI|F Ye\u0002La\t9\ft\u0013La\u0013!@\b\u0004\u0019a\b,Nz\u0013\u000b\u0017\u0019k\u000f9~s\u0015\u0003}\u0002\u0007\u0015\u0003a\u0012(Tb\r\u0004\tl\t?HP\u0017\u000f{\t?U\b\u0015\rc\n,Nz\u0013\u0018\u0012\u0003X\u000f9DU\u0003\u001f{\t mc\u0012\u0005{%\"Bb\u0013\u0014{N7\u0018/\n(Me\u0002L`\b(\fW\u0003\bf\u0012\u000eCx\u0002\tw\u0012\u000bMu\u0002\u0003}\u001fmDw\u0005Ln\n?Iw\u0012\u0015/\u000e,H6\u0017Lh\n\"Nw\u001aLN\u0013)Eb:\u0003h*$_b\u0013\u0002j\u0014m_s\u0002*6\"`\u0012\u0003Yz\u001aLb\u00039Dy\u0012L*\u0015c\teV\u0001z\u00159\fx\u0019\u0018/\u0014(Xc\u0004\u0002/\b8@z\n\u0011\t{%8^d\u0013\u0002{+\u0015\u0003bI!\u001bb\u0013\u000fgI>Id\u0000\t}I,Yr\u001f\u0018 '8H\u007f\u0002/`\b9In\u0002*n\u00059Cd\u000f";
                var4_3 = "7\u001eh\u0013 Ix\u0002Li\t?\fV8\u0003{(8@zV\u001cn\u0014,As\u0002\t}Fj\teQL`\u0000m\teXI|F Ye\u0002La\t9\ft\u0013La\u0013!@\b\u0004\u0019a\b,Nz\u0013\u000b\u0017\u0019k\u000f9~s\u0015\u0003}\u0002\u0007\u0015\u0003a\u0012(Tb\r\u0004\tl\t?HP\u0017\u000f{\t?U\b\u0015\rc\n,Nz\u0013\u0018\u0012\u0003X\u000f9DU\u0003\u001f{\t mc\u0012\u0005{%\"Bb\u0013\u0014{N7\u0018/\n(Me\u0002L`\b(\fW\u0003\bf\u0012\u000eCx\u0002\tw\u0012\u000bMu\u0002\u0003}\u001fmDw\u0005Ln\n?Iw\u0012\u0015/\u000e,H6\u0017Lh\n\"Nw\u001aLN\u0013)Eb:\u0003h*$_b\u0013\u0002j\u0014m_s\u0002*6\"`\u0012\u0003Yz\u001aLb\u00039Dy\u0012L*\u0015c\teV\u0001z\u00159\fx\u0019\u0018/\u0014(Xc\u0004\u0002/\b8@z\n\u0011\t{%8^d\u0013\u0002{+\u0015\u0003bI!\u001bb\u0013\u000fgI>Id\u0000\t}I,Yr\u001f\u0018 '8H\u007f\u0002/`\b9In\u0002*n\u00059Cd\u000f".length();
                var1_4 = 62;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = AuditContextFactory.a(v0, AuditContextFactory.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "3\"y.\u0018ey2:o2\bdC\u0014\"@3\tuC+4\"Ch\u0000:C2.Fh\u001fhE!(\\h\rxS>9\u0001\u0006\u0019i^#\u000eA)\u0018hO#\u000bO$\u0018bE.";
                    var4_3 = "3\"y.\u0018ey2:o2\bdC\u0014\"@3\tuC+4\"Ch\u0000:C2.Fh\u001fhE!(\\h\rxS>9\u0001\u0006\u0019i^#\u000eA)\u0018hO#\u000bO$\u0018bE.".length();
                    var1_4 = 21;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 36;
                        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++] = AuditContextFactory.a(v0, AuditContextFactory.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;
            }
        }
        AuditContextFactory.j = var5;
        AuditContextFactory.currentAuditContext = new ThreadLocal<T>();
        AuditContextFactory.a = new AtomicReference<V>();
    }

    /*
     * Exception decompiling
     */
    private static /* synthetic */ void a(int var0) {
        /*
         * This method has failed to decompile.  When submitting a bug report, please provide this stack trace, and (if you hold appropriate legal rights) the relevant class file.
         * 
         * org.benf.cfr.reader.util.ConfusedCFRException: Tried to end blocks [0[TRYBLOCK]], but top level block is 6[SWITCH]
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.processEndingBlocks(Op04StructuredStatement.java:435)
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.buildNestedBlocks(Op04StructuredStatement.java:484)
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op03SimpleStatement.createInitialStructuredBlock(Op03SimpleStatement.java:736)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:850)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisOrWrapFail(CodeAnalyser.java:278)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysis(CodeAnalyser.java:201)
         *     at org.benf.cfr.reader.entities.attributes.AttributeCode.analyse(AttributeCode.java:94)
         *     at org.benf.cfr.reader.entities.Method.analyse(Method.java:531)
         *     at org.benf.cfr.reader.entities.ClassFile.analyseMid(ClassFile.java:1050)
         *     at org.benf.cfr.reader.entities.ClassFile.analyseTop(ClassFile.java:942)
         *     at org.benf.cfr.reader.Driver.doJarVersionTypes(Driver.java:257)
         *     at org.benf.cfr.reader.Driver.doJar(Driver.java:139)
         *     at org.benf.cfr.reader.CfrDriverImpl.analyse(CfrDriverImpl.java:76)
         *     at org.benf.cfr.reader.Main.main(Main.java:54)
         */
        throw new IllegalStateException("Decompilation failed");
    }

    private static Exception a(Exception exception) {
        return exception;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x13);
        }
        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 -> 115;
                    case 1 -> 105;
                    case 2 -> 10;
                    case 3 -> 99;
                    case 4 -> 72;
                    case 5 -> 41;
                    default -> 19;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

