package k7;

import com.google.android.gms.internal.measurement.C1;
import d5.AbstractC0844a;
import java.util.ArrayList;

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

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

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public ArrayList f14118g;

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

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

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

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

    public p() {
        ArrayList arrayList = new ArrayList();
        this.f14117f = arrayList;
        arrayList.add("");
    }

    public final q a() {
        ArrayList arrayList;
        String str = this.f14112a;
        if (str == null) {
            throw new IllegalStateException("scheme == null");
        }
        String strF = l.f(this.f14113b, 0, 0, 7);
        String strF2 = l.f(this.f14114c, 0, 0, 7);
        String str2 = this.f14115d;
        if (str2 == null) {
            throw new IllegalStateException("host == null");
        }
        int iB = b();
        ArrayList arrayList2 = this.f14117f;
        ArrayList arrayList3 = new ArrayList(B6.m.a0(arrayList2));
        int size = arrayList2.size();
        int i = 0;
        while (i < size) {
            Object obj = arrayList2.get(i);
            i++;
            arrayList3.add(l.f((String) obj, 0, 0, 7));
        }
        ArrayList arrayList4 = this.f14118g;
        if (arrayList4 != null) {
            ArrayList arrayList5 = new ArrayList(B6.m.a0(arrayList4));
            int size2 = arrayList4.size();
            int i5 = 0;
            while (i5 < size2) {
                Object obj2 = arrayList4.get(i5);
                i5++;
                String str3 = (String) obj2;
                arrayList5.add(str3 != null ? l.f(str3, 0, 0, 3) : null);
            }
            arrayList = arrayList5;
        } else {
            arrayList = null;
        }
        String str4 = this.f14119h;
        return new q(str, strF, strF2, str2, iB, arrayList3, arrayList, str4 != null ? l.f(str4, 0, 0, 7) : null, toString());
    }

    public final int b() {
        int i = this.f14116e;
        if (i != -1) {
            return i;
        }
        String str = this.f14112a;
        P6.g.b(str);
        if (str.equals("http")) {
            return 80;
        }
        return str.equals("https") ? 443 : -1;
    }

    /* JADX WARN: Removed duplicated region for block: B:39:0x007f  */
    /* JADX WARN: Removed duplicated region for block: B:75:0x013a  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void c(q qVar, String str) {
        int i;
        int iG;
        int i5;
        char cCharAt;
        P6.g.e(str, "input");
        byte[] bArr = l7.b.f14412a;
        int iN = l7.b.n(0, str.length(), str);
        int iO = l7.b.o(iN, str.length(), str);
        if (iO - iN < 2) {
            i = -1;
        } else {
            char cCharAt2 = str.charAt(iN);
            char c3 = 'a';
            if ((P6.g.f(cCharAt2, 97) >= 0 && P6.g.f(cCharAt2, 122) <= 0) || (P6.g.f(cCharAt2, 65) >= 0 && P6.g.f(cCharAt2, 90) <= 0)) {
                i = iN + 1;
                while (true) {
                    if (i >= iO) {
                        break;
                    }
                    char cCharAt3 = str.charAt(i);
                    if ((c3 > cCharAt3 || cCharAt3 >= '{') && (('A' > cCharAt3 || cCharAt3 >= '[') && (('0' > cCharAt3 || cCharAt3 >= ':') && cCharAt3 != '+' && cCharAt3 != '-' && cCharAt3 != '.'))) {
                        if (cCharAt3 != ':') {
                            break;
                        }
                    } else {
                        i++;
                        c3 = 'a';
                    }
                }
                i = -1;
            }
        }
        if (i == -1) {
            if (qVar == null) {
                throw new IllegalArgumentException(AbstractC0844a.x("Expected URL scheme 'http' or 'https' but no scheme was found for ", str.length() > 6 ? X6.e.i0(6, str).concat("...") : str));
            }
            this.f14112a = qVar.f14121a;
        } else if (X6.n.L(str, "https:", iN, true)) {
            this.f14112a = "https";
            iN += 6;
        } else {
            if (!X6.n.L(str, "http:", iN, true)) {
                StringBuilder sb = new StringBuilder("Expected URL scheme 'http' or 'https' but was '");
                String strSubstring = str.substring(0, i);
                P6.g.d(strSubstring, "this as java.lang.String…ing(startIndex, endIndex)");
                sb.append(strSubstring);
                sb.append('\'');
                throw new IllegalArgumentException(sb.toString());
            }
            this.f14112a = "http";
            iN += 5;
        }
        int i8 = 0;
        for (int i9 = iN; i9 < iO && ((cCharAt = str.charAt(i9)) == '\\' || cCharAt == '/'); i9++) {
            i8++;
        }
        ArrayList arrayList = this.f14117f;
        char c8 = '#';
        if (i8 >= 2 || qVar == null) {
            int i10 = iN + i8;
            boolean z8 = false;
            boolean z9 = false;
            while (true) {
                iG = l7.b.g(str, i10, iO, "@/\\?#");
                byte bCharAt = iG != iO ? str.charAt(iG) : (byte) -1;
                if (bCharAt == -1 || bCharAt == c8 || bCharAt == 47 || bCharAt == 92 || bCharAt == 63) {
                    break;
                }
                if (bCharAt == 64) {
                    if (z8) {
                        this.f14114c += "%40" + l.b(str, i10, iG, " \"':;<=>@[]^`{}|/\\?#", 240);
                        z8 = z8;
                    } else {
                        boolean z10 = z8;
                        int iF = l7.b.f(str, ':', i10, iG);
                        String strB = l.b(str, i10, iF, " \"':;<=>@[]^`{}|/\\?#", 240);
                        if (z9) {
                            strB = this.f14113b + "%40" + strB;
                        }
                        this.f14113b = strB;
                        if (iF != iG) {
                            this.f14114c = l.b(str, iF + 1, iG, " \"':;<=>@[]^`{}|/\\?#", 240);
                            z8 = true;
                        } else {
                            z8 = z10;
                        }
                        z9 = true;
                    }
                    i10 = iG + 1;
                    c8 = '#';
                }
            }
            int i11 = i10;
            while (true) {
                if (i11 >= iG) {
                    i11 = iG;
                    break;
                }
                char cCharAt4 = str.charAt(i11);
                if (cCharAt4 != '[') {
                    if (cCharAt4 == ':') {
                        break;
                    }
                } else {
                    do {
                        i11++;
                        if (i11 >= iG) {
                            break;
                        }
                    } while (str.charAt(i11) != ']');
                }
                i11++;
            }
            int i12 = i11 + 1;
            if (i12 < iG) {
                this.f14115d = C1.O(l.f(str, i10, i11, 4));
                try {
                    i5 = Integer.parseInt(l.b(str, i12, iG, "", 248));
                } catch (NumberFormatException unused) {
                }
                if (1 > i5 || i5 >= 65536) {
                    i5 = -1;
                }
                this.f14116e = i5;
                if (i5 == -1) {
                    StringBuilder sb2 = new StringBuilder("Invalid URL port: \"");
                    String strSubstring2 = str.substring(i12, iG);
                    P6.g.d(strSubstring2, "this as java.lang.String…ing(startIndex, endIndex)");
                    sb2.append(strSubstring2);
                    sb2.append('\"');
                    throw new IllegalArgumentException(sb2.toString().toString());
                }
            } else {
                this.f14115d = C1.O(l.f(str, i10, i11, 4));
                String str2 = this.f14112a;
                P6.g.b(str2);
                this.f14116e = str2.equals("http") ? 80 : str2.equals("https") ? 443 : -1;
            }
            if (this.f14115d == null) {
                StringBuilder sb3 = new StringBuilder("Invalid URL host: \"");
                String strSubstring3 = str.substring(i10, i11);
                P6.g.d(strSubstring3, "this as java.lang.String…ing(startIndex, endIndex)");
                sb3.append(strSubstring3);
                sb3.append('\"');
                throw new IllegalArgumentException(sb3.toString().toString());
            }
            iN = iG;
        } else if (P6.g.a(qVar.f14121a, this.f14112a)) {
            this.f14113b = qVar.e();
            this.f14114c = qVar.a();
            this.f14115d = qVar.f14124d;
            this.f14116e = qVar.f14125e;
            arrayList.clear();
            arrayList.addAll(qVar.c());
            if (iN == iO || str.charAt(iN) == '#') {
                String strD = qVar.d();
                this.f14118g = strD != null ? l.g(l.b(strD, 0, 0, " \"'<>#", 211)) : null;
            }
        }
        int iG2 = l7.b.g(str, iN, iO, "?#");
        if (iN != iG2) {
            char cCharAt5 = str.charAt(iN);
            if (cCharAt5 == '/' || cCharAt5 == '\\') {
                arrayList.clear();
                arrayList.add("");
                iN++;
            } else {
                arrayList.set(arrayList.size() - 1, "");
            }
            while (iN < iG2) {
                int iG3 = l7.b.g(str, iN, iG2, "/\\");
                boolean z11 = iG3 < iG2;
                String strB2 = l.b(str, iN, iG3, " \"<>^`{}|/\\?#", 240);
                if (!strB2.equals(".") && !strB2.equalsIgnoreCase("%2e")) {
                    if (!strB2.equals("..") && !strB2.equalsIgnoreCase("%2e.") && !strB2.equalsIgnoreCase(".%2e") && !strB2.equalsIgnoreCase("%2e%2e")) {
                        if (((CharSequence) arrayList.get(arrayList.size() - 1)).length() == 0) {
                            arrayList.set(arrayList.size() - 1, strB2);
                        } else {
                            arrayList.add(strB2);
                        }
                        if (z11) {
                            arrayList.add("");
                        }
                    } else if (((String) arrayList.remove(arrayList.size() - 1)).length() != 0 || arrayList.isEmpty()) {
                        arrayList.add("");
                    } else {
                        arrayList.set(arrayList.size() - 1, "");
                    }
                }
                iN = z11 ? iG3 + 1 : iG3;
            }
        }
        if (iG2 < iO && str.charAt(iG2) == '?') {
            int iF2 = l7.b.f(str, '#', iG2, iO);
            this.f14118g = l.g(l.b(str, iG2 + 1, iF2, " \"'<>#", 208));
            iG2 = iF2;
        }
        if (iG2 >= iO || str.charAt(iG2) != '#') {
            return;
        }
        this.f14119h = l.b(str, iG2 + 1, iO, "", 176);
    }

    /* JADX WARN: Removed duplicated region for block: B:34:0x008b  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final String toString() {
        StringBuilder sb = new StringBuilder();
        String str = this.f14112a;
        if (str != null) {
            sb.append(str);
            sb.append("://");
        } else {
            sb.append("//");
        }
        if (this.f14113b.length() > 0 || this.f14114c.length() > 0) {
            sb.append(this.f14113b);
            if (this.f14114c.length() > 0) {
                sb.append(':');
                sb.append(this.f14114c);
            }
            sb.append('@');
        }
        String str2 = this.f14115d;
        if (str2 != null) {
            if (X6.e.N(str2, ':')) {
                sb.append('[');
                sb.append(this.f14115d);
                sb.append(']');
            } else {
                sb.append(this.f14115d);
            }
        }
        int i = -1;
        if (this.f14116e != -1 || this.f14112a != null) {
            int iB = b();
            String str3 = this.f14112a;
            if (str3 == null) {
                sb.append(':');
                sb.append(iB);
            } else {
                if (str3.equals("http")) {
                    i = 80;
                } else if (str3.equals("https")) {
                    i = 443;
                }
                if (iB != i) {
                }
            }
        }
        ArrayList arrayList = this.f14117f;
        P6.g.e(arrayList, "<this>");
        int size = arrayList.size();
        for (int i5 = 0; i5 < size; i5++) {
            sb.append('/');
            sb.append((String) arrayList.get(i5));
        }
        if (this.f14118g != null) {
            sb.append('?');
            ArrayList arrayList2 = this.f14118g;
            P6.g.b(arrayList2);
            l.h(arrayList2, sb);
        }
        if (this.f14119h != null) {
            sb.append('#');
            sb.append(this.f14119h);
        }
        String string = sb.toString();
        P6.g.d(string, "StringBuilder().apply(builderAction).toString()");
        return string;
    }
}
