package com.l7tech.server.custom.format;

import com.l7tech.policy.assertion.ext.message.CustomJsonData;
import com.l7tech.policy.assertion.ext.message.format.CustomMessageFormat;
import com.l7tech.policy.assertion.ext.message.format.CustomMessageFormatFactory;
import com.l7tech.server.StashManagerFactory;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Document;

public class CustomMessageFormatRegistry {
   private final Map<Class, CustomMessageFormat> a;
   private final CustomMessageFormatFactory b;
   private static final String[] c;

   public final Map<Class, CustomMessageFormat> getFormatMap() {
      return this.a;
   }

   public final CustomMessageFormatFactory getMessageFormatFactory() {
      return this.b;
   }

   public CustomMessageFormatRegistry(@NotNull final StashManagerFactory var1) {
      if (var1 == null) {
         a(0);
      }

      this(new HashMap<Class, CustomMessageFormat>() {
         static final long serialVersionUID = 7625612101453042151L;
         private static final String[] a;

         {
            String[] var2 = a;
            this.put(Document.class, new CustomMessageXmlFormat(var1, var2[2], var2[4]));
            this.put(CustomJsonData.class, new CustomMessageJsonFormat(var1, var2[0], var2[5]));
            this.put(InputStream.class, new CustomMessageInputStreamFormat(var1, var2[3], var2[1]));
         }

         static {
            String[] var5 = new String[6];
            int var3 = 0;
            String var2 = "xMg%\"{pX\u001ei[ FlM\np[0Gm\\\u0004p[>Wm[\nz\u001eStqZ\u0006|\u000f\u0003jSd\u000b{pX\u001ei(\u0007@{I\u0006";
            int var4 = "xMg%\"{pX\u001ei[ FlM\np[0Gm\\\u0004p[>Wm[\nz\u001eStqZ\u0006|\u000f\u0003jSd\u000b{pX\u001ei(\u0007@{I\u0006".length();
            char var1x = 4;
            int var7 = -1;

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

               while (true) {
                  String var8 = a(var10000, a(var10001));
                  switch (var10002) {
                     case 0:
                        var5[var3++] = var8;
                        if ((var7 += var1x) >= var4) {
                           a = var5;
                           return;
                        }

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

                        var2 = "G`Tzo?1w@tza/1pN~?\f\f-qBx.\u001aI|V\u0014\f\t7p[v7\f\u0007'p\\x=Ij\u0004l]t;X";
                        var4 = "G`Tzo?1w@tza/1pN~?\f\f-qBx.\u001aI|V\u0014\f\t7p[v7\f\u0007'p\\x=Ij\u0004l]t;X".length();
                        var1x = 25;
                        var7 = -1;
                  }

                  var10000 = 60;
                  var10001 = var2.substring(++var7, var7 + var1x);
                  var10002 = 0;
               }
            }
         }

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

            return var10000;
         }

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

            for (int var2 = 0; var10000 > var2; var2++) {
               var10001[var2] = (char)(var10001[var2] ^ var10002 ^ switch (var2 % 7) {
                  case 0 -> 63;
                  case 1 -> 19;
                  case 2 -> 37;
                  case 3 -> 102;
                  case 4 -> 16;
                  case 5 -> 118;
                  default -> 126;
               });
            }

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

   protected CustomMessageFormatRegistry(@NotNull Map<Class, CustomMessageFormat> var1) {
      if (var1 == null) {
         a(1);
      }

      super();
      this.a = new ConcurrentHashMap<>(var1);
      this.b = new CustomMessageFormatFactoryImpl(this);
   }

   public <T> void register(@NotNull Class<T> var1, @NotNull CustomMessageFormat var2) {
      try {
         if (var1 == null) {
            a(2);
         }
      } catch (IllegalArgumentException var3) {
         throw a(var3);
      }

      try {
         if (var2 == null) {
            a(3);
         }
      } catch (IllegalArgumentException var4) {
         throw a(var4);
      }

      this.getFormatMap().put(var1, var2);
   }

   public <T> CustomMessageFormat remove(@NotNull Class<T> var1) {
      try {
         if (var1 == null) {
            a(4);
         }
      } catch (IllegalArgumentException var2) {
         throw a(var2);
      }

      return this.getFormatMap().remove(var1);
   }

   public <T> CustomMessageFormat<T> get(@NotNull Class<T> var1) {
      try {
         if (var1 == null) {
            a(5);
         }
      } catch (IllegalArgumentException var2) {
         throw a(var2);
      }

      return this.getFormatMap().get(var1);
   }

   public CustomMessageFormat getForName(@NotNull String param1) {
      // $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: aload 1
      // 01: ifnonnull 10
      // 04: bipush 6
      // 06: invokestatic com/l7tech/server/custom/format/CustomMessageFormatRegistry.a (I)V
      // 09: goto 10
      // 0c: invokestatic com/l7tech/server/custom/format/CustomMessageFormatRegistry.a (Ljava/lang/IllegalArgumentException;)Ljava/lang/IllegalArgumentException;
      // 0f: athrow
      // 10: aload 0
      // 11: invokevirtual com/l7tech/server/custom/format/CustomMessageFormatRegistry.getFormatMap ()Ljava/util/Map;
      // 14: invokeinterface java/util/Map.values ()Ljava/util/Collection; 1
      // 19: invokeinterface java/util/Collection.iterator ()Ljava/util/Iterator; 1
      // 1e: astore 2
      // 1f: aload 2
      // 20: invokeinterface java/util/Iterator.hasNext ()Z 1
      // 25: ifeq 58
      // 28: aload 2
      // 29: invokeinterface java/util/Iterator.next ()Ljava/lang/Object; 1
      // 2e: checkcast com/l7tech/policy/assertion/ext/message/format/CustomMessageFormat
      // 31: astore 3
      // 32: aload 3
      // 33: invokeinterface com/l7tech/policy/assertion/ext/message/format/CustomMessageFormat.getFormatName ()Ljava/lang/String; 1
      // 38: astore 4
      // 3a: aload 4
      // 3c: ifnull 55
      // 3f: aload 1
      // 40: aload 4
      // 42: invokevirtual java/lang/String.compareToIgnoreCase (Ljava/lang/String;)I
      // 45: ifne 55
      // 48: goto 4f
      // 4b: invokestatic com/l7tech/server/custom/format/CustomMessageFormatRegistry.a (Ljava/lang/IllegalArgumentException;)Ljava/lang/IllegalArgumentException;
      // 4e: athrow
      // 4f: aload 3
      // 50: areturn
      // 51: invokestatic com/l7tech/server/custom/format/CustomMessageFormatRegistry.a (Ljava/lang/IllegalArgumentException;)Ljava/lang/IllegalArgumentException;
      // 54: athrow
      // 55: goto 1f
      // 58: aconst_null
      // 59: areturn
      // try (0 -> 4): 5 java/lang/IllegalArgumentException
      // try (22 -> 28): 29 java/lang/IllegalArgumentException
      // try (24 -> 33): 33 java/lang/IllegalArgumentException
   }

   public Collection<CustomMessageFormat> getKnownFormats() {
      return Collections.unmodifiableCollection(this.getFormatMap().values());
   }

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

   static {
      String[] var5 = new String[12];
      int var3 = 0;
      String var2 = "d4J\u0010\u0010b-b<[\u0007e>L;\u001ed\u0010>B#Y#\u0012u\rwqX9\r0#M>J\u0018\n|\u000f#!_$\u001e}\u0006w4LvX5\u0010$qQ0_5\u0010-tMv\u0012e\u0010wqP9\u000b0\u0001fqP#\u0013|\bq4Y?\fd\u0006q\u0013q4N$\u001ac\u0006m%_\"\u0016\u007f\r@=_%\f\u0006?8P?\u000b.;`>Sy\u0013'\u0017f2Vy\fu\u0011u4Ly\u001ce\u0010w>Sy\u0019\u007f\u0011n0Jy<e\u0010w>S\u001b\u001ac\u0010b6[\u0010\u0010b\u000eb%l3\u0018y\u0010w#G\u0006q4S9\tu\u0003d4J\ne>L;\u001ed-b<[";
      int var4 = "d4J\u0010\u0010b-b<[\u0007e>L;\u001ed\u0010>B#Y#\u0012u\rwqX9\r0#M>J\u0018\n|\u000f#!_$\u001e}\u0006w4LvX5\u0010$qQ0_5\u0010-tMv\u0012e\u0010wqP9\u000b0\u0001fqP#\u0013|\bq4Y?\fd\u0006q\u0013q4N$\u001ac\u0006m%_\"\u0016\u007f\r@=_%\f\u0006?8P?\u000b.;`>Sy\u0013'\u0017f2Vy\fu\u0011u4Ly\u001ce\u0010w>Sy\u0019\u007f\u0011n0Jy<e\u0010w>S\u001b\u001ac\u0010b6[\u0010\u0010b\u000eb%l3\u0018y\u0010w#G\u0006q4S9\tu\u0003d4J\ne>L;\u001ed-b<["
         .length();
      char var1 = '\n';
      int var7 = -1;

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

         while (true) {
            String var8 = a(var10000, a(var10001));
            switch (var10002) {
               case 0:
                  var5[var3++] = var8;
                  if ((var7 += var1) >= var4) {
                     c = var5;
                     return;
                  }

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

                  var2 = "\u0007\\.Y|\u0006\u0013\u0012G=Gu?`\u000fR;Qo4`\u0002G3Fd";
                  var4 = "\u0007\\.Y|\u0006\u0013\u0012G=Gu?`\u000fR;Qo4`\u0002G3Fd".length();
                  var1 = 6;
                  var7 = -1;
            }

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

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

      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 -> 70;
            case 1 -> 20;
            case 2 -> 123;
            case 3 -> 19;
            case 4 -> 58;
            case 5 -> 85;
            default -> 38;
         });
      }

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