package com.l7tech.message;

import com.l7tech.common.io.XmlUtil;
import com.l7tech.common.mime.NoSuchPartException;
import com.l7tech.common.mime.PartInfo;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.xml.DomElementCursor;
import com.l7tech.xml.ElementCursor;
import java.io.IOException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

class m implements XmlKnob {
   private final MimeKnob a;
   final XmlFacet this$0;
   private static final String[] b;

   public m(final XmlFacet param1, MimeKnob nullx) {
      this.this$0 = var1;
      if (nullx == null) {
         throw new NullPointerException();
      }

      this.a = nullx;
   }

   @Override
   public ElementCursor getElementCursor() throws SAXException, IOException {
      this.getDocumentReadOnly();
      return this.this$0.d;
   }

   @Override
   public Document getDocumentReadOnly() throws SAXException, IOException {
      if (this.this$0.d == null) {
         PartInfo var1 = this.a.getFirstPart();

         try {
            if (!var1.getContentType().isXml()) {
               throw new SAXException(b[1]);
            }
         } catch (NoSuchPartException var4) {
            throw a(var4);
         }

         try {
            this.this$0.d = new DomElementCursor(XmlUtil.parse(var1.getInputStream(false)));
         } catch (NoSuchPartException var3) {
            throw new SAXException(b[0] + ExceptionUtils.getMessage(var3), var3);
         }
      }

      return this.this$0.d.getDocument();
   }

   @Override
   public Document getDocumentWritable() throws SAXException, IOException {
      // $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 0
      // 01: invokevirtual com/l7tech/message/m.getDocumentReadOnly ()Lorg/w3c/dom/Document;
      // 04: astore 1
      // 05: aload 0
      // 06: getfield com/l7tech/message/m.this$0 Lcom/l7tech/message/XmlFacet;
      // 09: bipush 0
      // 0a: putfield com/l7tech/message/XmlFacet.e Z
      // 0d: aload 0
      // 0e: getfield com/l7tech/message/m.this$0 Lcom/l7tech/message/XmlFacet;
      // 11: invokevirtual com/l7tech/message/XmlFacet.getMessage ()Lcom/l7tech/message/Message;
      // 14: invokevirtual com/l7tech/message/Message.b ()V
      // 17: aload 0
      // 18: getfield com/l7tech/message/m.this$0 Lcom/l7tech/message/XmlFacet;
      // 1b: invokevirtual com/l7tech/message/XmlFacet.getMessage ()Lcom/l7tech/message/Message;
      // 1e: invokevirtual com/l7tech/message/Message.isEnableOriginalDocument ()Z
      // 21: ifeq 4d
      // 24: aload 0
      // 25: getfield com/l7tech/message/m.this$0 Lcom/l7tech/message/XmlFacet;
      // 28: getfield com/l7tech/message/XmlFacet.c Lorg/w3c/dom/Document;
      // 2b: ifnonnull 4d
      // 2e: goto 35
      // 31: invokestatic com/l7tech/message/m.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 34: athrow
      // 35: aload 0
      // 36: getfield com/l7tech/message/m.this$0 Lcom/l7tech/message/XmlFacet;
      // 39: aload 1
      // 3a: bipush 1
      // 3b: invokeinterface org/w3c/dom/Document.cloneNode (Z)Lorg/w3c/dom/Node; 2
      // 40: checkcast org/w3c/dom/Document
      // 43: putfield com/l7tech/message/XmlFacet.c Lorg/w3c/dom/Document;
      // 46: goto 4d
      // 49: invokestatic com/l7tech/message/m.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 4c: athrow
      // 4d: aload 1
      // 4e: areturn
      // try (3 -> 20): 21 org/xml/sax/SAXException
      // try (16 -> 30): 31 org/xml/sax/SAXException
   }

   @Override
   public Document getOriginalDocument() throws SAXException, IOException {
      try {
         if (!this.this$0.getMessage().isEnableOriginalDocument()) {
            throw new UnsupportedOperationException(b[4]);
         }
      } catch (SAXException var1) {
         throw a(var1);
      }

      try {
         if (this.this$0.c == null) {
            this.this$0.c = (Document)this.getDocumentReadOnly().cloneNode(true);
         }
      } catch (SAXException var2) {
         throw a(var2);
      }

      return this.this$0.c;
   }

   @Override
   public InputSource getInputSource(boolean var1) throws IOException, SAXException {
      PartInfo var2 = this.this$0.getMessage().getMimeKnob().getFirstPart();

      try {
         if (!var2.getContentType().isXml()) {
            throw new SAXException(b[2]);
         }
      } catch (NoSuchPartException var5) {
         throw a(var5);
      }

      try {
         return new InputSource(var2.getInputStream(var1));
      } catch (NoSuchPartException var4) {
         throw new IOException(b[3] + ExceptionUtils.getMessage(var4), var4);
      }
   }

   @Override
   public void setDocument(Document var1) {
      this.this$0.e = false;
      this.this$0.d = new DomElementCursor(var1);
      this.this$0.getMessage().b();
   }

   @Override
   public boolean isDomParsed() {
      try {
         if (this.this$0.d != null) {
            return true;
         }
      } catch (NullPointerException var1) {
         throw a(var1);
      }

      return false;
   }

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

   static {
      String[] var5 = new String[5];
      int var3 = 0;
      String var2 = "\u0003n?YT\u0015W\"o~KY\u0002\u00043 \u0006vtJW0\u0015o0O]\u001e\u0003vt'K]P\u00180 8RJ\u0003\u0003vp?ILP\u00180 3^K\u0003\u00161e~RKP\u00199t~cu<0\u0015o0O]\u001e\u0003vt'K]P\u00180 8RJ\u0003\u0003vp?ILP\u00180 3^K\u0003\u00161e~RKP\u00199t~cu<";
      int var4 = "\u0003n?YT\u0015W\"o~KY\u0002\u00043 \u0006vtJW0\u0015o0O]\u001e\u0003vt'K]P\u00180 8RJ\u0003\u0003vp?ILP\u00180 3^K\u0003\u00161e~RKP\u00199t~cu<0\u0015o0O]\u001e\u0003vt'K]P\u00180 8RJ\u0003\u0003vp?ILP\u00180 3^K\u0003\u00161e~RKP\u00199t~cu<"
         .length();
      char var1 = 21;
      int var7 = -1;

      label24:
      while (true) {
         byte var10000 = 77;
         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) {
                     b = var5;
                     return;
                  }

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

                  var2 = "i\u0004U3>\u007f=H\u0005\u001401yxO\u0019\u00147;hnHJD0 n=S\f\u0014<7in]\rQkr&S\u0018]6;t|P.[2'wxR\u001eg$\"jrN\u001e\u00148!:sS\u001e\u00144<{\u007fP\u000fP";
                  var4 = "i\u0004U3>\u007f=H\u0005\u001401yxO\u0019\u00147;hnHJD0 n=S\f\u0014<7in]\rQkr&S\u0018]6;t|P.[2'wxR\u001eg$\"jrN\u001e\u00148!:sS\u001e\u00144<{\u007fP\u000fP"
                     .length();
                  var1 = '(';
                  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] ^ 58);
      }

      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 -> 27;
            case 1 -> 77;
            case 2 -> 19;
            case 3 -> 118;
            case 4 -> 117;
            case 5 -> 61;
            default -> 58;
         });
      }

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