package com.l7tech.server.policy.assertion.composite;

import com.l7tech.gateway.common.LicenseException;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.composite.CompositeAssertion;
import com.l7tech.server.NoopTelemetryMediator;
import com.l7tech.server.TelemetryMediator;
import com.l7tech.server.TelemetryMediatorFactory;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.message.metrics.GatewayMetricsUtils;
import com.l7tech.server.message.metrics.LatencyMetrics;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.server.policy.assertion.AssertionStatusException;
import com.l7tech.server.policy.assertion.ServerAssertion;
import com.l7tech.util.TimeSource;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.BeanFactory;

public abstract class ServerCompositeAssertion<CT extends CompositeAssertion> extends AbstractServerAssertion<CT> {
   private static final Logger d;
   private List<ServerAssertion> e;
   private final boolean[] f;
   protected final TimeSource timeSource;
   @Inject
   @Named("telemetryMediatorFactory")
   TelemetryMediatorFactory telemetryMediatorFactory;
   private static final String[] o;

   public ServerCompositeAssertion(CT param1, BeanFactory param2) throws PolicyAssertionException, LicenseException {
      // $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:
      // 000: aload 0
      // 001: aload 1
      // 002: invokespecial com/l7tech/server/policy/assertion/AbstractServerAssertion.<init> (Lcom/l7tech/policy/assertion/Assertion;)V
      // 005: aload 2
      // 006: ifnonnull 01a
      // 009: new java/lang/IllegalArgumentException
      // 00c: dup
      // 00d: getstatic com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.o [Ljava/lang/String;
      // 010: astore 10
      // 012: aload 10
      // 014: bipush 0
      // 015: aaload
      // 016: invokespecial java/lang/IllegalArgumentException.<init> (Ljava/lang/String;)V
      // 019: athrow
      // 01a: aload 2
      // 01b: getstatic com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.o [Ljava/lang/String;
      // 01e: bipush 2
      // 01f: aaload
      // 020: ldc com/l7tech/server/policy/ServerPolicyFactory
      // 022: invokeinterface org/springframework/beans/factory/BeanFactory.getBean (Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Object; 3
      // 027: checkcast com/l7tech/server/policy/ServerPolicyFactory
      // 02a: astore 3
      // 02b: new java/util/ArrayList
      // 02e: dup
      // 02f: aload 1
      // 030: invokevirtual com/l7tech/policy/assertion/composite/CompositeAssertion.getChildren ()Ljava/util/List;
      // 033: invokeinterface java/util/List.size ()I 1
      // 038: invokespecial java/util/ArrayList.<init> (I)V
      // 03b: astore 4
      // 03d: aload 0
      // 03e: aload 0
      // 03f: invokevirtual com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.getTimeSource ()Lcom/l7tech/util/TimeSource;
      // 042: putfield com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.timeSource Lcom/l7tech/util/TimeSource;
      // 045: aload 0
      // 046: aload 1
      // 047: invokevirtual com/l7tech/policy/assertion/composite/CompositeAssertion.getChildren ()Ljava/util/List;
      // 04a: invokeinterface java/util/List.size ()I 1
      // 04f: newarray 4
      // 051: putfield com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.f [Z
      // 054: aconst_null
      // 055: astore 5
      // 057: bipush 0
      // 058: istore 6
      // 05a: aload 1
      // 05b: invokevirtual com/l7tech/policy/assertion/composite/CompositeAssertion.children ()Ljava/util/Iterator;
      // 05e: astore 7
      // 060: aload 7
      // 062: invokeinterface java/util/Iterator.hasNext ()Z 1
      // 067: ifeq 0ed
      // 06a: aload 7
      // 06c: invokeinterface java/util/Iterator.next ()Ljava/lang/Object; 1
      // 071: checkcast com/l7tech/policy/assertion/Assertion
      // 074: astore 8
      // 076: aload 0
      // 077: getfield com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.f [Z
      // 07a: iload 6
      // 07c: bipush 0
      // 07d: bastore
      // 07e: aload 5
      // 080: ifnull 0b3
      // 083: aload 8
      // 085: bipush 2
      // 086: anewarray 289
      // 089: dup
      // 08a: bipush 0
      // 08b: getstatic com/l7tech/policy/variable/BuiltinVariables.ASSERTION_LATENCY_MS Ljava/lang/String;
      // 08e: aastore
      // 08f: dup
      // 090: bipush 1
      // 091: getstatic com/l7tech/policy/variable/BuiltinVariables.ASSERTION_LATENCY_NS Ljava/lang/String;
      // 094: aastore
      // 095: invokestatic com/l7tech/policy/variable/PolicyVariableUtils.usesAnyVariable (Lcom/l7tech/policy/assertion/Assertion;[Ljava/lang/String;)Z
      // 098: ifeq 0b3
      // 09b: goto 0a2
      // 09e: invokestatic com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 0a1: athrow
      // 0a2: aload 0
      // 0a3: getfield com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.f [Z
      // 0a6: iload 6
      // 0a8: bipush 1
      // 0a9: isub
      // 0aa: bipush 1
      // 0ab: bastore
      // 0ac: goto 0b3
      // 0af: invokestatic com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 0b2: athrow
      // 0b3: aload 8
      // 0b5: astore 5
      // 0b7: iinc 6 1
      // 0ba: aload 3
      // 0bb: aload 8
      // 0bd: invokevirtual com/l7tech/server/policy/ServerPolicyFactory.compileSubtree (Lcom/l7tech/policy/assertion/Assertion;)Lcom/l7tech/server/policy/assertion/ServerAssertion;
      // 0c0: astore 9
      // 0c2: getstatic com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.g Z
      // 0c5: ifne 0e0
      // 0c8: aload 9
      // 0ca: ifnonnull 0e0
      // 0cd: goto 0d4
      // 0d0: invokestatic com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 0d3: athrow
      // 0d4: new java/lang/AssertionError
      // 0d7: dup
      // 0d8: invokespecial java/lang/AssertionError.<init> ()V
      // 0db: athrow
      // 0dc: invokestatic com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 0df: athrow
      // 0e0: aload 4
      // 0e2: aload 9
      // 0e4: invokeinterface java/util/List.add (Ljava/lang/Object;)Z 2
      // 0e9: pop
      // 0ea: goto 060
      // 0ed: goto 143
      // 0f0: astore 5
      // 0f2: aload 4
      // 0f4: invokeinterface java/util/List.iterator ()Ljava/util/Iterator; 1
      // 0f9: astore 6
      // 0fb: aload 6
      // 0fd: invokeinterface java/util/Iterator.hasNext ()Z 1
      // 102: ifeq 119
      // 105: aload 6
      // 107: invokeinterface java/util/Iterator.next ()Ljava/lang/Object; 1
      // 10c: checkcast com/l7tech/server/policy/assertion/ServerAssertion
      // 10f: astore 7
      // 111: aload 7
      // 113: invokestatic com/l7tech/util/ResourceUtils.closeQuietly (Ljava/io/Closeable;)V
      // 116: goto 0fb
      // 119: aload 5
      // 11b: instanceof com/l7tech/policy/assertion/PolicyAssertionException
      // 11e: ifeq 12b
      // 121: aload 5
      // 123: checkcast com/l7tech/policy/assertion/PolicyAssertionException
      // 126: athrow
      // 127: invokestatic com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 12a: athrow
      // 12b: aload 5
      // 12d: instanceof com/l7tech/gateway/common/LicenseException
      // 130: ifeq 13d
      // 133: aload 5
      // 135: checkcast com/l7tech/gateway/common/LicenseException
      // 138: athrow
      // 139: invokestatic com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 13c: athrow
      // 13d: aload 5
      // 13f: invokestatic com/l7tech/util/ExceptionUtils.wrap (Ljava/lang/Throwable;)Ljava/lang/RuntimeException;
      // 142: athrow
      // 143: aload 0
      // 144: aload 4
      // 146: invokestatic java/util/Collections.unmodifiableList (Ljava/util/List;)Ljava/util/List;
      // 149: putfield com/l7tech/server/policy/assertion/composite/ServerCompositeAssertion.e Ljava/util/List;
      // 14c: return
      // try (95 -> 104): 104 java/lang/Exception
      // try (93 -> 97): 98 java/lang/Exception
      // try (60 -> 83): 84 java/lang/Exception
      // try (53 -> 73): 74 java/lang/Exception
      // try (39 -> 111): 112 java/lang/Exception
      // try (126 -> 132): 132 java/lang/Exception
      // try (134 -> 140): 140 java/lang/Exception
   }

   protected TimeSource getTimeSource() {
      return new TimeSource();
   }

   public List<ServerAssertion> getChildren() {
      return this.e;
   }

   @Override
   public String toString() {
      return "<" + this.getClass().getName() + o[1] + this.e + ">";
   }

   protected void seenAssertionStatus(PolicyEnforcementContext var1, AssertionStatus var2) {
      try {
         if (var1 != null) {
            var1.addSeenAssertionStatus(var2);
         }
      } catch (IllegalArgumentException var3) {
         throw a(var3);
      }
   }

   protected void rollbackDeferredAssertions(PolicyEnforcementContext var1) {
      try {
         if (var1 != null) {
            var1.removeDeferredAssertion(this);
         }
      } catch (IllegalArgumentException var6) {
         throw a(var6);
      }

      for (ServerAssertion var3 : this.e) {
         try {
            if (var3 instanceof ServerCompositeAssertion) {
               ((ServerCompositeAssertion)var3).rollbackDeferredAssertions(var1);
            }
         } catch (IllegalArgumentException var5) {
            throw a(var5);
         }

         try {
            if (var1 != null) {
               var1.removeDeferredAssertion(var3);
            }
         } catch (IllegalArgumentException var4) {
            throw a(var4);
         }
      }
   }

   protected final AssertionStatus iterateChildren(PolicyEnforcementContext var1, ServerCompositeAssertion.AssertionResultListener var2) throws IOException, PolicyAssertionException {
      List var3 = this.getChildren();
      AssertionStatus var4 = AssertionStatus.NONE;

      TelemetryMediator var10000;
      label106: {
         try {
            if (this.telemetryMediatorFactory != null) {
               var10000 = this.telemetryMediatorFactory.createTelemetryMediator();
               break label106;
            }
         } catch (AssertionStatusException var28) {
            throw a(var28);
         }

         var10000 = NoopTelemetryMediator.INSTANCE;
      }

      TelemetryMediator var5 = var10000;
      int var6 = 0;
      long var7 = 0L;

      for (ServerAssertion var10 : var3) {
         var1.assertionStarting(var10);
         if (this.f[var6]) {
            var7 = this.timeSource.nanoTime();
         }

         long var11 = this.timeSource.currentTimeMillis();
         var5.getStartAssertionEvent(var10.getAssertion(), var1);

         try {
            var4 = var10.checkRequest(var1);
            var5.getStatusMsg(var4);
         } catch (AssertionStatusException var24) {
            var5.getStatusException(var24);
            var4 = var24.getAssertionStatus();
         } catch (Throwable var25) {
            var5.getThrowableInfo(var25);
            GatewayMetricsUtils.publishAssertionFinish(var1, var10, new LatencyMetrics(var11, this.timeSource.currentTimeMillis()));
            throw var25;
         } finally {
            long var13 = this.timeSource.currentTimeMillis();
            var5.getEndAssertionEvent(var10.getAssertion(), var1);
         }

         try {
            if (this.f[var6]) {
               var1.setAssertionLatencyNanos(this.timeSource.nanoTime() - var7);
            }
         } catch (AssertionStatusException var27) {
            throw a(var27);
         }

         var6++;
         long var29;
         var1.assertionFinished(var10, var4, new LatencyMetrics(var11, var29));
         if (var2 != null) {
            boolean var15 = var2.assertionFinished(var1, var4);

            try {
               if (!var15) {
                  return var4;
               }
            } catch (AssertionStatusException var23) {
               throw a(var23);
            }
         }
      }

      return var4;
   }

   @Override
   public void close() {
      for (ServerAssertion var2 : this.e) {
         try {
            var2.close();
         } catch (Exception var4) {
            d.log(Level.WARNING, o[3], var4);
         }
      }
   }

   static {
      String[] var5 = new String[4];
      int var3 = 0;
      String var2 = "JXG2b\u0012crYAsW\u000b|p\u0010a}M\u0016vfD\u0002{PBa{AW{Q\u0007w\n>SJ{O\u0006a{^\u001f";
      int var4 = "JXG2b\u0012crYAsW\u000b|p\u0010a}M\u0016vfD\u0002{PBa{AW{Q\u0007w\n>SJ{O\u0006a{^\u001f".length();
      char var1 = '#';
      int var8 = -1;

      label39:
      while (true) {
         byte var10000 = 92;
         String var10001 = var2.substring(++var8, var8 + var1);
         byte var10002 = -1;

         while (true) {
            String var9 = b(var10000, a(var10001));
            switch (var10002) {
               case 0:
                  var5[var3++] = var9;
                  if ((var8 += var1) >= var4) {
                     o = var5;

                     label26: {
                        try {
                           if (!ServerCompositeAssertion.class.desiredAssertionStatus()) {
                              var10 = true;
                              break label26;
                           }
                        } catch (IllegalArgumentException var6) {
                           throw a(var6);
                        }

                        var10 = false;
                     }

                     g = var10;
                     d = Logger.getLogger(ServerCompositeAssertion.class.getName());
                     return;
                  }

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

                  var2 = " \u0011\u00005\u000eU\u001b1\u001d\u00183\u001fU%\u0013\u0016\u00050\t\f<#\r\t.\u0019E2>^\b5\t\f3?\nL?\u0001C.5^\u000f0\bM3<\u0007";
                  var4 = " \u0011\u00005\u000eU\u001b1\u001d\u00183\u001fU%\u0013\u0016\u00050\t\f<#\r\t.\u0019E2>^\b5\t\f3?\nL?\u0001C.5^\u000f0\bM3<\u0007"
                     .length();
                  var1 = '\r';
                  var8 = -1;
            }

            var10000 = 18;
            var10001 = var2.substring(++var8, var8 + var1);
            var10002 = 0;
         }
      }
   }

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

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

      return var10000;
   }

   private static String b(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 -> 66;
            case 1 -> 108;
            case 2 -> 126;
            case 3 -> 78;
            case 4 -> 127;
            case 5 -> 62;
            default -> 79;
         });
      }

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

   protected interface AssertionResultListener {
      boolean assertionFinished(PolicyEnforcementContext var1, AssertionStatus var2);
   }
}
