package com.l7tech.policy.code.config;

import com.l7tech.policy.code.AssertionCodeException;
import com.l7tech.policy.code.config.schema.JsonSchemaGenerator;
import com.l7tech.policy.code.deserializer.FieldDeserializer;
import com.l7tech.policy.code.serializer.FieldSerializer;
import com.l7tech.policy.code.transformer.FieldTransformerFactory;
import com.l7tech.util.Config;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class PolicyCodeConfig implements BeanTypeConfigFinder {
   public static final int SERIALIZE_DEFAULT_FIELDS_ATTRIBUTE_MASK = 1;
   public static final int SERIALIZE_AS_OBJECT_ALWAYS_MASK = 2;
   private static final ThreadLocal<Integer> a;
   protected final Map<String, BeanTypeConfig> btConfigs = new ConcurrentHashMap<>();
   protected final Map<String, BeanTypeConfig> btConfigsByExtNames = new ConcurrentHashMap<>();
   protected final Map<String, AssertionTypeConfig> atConfigsByExtNames = new ConcurrentHashMap<>();
   protected final JsonSchemaGenerator jsonSchemaGenerator;
   private final FieldTransformerFactory b;
   private final FieldSerializer c;
   private final FieldDeserializer d;
   protected final Config config;
   public static final String CUSTOM_ASSERTION_EXT_NAME;
   public static final String INCLUDE_EXT_NAME;
   public static final String ENCASS_EXT_NAME;
   protected static final String IGNORE_REFERENCED_GUIDS;
   protected static final String USE_LEGACY_CUSTOM_ASSERTION_SERIALIZER;
   protected static final Set<String> CONFIG_PROPERTIES;
   private static final String[] l;

   protected PolicyCodeConfig(Config var1) {
      this.config = var1;
      this.b = new FieldTransformerFactory();
      this.c = new FieldSerializer(this);
      this.d = new FieldDeserializer(this);
      this.jsonSchemaGenerator = new JsonSchemaGenerator(this, this.b);
   }

   public boolean ignoreReferencedGuids() {
      return this.config.getBooleanProperty(l[2], false);
   }

   public boolean useLegacyCustomAssertionSerializer() {
      return this.config.getBooleanProperty(l[7], false);
   }

   public AssertionTypeConfig getTypeConfigByExtName(String var1) {
      AssertionTypeConfig var2 = this.getTypeConfigByExtNameSilently(var1);

      try {
         if (var2 == null) {
            throw new AssertionCodeException(l[5] + var1);
         } else {
            return var2;
         }
      } catch (AssertionCodeException var3) {
         throw a(var3);
      }
   }

   public AssertionTypeConfig getTypeConfigByExtNameSilently(String var1) {
      return this.atConfigsByExtNames.get(var1);
   }

   public BeanTypeConfig getSubTypeConfigSilently(Class<?> var1) {
      return this.btConfigs.get(var1.getName());
   }

   public BeanTypeConfig getSubTypeConfigByExtNameSilently(String var1) {
      return this.btConfigsByExtNames.get(var1);
   }

   public FieldDeserializer getFieldDeserializer() {
      return this.d;
   }

   public FieldSerializer getFieldSerializer() {
      return this.c;
   }

   public FieldTransformerFactory getTransformerFactory() {
      return this.b;
   }

   public JsonSchemaGenerator getJsonSchemaGenerator() {
      return this.jsonSchemaGenerator;
   }

   public void add(AssertionSubTypeConfig var1) {
      this.defineTransformerFor(var1);
      var1.setFinder(this);
      this.btConfigs.putIfAbsent(var1.getName(), var1);
      this.btConfigsByExtNames.putIfAbsent(var1.getExternalName(), var1);
   }

   @Override
   public BeanTypeConfig getBeanTypeConfigSilently(Class<?> var1) {
      return this.btConfigs.get(var1.getName());
   }

   @Override
   public BeanTypeConfig getBeanTypeConfigByExtNameSilently(String var1) {
      return this.btConfigsByExtNames.get(var1);
   }

   public boolean isSerializeDefaultFields() {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: getstatic com/l7tech/policy/code/config/PolicyCodeConfig.a Ljava/lang/ThreadLocal;
      // 03: invokevirtual java/lang/ThreadLocal.get ()Ljava/lang/Object;
      // 06: checkcast java/lang/Integer
      // 09: astore 1
      // 0a: aload 1
      // 0b: ifnull 26
      // 0e: aload 1
      // 0f: invokevirtual java/lang/Integer.intValue ()I
      // 12: bipush 1
      // 13: iand
      // 14: ifeq 26
      // 17: goto 1e
      // 1a: invokestatic com/l7tech/policy/code/config/PolicyCodeConfig.a (Lcom/l7tech/policy/code/AssertionCodeException;)Lcom/l7tech/policy/code/AssertionCodeException;
      // 1d: athrow
      // 1e: bipush 1
      // 1f: goto 27
      // 22: invokestatic com/l7tech/policy/code/config/PolicyCodeConfig.a (Lcom/l7tech/policy/code/AssertionCodeException;)Lcom/l7tech/policy/code/AssertionCodeException;
      // 25: athrow
      // 26: bipush 0
      // 27: ireturn
      // try (4 -> 11): 12 com/l7tech/policy/code/AssertionCodeException
      // try (6 -> 16): 16 com/l7tech/policy/code/AssertionCodeException
   }

   public boolean isNotSerializeDefaultFields() {
      try {
         if (!this.isSerializeDefaultFields()) {
            return true;
         }
      } catch (AssertionCodeException var1) {
         throw a(var1);
      }

      return false;
   }

   public boolean isNotSerializeAsObjectAlways() {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: getstatic com/l7tech/policy/code/config/PolicyCodeConfig.a Ljava/lang/ThreadLocal;
      // 03: invokevirtual java/lang/ThreadLocal.get ()Ljava/lang/Object;
      // 06: checkcast java/lang/Integer
      // 09: astore 1
      // 0a: aload 1
      // 0b: ifnull 1e
      // 0e: aload 1
      // 0f: invokevirtual java/lang/Integer.intValue ()I
      // 12: bipush 2
      // 13: iand
      // 14: ifne 26
      // 17: goto 1e
      // 1a: invokestatic com/l7tech/policy/code/config/PolicyCodeConfig.a (Lcom/l7tech/policy/code/AssertionCodeException;)Lcom/l7tech/policy/code/AssertionCodeException;
      // 1d: athrow
      // 1e: bipush 1
      // 1f: goto 27
      // 22: invokestatic com/l7tech/policy/code/config/PolicyCodeConfig.a (Lcom/l7tech/policy/code/AssertionCodeException;)Lcom/l7tech/policy/code/AssertionCodeException;
      // 25: athrow
      // 26: bipush 0
      // 27: ireturn
      // try (4 -> 11): 12 com/l7tech/policy/code/AssertionCodeException
      // try (6 -> 16): 16 com/l7tech/policy/code/AssertionCodeException
   }

   public void setAttributes(int var1) {
      a.set(var1);
   }

   public void clearAttributes() {
      a.remove();
   }

   protected void defineTransformerFor(AssertionSubTypeConfig var1) {
      var1.setTransformer(this.b.getTransformer(var1.getCategory(), var1.getTypeClass(), var1.getCategoryHint()));
   }

   static {
      String[] var5 = new String[8];
      int var3 = 0;
      String var2 = "{#\\\u0001\n80\"B\"S\u0004\u001c%\u0014A\u000eP\t\u001ar<U#P\u001f\u001a\u000e0T(M\b\u0011?0V\nJ\u0004\u001b/\"B\"S\u0004\u001c%\u0014A\u000eP\t\u001ar<U#P\u001f\u001a\u000e0T(M\b\u0011?0V\nJ\u0004\u001b/\u000fq8L\u0019\u00101\u0014A>Z\u001f\u000b5:\\\fw#\\\f\u000f/ ^,K\b\u001b g#T\u0003\u0010+;\u0012,L\u001e\u001a.![\"QA_9-F(M\u0003\u001e0\u001bS ZP";
      int var4 = "{#\\\u0001\n80\"B\"S\u0004\u001c%\u0014A\u000eP\t\u001ar<U#P\u001f\u001a\u000e0T(M\b\u0011?0V\nJ\u0004\u001b/\"B\"S\u0004\u001c%\u0014A\u000eP\t\u001ar<U#P\u001f\u001a\u000e0T(M\b\u0011?0V\nJ\u0004\u001b/\u000fq8L\u0019\u00101\u0014A>Z\u001f\u000b5:\\\fw#\\\f\u000f/ ^,K\b\u001b g#T\u0003\u0010+;\u0012,L\u001e\u001a.![\"QA_9-F(M\u0003\u001e0\u001bS ZP"
         .length();
      char var1 = 7;
      int var7 = -1;

      label24:
      while (true) {
         byte var10000 = 54;
         String var10001 = var2.substring(++var7, var7 + var1);
         byte var10002 = -1;

         while (true) {
            String var9 = a(var10000, d(var10001));
            switch (var10002) {
               case 0:
                  var5[var3++] = var9;
                  if ((var7 += var1) >= var4) {
                     l = var5;
                     String[] var10 = l;
                     CUSTOM_ASSERTION_EXT_NAME = l[3];
                     ENCASS_EXT_NAME = var10[4];
                     IGNORE_REFERENCED_GUIDS = var10[1];
                     USE_LEGACY_CUSTOM_ASSERTION_SERIALIZER = var10[6];
                     INCLUDE_EXT_NAME = var10[0];
                     a = new ThreadLocal<>();
                     String[] var8 = l;
                     CONFIG_PROPERTIES = Set.of(var8[1], var8[6]);
                     return;
                  }

                  var1 = var2.charAt(var7);
                  break;
               default:
                  var5[var3++] = var9;
                  if ((var7 += var1) < var4) {
                     var1 = var2.charAt(var7);
                     continue label24;
                  }

                  var2 = "U5D\u0013\u000b2\u0003V\u0019G\u001e\re7V?d\u001f\u000f*!\\\u0019]\t\u001c$/d)[\u001f\u001a?+J4{\u001f\u001a\"#I3R\u001f\u001a/U5D\u0013\u000b2\u0003V\u0019G\u001e\re7V?d\u001f\u000f*!\\\u0019]\t\u001c$/d)[\u001f\u001a?+J4{\u001f\u001a\"#I3R\u001f\u001a";
                  var4 = "U5D\u0013\u000b2\u0003V\u0019G\u001e\re7V?d\u001f\u000f*!\\\u0019]\t\u001c$/d)[\u001f\u001a?+J4{\u001f\u001a\"#I3R\u001f\u001a/U5D\u0013\u000b2\u0003V\u0019G\u001e\re7V?d\u001f\u000f*!\\\u0019]\t\u001c$/d)[\u001f\u001a?+J4{\u001f\u001a\"#I3R\u001f\u001a"
                     .length();
                  var1 = '/';
                  var7 = -1;
            }

            var10000 = 33;
            var10001 = var2.substring(++var7, var7 + var1);
            var10002 = 0;
         }
      }
   }

   private static AssertionCodeException a(AssertionCodeException var0) {
      return var0;
   }

   private static char[] d(String var0) {
      char[] var10000 = var0.toCharArray();
      if (var10000.length < 2) {
         var10000[0] = (char)(var10000[0] ^ 99);
      }

      return var10000;
   }

   private static String a(int var0, char[] var1) {
      int var10003 = var1.length;
      int var10002 = var0;
      char[] var10001 = var1;
      int var10000 = var10003;

      for (int var2 = 0; var10000 > var2; var2++) {
         var10001[var2] = (char)(var10001[var2] ^ var10002 ^ switch (var2 % 7) {
            case 0 -> 4;
            case 1 -> 123;
            case 2 -> 9;
            case 3 -> 91;
            case 4 -> 73;
            case 5 -> 106;
            default -> 99;
         });
      }

      return new String(var10001).intern();
   }
}
