package com.l7tech.external.assertions.gatewaymanagement.server;

import com.l7tech.gateway.common.security.keystore.SsgKeyEntry;
import com.l7tech.gateway.common.security.rbac.OperationType;
import com.l7tech.objectmodel.DeleteException;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.ObjectModelException;
import com.l7tech.server.admin.PrivateKeyAdminHelper;
import com.l7tech.server.security.keystore.SsgKeyStore;
import com.l7tech.util.Either;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.Pair;
import java.security.KeyStoreException;
import java.util.Map;

class b5 implements ResourceFactorySupport.TransactionalCallback<Either<ResourceFactory.ResourceNotFoundException, String>> {
   final Map val$selectorMap;
   final PrivateKeyResourceFactory this$0;
   private static final String[] a;

   b5(PrivateKeyResourceFactory var1, Map var2) {
      this.this$0 = var1;
      this.val$selectorMap = var2;
   }

   public Either<ResourceFactory.ResourceNotFoundException, String> execute() throws ObjectModelException {
      try {
         Pair var1 = this.this$0.a(this.val$selectorMap);
         SsgKeyEntry var2 = this.this$0.a(var1);
         this.this$0.checkPermitted(OperationType.DELETE, null, var2);
         SsgKeyStore var3 = this.this$0.a(var2.getKeystoreId());

         try {
            PrivateKeyAdminHelper var4 = this.this$0.a();

            try {
               if (var4.isKeyActive(var3, var2.getAlias())) {
                  throw new ResourceFactory.ResourceAccessException(a[0]);
               }
            } catch (KeyStoreException var5) {
               throw a(var5);
            }

            var4.validateKeyDeletionAllowed((Goid)var1.left, (String)var1.right);
            var4.doDeletePrivateKeyEntry(var3, var2.getAlias());
         } catch (KeyStoreException | FindException var6) {
            throw new DeleteException(a[1] + ExceptionUtils.getMessage(var6), var6);
         }

         return Either.right(PrivateKeyResourceFactory.a((Goid)var1.left, (String)var1.right));
      } catch (ResourceFactory.ResourceNotFoundException var7) {
         return Either.left(var7);
      }
   }

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

   static {
      String[] var5 = new String[2];
      int var3 = 0;
      String var2 = "{i\u00065G\u0005wBuJ5J\n6Go\u000er\u0014nt\u00183V\\3Nj\u000f(M\u00120\u000bm\u000f%\u001e\\";
      int var4 = "{i\u00065G\u0005wBuJ5J\n6Go\u000er\u0014nt\u00183V\\3Nj\u000f(M\u00120\u000bm\u000f%\u001e\\".length();
      char var1 = 18;
      int var0 = -1;

      while (true) {
         String var10002 = a(64, a(var2.substring(++var0, var0 + var1)));
         byte var10001 = -1;
         var5[var3++] = var10002;
         if ((var0 += var1) >= var4) {
            a = var5;
            return;
         }

         var1 = var2.charAt(var0);
      }
   }

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

      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 -> 107;
            case 1 -> 70;
            case 2 -> 42;
            case 3 -> 28;
            case 4 -> 100;
            case 5 -> 60;
            default -> 23;
         });
      }

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