package g;

import com.seewo.libmcuservice.constant.IMcuConstants;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;

/* JADX INFO: loaded from: classes.dex */
public final class s {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final char[] f6439a = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final String f6440b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final String f6441c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final String f6442d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final String f6443e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final int f6444f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final List<String> f6445g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    @Nullable
    public final List<String> f6446h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    @Nullable
    public final String f6447i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final String f6448j;

    public static final class a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        @Nullable
        public String f6449a;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        @Nullable
        public String f6452d;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public final List<String> f6454f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        @Nullable
        public List<String> f6455g;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        @Nullable
        public String f6456h;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public String f6450b = "";

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public String f6451c = "";

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public int f6453e = -1;

        public a() {
            ArrayList arrayList = new ArrayList();
            this.f6454f = arrayList;
            arrayList.add("");
        }

        public s a() {
            if (this.f6449a == null) {
                throw new IllegalStateException("scheme == null");
            }
            if (this.f6452d != null) {
                return new s(this);
            }
            throw new IllegalStateException("host == null");
        }

        public a b(@Nullable String str) {
            this.f6455g = str != null ? s.p(s.b(str, " \"'<>#", true, false, true, true)) : null;
            return this;
        }

        /* JADX WARN: Removed duplicated region for block: B:139:0x02a8  */
        /* JADX WARN: Removed duplicated region for block: B:33:0x0062  */
        /* JADX WARN: Unreachable blocks removed: 1, instructions: 2 */
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        public g.s.a c(@javax.annotation.Nullable g.s r23, java.lang.String r24) {
            /*
                Method dump skipped, instruction units count: 970
                To view this dump add '--comments-level debug' option
            */
            throw new UnsupportedOperationException("Method not decompiled: g.s.a.c(g.s, java.lang.String):g.s$a");
        }

        public String toString() {
            String str;
            StringBuilder sb = new StringBuilder();
            String str2 = this.f6449a;
            if (str2 != null) {
                sb.append(str2);
                str = "://";
            } else {
                str = "//";
            }
            sb.append(str);
            if (!this.f6450b.isEmpty() || !this.f6451c.isEmpty()) {
                sb.append(this.f6450b);
                if (!this.f6451c.isEmpty()) {
                    sb.append(':');
                    sb.append(this.f6451c);
                }
                sb.append('@');
            }
            String str3 = this.f6452d;
            if (str3 != null) {
                if (str3.indexOf(58) != -1) {
                    sb.append('[');
                    sb.append(this.f6452d);
                    sb.append(']');
                } else {
                    sb.append(this.f6452d);
                }
            }
            int iD = this.f6453e;
            if (iD != -1 || this.f6449a != null) {
                if (iD == -1) {
                    iD = s.d(this.f6449a);
                }
                String str4 = this.f6449a;
                if (str4 == null || iD != s.d(str4)) {
                    sb.append(':');
                    sb.append(iD);
                }
            }
            List<String> list = this.f6454f;
            int size = list.size();
            for (int i2 = 0; i2 < size; i2++) {
                sb.append('/');
                sb.append(list.get(i2));
            }
            if (this.f6455g != null) {
                sb.append('?');
                s.j(sb, this.f6455g);
            }
            if (this.f6456h != null) {
                sb.append('#');
                sb.append(this.f6456h);
            }
            return sb.toString();
        }
    }

    public s(a aVar) {
        this.f6440b = aVar.f6449a;
        this.f6441c = m(aVar.f6450b, false);
        this.f6442d = m(aVar.f6451c, false);
        this.f6443e = aVar.f6452d;
        int i2 = aVar.f6453e;
        this.f6444f = i2 == -1 ? d(aVar.f6449a) : i2;
        this.f6445g = n(aVar.f6454f, false);
        List<String> list = aVar.f6455g;
        this.f6446h = list != null ? n(list, true) : null;
        String str = aVar.f6456h;
        this.f6447i = str != null ? l(str, 0, str.length(), false) : null;
        this.f6448j = aVar.toString();
    }

    public static String a(String str, int i2, int i3, String str2, boolean z, boolean z2, boolean z3, boolean z4, Charset charset) {
        int iCharCount = i2;
        while (iCharCount < i3) {
            int iCodePointAt = str.codePointAt(iCharCount);
            int i4 = -1;
            if (iCodePointAt < 32 || iCodePointAt == 127 || ((iCodePointAt >= 128 && z4) || str2.indexOf(iCodePointAt) != -1 || ((iCodePointAt == 37 && (!z || (z2 && !o(str, iCharCount, i3)))) || (iCodePointAt == 43 && z3)))) {
                h.f fVar = new h.f();
                fVar.f0(str, i2, iCharCount);
                h.f fVar2 = null;
                while (iCharCount < i3) {
                    int iCodePointAt2 = str.codePointAt(iCharCount);
                    if (!z || (iCodePointAt2 != 9 && iCodePointAt2 != 10 && iCodePointAt2 != 12 && iCodePointAt2 != 13)) {
                        if (iCodePointAt2 == 43 && z3) {
                            fVar.e0(z ? "+" : "%2B");
                        } else if (iCodePointAt2 < 32 || iCodePointAt2 == 127 || ((iCodePointAt2 >= 128 && z4) || str2.indexOf(iCodePointAt2) != i4 || (iCodePointAt2 == 37 && (!z || (z2 && !o(str, iCharCount, i3)))))) {
                            if (fVar2 == null) {
                                fVar2 = new h.f();
                            }
                            if (charset == null || charset.equals(g.i0.c.f6148i)) {
                                fVar2.g0(iCodePointAt2);
                            } else {
                                int iCharCount2 = Character.charCount(iCodePointAt2) + iCharCount;
                                if (iCharCount < 0) {
                                    throw new IllegalAccessError(a.b.c.a.a.o("beginIndex < 0: ", iCharCount));
                                }
                                if (iCharCount2 < iCharCount) {
                                    throw new IllegalArgumentException("endIndex < beginIndex: " + iCharCount2 + " < " + iCharCount);
                                }
                                if (iCharCount2 > str.length()) {
                                    StringBuilder sbD = a.b.c.a.a.D("endIndex > string.length: ", iCharCount2, " > ");
                                    sbD.append(str.length());
                                    throw new IllegalArgumentException(sbD.toString());
                                }
                                if (charset.equals(h.z.f6575a)) {
                                    fVar2.f0(str, iCharCount, iCharCount2);
                                } else {
                                    byte[] bytes = str.substring(iCharCount, iCharCount2).getBytes(charset);
                                    fVar2.Y(bytes, 0, bytes.length);
                                }
                            }
                            while (!fVar2.q()) {
                                int iJ = fVar2.J() & IMcuConstants.DEFAULT_VALUE;
                                fVar.Z(37);
                                char[] cArr = f6439a;
                                fVar.Z(cArr[(iJ >> 4) & 15]);
                                fVar.Z(cArr[iJ & 15]);
                            }
                        } else {
                            fVar.g0(iCodePointAt2);
                        }
                    }
                    iCharCount += Character.charCount(iCodePointAt2);
                    i4 = -1;
                }
                return fVar.T();
            }
            iCharCount += Character.charCount(iCodePointAt);
        }
        return str.substring(i2, i3);
    }

    public static String b(String str, String str2, boolean z, boolean z2, boolean z3, boolean z4) {
        return a(str, 0, str.length(), str2, z, z2, z3, z4, null);
    }

    public static String c(String str, String str2, boolean z, boolean z2, boolean z3, boolean z4, Charset charset) {
        return a(str, 0, str.length(), str2, z, z2, z3, z4, null);
    }

    public static int d(String str) {
        if (str.equals("http")) {
            return 80;
        }
        return str.equals("https") ? 443 : -1;
    }

    public static void j(StringBuilder sb, List<String> list) {
        int size = list.size();
        for (int i2 = 0; i2 < size; i2 += 2) {
            String str = list.get(i2);
            String str2 = list.get(i2 + 1);
            if (i2 > 0) {
                sb.append('&');
            }
            sb.append(str);
            if (str2 != null) {
                sb.append('=');
                sb.append(str2);
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:23:0x0052  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public static java.lang.String l(java.lang.String r8, int r9, int r10, boolean r11) {
        /*
            r0 = r9
        L1:
            if (r0 >= r10) goto L60
            char r1 = r8.charAt(r0)
            r2 = 43
            r3 = 37
            if (r1 == r3) goto L15
            if (r1 != r2) goto L12
            if (r11 == 0) goto L12
            goto L15
        L12:
            int r0 = r0 + 1
            goto L1
        L15:
            h.f r1 = new h.f
            r1.<init>()
            r1.f0(r8, r9, r0)
        L1d:
            if (r0 >= r10) goto L5b
            int r9 = r8.codePointAt(r0)
            if (r9 != r3) goto L48
            int r4 = r0 + 2
            if (r4 >= r10) goto L48
            int r5 = r0 + 1
            char r5 = r8.charAt(r5)
            int r5 = g.i0.c.f(r5)
            char r6 = r8.charAt(r4)
            int r6 = g.i0.c.f(r6)
            r7 = -1
            if (r5 == r7) goto L52
            if (r6 == r7) goto L52
            int r0 = r5 << 4
            int r0 = r0 + r6
            r1.Z(r0)
            r0 = r4
            goto L55
        L48:
            if (r9 != r2) goto L52
            if (r11 == 0) goto L52
            r4 = 32
            r1.Z(r4)
            goto L55
        L52:
            r1.g0(r9)
        L55:
            int r9 = java.lang.Character.charCount(r9)
            int r0 = r0 + r9
            goto L1d
        L5b:
            java.lang.String r8 = r1.T()
            return r8
        L60:
            java.lang.String r8 = r8.substring(r9, r10)
            return r8
        */
        throw new UnsupportedOperationException("Method not decompiled: g.s.l(java.lang.String, int, int, boolean):java.lang.String");
    }

    public static String m(String str, boolean z) {
        return l(str, 0, str.length(), z);
    }

    public static boolean o(String str, int i2, int i3) {
        int i4 = i2 + 2;
        return i4 < i3 && str.charAt(i2) == '%' && g.i0.c.f(str.charAt(i2 + 1)) != -1 && g.i0.c.f(str.charAt(i4)) != -1;
    }

    public static List<String> p(String str) {
        String strSubstring;
        ArrayList arrayList = new ArrayList();
        int i2 = 0;
        while (i2 <= str.length()) {
            int iIndexOf = str.indexOf(38, i2);
            if (iIndexOf == -1) {
                iIndexOf = str.length();
            }
            int iIndexOf2 = str.indexOf(61, i2);
            if (iIndexOf2 == -1 || iIndexOf2 > iIndexOf) {
                arrayList.add(str.substring(i2, iIndexOf));
                strSubstring = null;
            } else {
                arrayList.add(str.substring(i2, iIndexOf2));
                strSubstring = str.substring(iIndexOf2 + 1, iIndexOf);
            }
            arrayList.add(strSubstring);
            i2 = iIndexOf + 1;
        }
        return arrayList;
    }

    public String e() {
        if (this.f6442d.isEmpty()) {
            return "";
        }
        return this.f6448j.substring(this.f6448j.indexOf(58, this.f6440b.length() + 3) + 1, this.f6448j.indexOf(64));
    }

    public boolean equals(@Nullable Object obj) {
        return (obj instanceof s) && ((s) obj).f6448j.equals(this.f6448j);
    }

    public String f() {
        int iIndexOf = this.f6448j.indexOf(47, this.f6440b.length() + 3);
        String str = this.f6448j;
        return this.f6448j.substring(iIndexOf, g.i0.c.i(str, iIndexOf, str.length(), "?#"));
    }

    public List<String> g() {
        int iIndexOf = this.f6448j.indexOf(47, this.f6440b.length() + 3);
        String str = this.f6448j;
        int i2 = g.i0.c.i(str, iIndexOf, str.length(), "?#");
        ArrayList arrayList = new ArrayList();
        while (iIndexOf < i2) {
            int i3 = iIndexOf + 1;
            int iH = g.i0.c.h(this.f6448j, i3, i2, '/');
            arrayList.add(this.f6448j.substring(i3, iH));
            iIndexOf = iH;
        }
        return arrayList;
    }

    @Nullable
    public String h() {
        if (this.f6446h == null) {
            return null;
        }
        int iIndexOf = this.f6448j.indexOf(63) + 1;
        String str = this.f6448j;
        return this.f6448j.substring(iIndexOf, g.i0.c.h(str, iIndexOf, str.length(), '#'));
    }

    public int hashCode() {
        return this.f6448j.hashCode();
    }

    public String i() {
        if (this.f6441c.isEmpty()) {
            return "";
        }
        int length = this.f6440b.length() + 3;
        String str = this.f6448j;
        return this.f6448j.substring(length, g.i0.c.i(str, length, str.length(), ":@"));
    }

    @Nullable
    public a k(String str) {
        try {
            a aVar = new a();
            aVar.c(this, str);
            return aVar;
        } catch (IllegalArgumentException unused) {
            return null;
        }
    }

    public final List<String> n(List<String> list, boolean z) {
        int size = list.size();
        ArrayList arrayList = new ArrayList(size);
        for (int i2 = 0; i2 < size; i2++) {
            String str = list.get(i2);
            arrayList.add(str != null ? l(str, 0, str.length(), z) : null);
        }
        return Collections.unmodifiableList(arrayList);
    }

    public URI q() {
        a aVar = new a();
        aVar.f6449a = this.f6440b;
        aVar.f6450b = i();
        aVar.f6451c = e();
        aVar.f6452d = this.f6443e;
        aVar.f6453e = this.f6444f != d(this.f6440b) ? this.f6444f : -1;
        aVar.f6454f.clear();
        aVar.f6454f.addAll(g());
        aVar.b(h());
        aVar.f6456h = this.f6447i == null ? null : this.f6448j.substring(this.f6448j.indexOf(35) + 1);
        int size = aVar.f6454f.size();
        for (int i2 = 0; i2 < size; i2++) {
            aVar.f6454f.set(i2, b(aVar.f6454f.get(i2), "[]", true, true, false, true));
        }
        List<String> list = aVar.f6455g;
        if (list != null) {
            int size2 = list.size();
            for (int i3 = 0; i3 < size2; i3++) {
                String str = aVar.f6455g.get(i3);
                if (str != null) {
                    aVar.f6455g.set(i3, b(str, "\\^`{|}", true, true, true, true));
                }
            }
        }
        String str2 = aVar.f6456h;
        if (str2 != null) {
            aVar.f6456h = b(str2, " \"#<>\\^`{|}", true, true, false, false);
        }
        String string = aVar.toString();
        try {
            return new URI(string);
        } catch (URISyntaxException e2) {
            try {
                return URI.create(string.replaceAll("[\\u0000-\\u001F\\u007F-\\u009F\\p{javaWhitespace}]", ""));
            } catch (Exception unused) {
                throw new RuntimeException(e2);
            }
        }
    }

    public String toString() {
        return this.f6448j;
    }
}
