package com.bytedance.sdk.component.ro.fm;

import defpackage.e;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import okhttp3.HttpUrl;

/* JADX INFO: loaded from: classes.dex */
public final class wsy {
    private static final char[] yz = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    private final String ajl;

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    private final String f6679ef;
    final String fm;
    private final String jnr;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    final int f6680lb;
    final String ro;
    private final String vt;
    private final List<String> wsy;
    private final List<String> wu;

    public static final class fm {
        final List<String> ajl;
        String fm;
        List<String> wsy;
        String wu;
        String yz;
        String ro = "";

        /* JADX INFO: renamed from: lb, reason: collision with root package name */
        String f6681lb = "";
        int jnr = -1;

        /* JADX INFO: renamed from: com.bytedance.sdk.component.ro.fm.wsy$fm$fm, reason: collision with other inner class name */
        public enum EnumC0039fm {
            SUCCESS,
            MISSING_SCHEME,
            UNSUPPORTED_SCHEME,
            INVALID_PORT,
            INVALID_HOST
        }

        public fm() {
            ArrayList arrayList = new ArrayList();
            this.ajl = arrayList;
            arrayList.add("");
        }

        private boolean ajl(String str) {
            return str.equals("..") || str.equalsIgnoreCase("%2e.") || str.equalsIgnoreCase(".%2e") || str.equalsIgnoreCase("%2e%2e");
        }

        private boolean jnr(String str) {
            return str.equals(".") || str.equalsIgnoreCase("%2e");
        }

        public fm fm(String str) {
            if (str == null) {
                throw new NullPointerException("scheme == null");
            }
            if (str.equalsIgnoreCase("http")) {
                this.fm = "http";
            } else {
                if (!str.equalsIgnoreCase("https")) {
                    throw new IllegalArgumentException("unexpected scheme: ".concat(str));
                }
                this.fm = "https";
            }
            return this;
        }

        public fm lb(String str) {
            if (str != null) {
                return fm(str, true);
            }
            throw new NullPointerException("encodedPathSegments == null");
        }

        public fm ro(String str) {
            if (str == null) {
                throw new NullPointerException("host == null");
            }
            String strJnr = jnr(str, 0, str.length());
            if (strJnr == null) {
                throw new IllegalArgumentException("unexpected host: ".concat(str));
            }
            this.yz = strJnr;
            return this;
        }

        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append(this.fm);
            sb.append("://");
            if (!this.ro.isEmpty() || !this.f6681lb.isEmpty()) {
                sb.append(this.ro);
                if (!this.f6681lb.isEmpty()) {
                    sb.append(':');
                    sb.append(this.f6681lb);
                }
                sb.append('@');
            }
            if (this.yz.indexOf(58) != -1) {
                sb.append('[');
                sb.append(this.yz);
                sb.append(']');
            } else {
                sb.append(this.yz);
            }
            int iFm = fm();
            if (iFm != wsy.fm(this.fm)) {
                sb.append(':');
                sb.append(iFm);
            }
            wsy.fm(sb, this.ajl);
            if (this.wsy != null) {
                sb.append('?');
                wsy.ro(sb, this.wsy);
            }
            if (this.wu != null) {
                sb.append('#');
                sb.append(this.wu);
            }
            return sb.toString();
        }

        public fm yz(String str) {
            this.wsy = str != null ? wsy.ro(wsy.fm(str, HttpUrl.QUERY_ENCODE_SET, true, false, true, true)) : null;
            return this;
        }

        private static String jnr(String str, int i10, int i11) {
            return com.bytedance.sdk.component.ro.fm.ro.ef.fm(wsy.fm(str, i10, i11, false));
        }

        private void lb() {
            if (this.ajl.remove(r0.size() - 1).isEmpty() && !this.ajl.isEmpty()) {
                this.ajl.set(r0.size() - 1, "");
            } else {
                this.ajl.add("");
            }
        }

        private static int yz(String str, int i10, int i11) {
            while (i10 < i11) {
                char cCharAt = str.charAt(i10);
                if (cCharAt == ':') {
                    return i10;
                }
                if (cCharAt == '[') {
                    do {
                        i10++;
                        if (i10 < i11) {
                        }
                    } while (str.charAt(i10) != ']');
                }
                i10++;
            }
            return i11;
        }

        public wsy ro() {
            if (this.fm == null) {
                throw new IllegalStateException("scheme == null");
            }
            if (this.yz != null) {
                return new wsy(this);
            }
            throw new IllegalStateException("host == null");
        }

        private static int ajl(String str, int i10, int i11) {
            int i12;
            try {
                i12 = Integer.parseInt(wsy.fm(str, i10, i11, "", false, false, false, true, null));
            } catch (NumberFormatException unused) {
            }
            if (i12 <= 0 || i12 > 65535) {
                return -1;
            }
            return i12;
        }

        public int fm() {
            int i10 = this.jnr;
            return i10 != -1 ? i10 : wsy.fm(this.fm);
        }

        private fm fm(String str, boolean z10) {
            int i10 = 0;
            do {
                int iFm = com.bytedance.sdk.component.ro.fm.ro.ef.fm(str, i10, str.length(), "/\\");
                fm(str, i10, iFm, iFm < str.length(), z10);
                i10 = iFm + 1;
            } while (i10 <= str.length());
            return this;
        }

        private static int lb(String str, int i10, int i11) {
            int i12 = 0;
            while (i10 < i11) {
                char cCharAt = str.charAt(i10);
                if (cCharAt != '\\' && cCharAt != '/') {
                    break;
                }
                i12++;
                i10++;
            }
            return i12;
        }

        private static int ro(String str, int i10, int i11) {
            if (i11 - i10 < 2) {
                return -1;
            }
            char cCharAt = str.charAt(i10);
            if ((cCharAt >= 'a' && cCharAt <= 'z') || (cCharAt >= 'A' && cCharAt <= 'Z')) {
                while (true) {
                    i10++;
                    if (i10 >= i11) {
                        break;
                    }
                    char cCharAt2 = str.charAt(i10);
                    if (cCharAt2 < 'a' || cCharAt2 > 'z') {
                        if (cCharAt2 < 'A' || cCharAt2 > 'Z') {
                            if (cCharAt2 < '0' || cCharAt2 > '9') {
                                if (cCharAt2 != '+' && cCharAt2 != '-' && cCharAt2 != '.') {
                                    if (cCharAt2 == ':') {
                                        return i10;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return -1;
        }

        public fm fm(String str, String str2) {
            if (str != null) {
                if (this.wsy == null) {
                    this.wsy = new ArrayList();
                }
                this.wsy.add(wsy.fm(str, HttpUrl.QUERY_COMPONENT_REENCODE_SET, true, false, true, true));
                this.wsy.add(str2 != null ? wsy.fm(str2, HttpUrl.QUERY_COMPONENT_REENCODE_SET, true, false, true, true) : null);
                return this;
            }
            throw new NullPointerException("encodedName == null");
        }

        public EnumC0039fm fm(wsy wsyVar, String str) {
            int iFm;
            int i10;
            int iFm2 = com.bytedance.sdk.component.ro.fm.ro.ef.fm(str, 0, str.length());
            int iRo = com.bytedance.sdk.component.ro.fm.ro.ef.ro(str, iFm2, str.length());
            if (ro(str, iFm2, iRo) != -1) {
                if (str.regionMatches(true, iFm2, "https:", 0, 6)) {
                    this.fm = "https";
                    iFm2 += 6;
                } else {
                    if (!str.regionMatches(true, iFm2, "http:", 0, 5)) {
                        return EnumC0039fm.UNSUPPORTED_SCHEME;
                    }
                    this.fm = "http";
                    iFm2 += 5;
                }
            } else if (wsyVar != null) {
                this.fm = wsyVar.fm;
            } else {
                return EnumC0039fm.MISSING_SCHEME;
            }
            int iLb = lb(str, iFm2, iRo);
            char c6 = '?';
            char c10 = '#';
            if (iLb < 2 && wsyVar != null && wsyVar.fm.equals(this.fm)) {
                this.ro = wsyVar.ro();
                this.f6681lb = wsyVar.lb();
                this.yz = wsyVar.ro;
                this.jnr = wsyVar.f6680lb;
                this.ajl.clear();
                this.ajl.addAll(wsyVar.yz());
                if (iFm2 == iRo || str.charAt(iFm2) == '#') {
                    yz(wsyVar.jnr());
                }
            } else {
                boolean z10 = false;
                boolean z11 = false;
                int i11 = iFm2 + iLb;
                while (true) {
                    iFm = com.bytedance.sdk.component.ro.fm.ro.ef.fm(str, i11, iRo, "@/\\?#");
                    byte bCharAt = iFm != iRo ? str.charAt(iFm) : (byte) -1;
                    if (bCharAt == -1 || bCharAt == c10 || bCharAt == 47 || bCharAt == 92 || bCharAt == c6) {
                        break;
                    }
                    if (bCharAt == 64) {
                        if (!z10) {
                            int iFm3 = com.bytedance.sdk.component.ro.fm.ro.ef.fm(str, i11, iFm, ':');
                            i10 = iFm;
                            String strFm = wsy.fm(str, i11, iFm3, " \"':;<=>@[]^`{}|/\\?#", true, false, false, true, null);
                            if (z11) {
                                strFm = e.p(new StringBuilder(), this.ro, "%40", strFm);
                            }
                            this.ro = strFm;
                            if (iFm3 != i10) {
                                this.f6681lb = wsy.fm(str, iFm3 + 1, i10, " \"':;<=>@[]^`{}|/\\?#", true, false, false, true, null);
                                z10 = true;
                            }
                            z11 = true;
                        } else {
                            i10 = iFm;
                            this.f6681lb += "%40" + wsy.fm(str, i11, i10, " \"':;<=>@[]^`{}|/\\?#", true, false, false, true, null);
                        }
                        i11 = i10 + 1;
                        c6 = '?';
                        c10 = '#';
                    }
                }
                int iYz = yz(str, i11, iFm);
                int i12 = iYz + 1;
                if (i12 < iFm) {
                    this.yz = jnr(str, i11, iYz);
                    int iAjl = ajl(str, i12, iFm);
                    this.jnr = iAjl;
                    if (iAjl == -1) {
                        return EnumC0039fm.INVALID_PORT;
                    }
                } else {
                    this.yz = jnr(str, i11, iYz);
                    this.jnr = wsy.fm(this.fm);
                }
                if (this.yz == null) {
                    return EnumC0039fm.INVALID_HOST;
                }
                iFm2 = iFm;
            }
            int iFm4 = com.bytedance.sdk.component.ro.fm.ro.ef.fm(str, iFm2, iRo, "?#");
            fm(str, iFm2, iFm4);
            if (iFm4 < iRo && str.charAt(iFm4) == '?') {
                int iFm5 = com.bytedance.sdk.component.ro.fm.ro.ef.fm(str, iFm4, iRo, '#');
                this.wsy = wsy.ro(wsy.fm(str, iFm4 + 1, iFm5, HttpUrl.QUERY_ENCODE_SET, true, false, true, true, null));
                iFm4 = iFm5;
            }
            if (iFm4 < iRo && str.charAt(iFm4) == '#') {
                this.wu = wsy.fm(str, 1 + iFm4, iRo, "", true, false, false, false, null);
            }
            return EnumC0039fm.SUCCESS;
        }

        private void fm(String str, int i10, int i11) {
            if (i10 == i11) {
                return;
            }
            char cCharAt = str.charAt(i10);
            if (cCharAt != '/' && cCharAt != '\\') {
                List<String> list = this.ajl;
                list.set(list.size() - 1, "");
            } else {
                this.ajl.clear();
                this.ajl.add("");
                i10++;
            }
            while (true) {
                int i12 = i10;
                if (i12 >= i11) {
                    return;
                }
                i10 = com.bytedance.sdk.component.ro.fm.ro.ef.fm(str, i12, i11, "/\\");
                boolean z10 = i10 < i11;
                fm(str, i12, i10, z10, true);
                if (z10) {
                    i10++;
                }
            }
        }

        private void fm(String str, int i10, int i11, boolean z10, boolean z11) {
            String strFm = wsy.fm(str, i10, i11, HttpUrl.PATH_SEGMENT_ENCODE_SET, z11, false, false, true, null);
            if (jnr(strFm)) {
                return;
            }
            if (ajl(strFm)) {
                lb();
                return;
            }
            if (((String) e.c(this.ajl, 1)).isEmpty()) {
                List<String> list = this.ajl;
                list.set(list.size() - 1, strFm);
            } else {
                this.ajl.add(strFm);
            }
            if (z10) {
                this.ajl.add("");
            }
        }
    }

    public wsy(fm fmVar) {
        this.fm = fmVar.fm;
        this.jnr = fm(fmVar.ro, false);
        this.ajl = fm(fmVar.f6681lb, false);
        this.ro = fmVar.yz;
        this.f6680lb = fmVar.fm();
        this.wsy = fm(fmVar.ajl, false);
        List<String> list = fmVar.wsy;
        this.wu = list != null ? fm(list, true) : null;
        String str = fmVar.wu;
        this.f6679ef = str != null ? fm(str, false) : null;
        this.vt = fmVar.toString();
    }

    public boolean equals(Object obj) {
        return (obj instanceof wsy) && ((wsy) obj).vt.equals(this.vt);
    }

    public URL fm() {
        try {
            return new URL(this.vt);
        } catch (MalformedURLException e10) {
            throw new RuntimeException(e10);
        }
    }

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

    public String jnr() {
        if (this.wu == null) {
            return null;
        }
        int iIndexOf = this.vt.indexOf(63) + 1;
        String str = this.vt;
        return this.vt.substring(iIndexOf, com.bytedance.sdk.component.ro.fm.ro.ef.fm(str, iIndexOf, str.length(), '#'));
    }

    public String lb() {
        if (this.ajl.isEmpty()) {
            return "";
        }
        return this.vt.substring(this.vt.indexOf(58, this.fm.length() + 3) + 1, this.vt.indexOf(64));
    }

    public String ro() {
        if (this.jnr.isEmpty()) {
            return "";
        }
        int length = this.fm.length() + 3;
        String str = this.vt;
        return this.vt.substring(length, com.bytedance.sdk.component.ro.fm.ro.ef.fm(str, length, str.length(), ":@"));
    }

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

    public List<String> yz() {
        int iIndexOf = this.vt.indexOf(47, this.fm.length() + 3);
        String str = this.vt;
        int iFm = com.bytedance.sdk.component.ro.fm.ro.ef.fm(str, iIndexOf, str.length(), "?#");
        ArrayList arrayList = new ArrayList();
        while (iIndexOf < iFm) {
            int i10 = iIndexOf + 1;
            int iFm2 = com.bytedance.sdk.component.ro.fm.ro.ef.fm(this.vt, i10, iFm, '/');
            arrayList.add(this.vt.substring(i10, iFm2));
            iIndexOf = iFm2;
        }
        return arrayList;
    }

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

    public static void fm(StringBuilder sb, List<String> list) {
        int size = list.size();
        for (int i10 = 0; i10 < size; i10++) {
            sb.append('/');
            sb.append(list.get(i10));
        }
    }

    public static wsy lb(String str) {
        fm fmVar = new fm();
        if (fmVar.fm((wsy) null, str) == fm.EnumC0039fm.SUCCESS) {
            return fmVar.ro();
        }
        return null;
    }

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

    public static String fm(String str, boolean z10) {
        return fm(str, 0, str.length(), z10);
    }

    private List<String> fm(List<String> list, boolean z10) {
        int size = list.size();
        ArrayList arrayList = new ArrayList(size);
        for (int i10 = 0; i10 < size; i10++) {
            String str = list.get(i10);
            arrayList.add(str != null ? fm(str, z10) : null);
        }
        return Collections.unmodifiableList(arrayList);
    }

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

    public static String fm(String str, int i10, int i11, boolean z10) {
        for (int i12 = i10; i12 < i11; i12++) {
            char cCharAt = str.charAt(i12);
            if (cCharAt == '%' || (cCharAt == '+' && z10)) {
                com.bytedance.sdk.component.ro.fm.ro.fm fmVar = new com.bytedance.sdk.component.ro.fm.ro.fm();
                fmVar.fm(str, i10, i12);
                fm(fmVar, str, i12, i11, z10);
                return fmVar.lb();
            }
        }
        return str.substring(i10, i11);
    }

    /* JADX WARN: Removed duplicated region for block: B:15:0x0039  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static void fm(com.bytedance.sdk.component.ro.fm.ro.fm fmVar, String str, int i10, int i11, boolean z10) {
        int i12;
        while (i10 < i11) {
            int iCodePointAt = str.codePointAt(i10);
            if (iCodePointAt == 37 && (i12 = i10 + 2) < i11) {
                int iFm = com.bytedance.sdk.component.ro.fm.ro.ef.fm(str.charAt(i10 + 1));
                int iFm2 = com.bytedance.sdk.component.ro.fm.ro.ef.fm(str.charAt(i12));
                if (iFm != -1 && iFm2 != -1) {
                    fmVar.ro((iFm << 4) + iFm2);
                    i10 = i12;
                }
            } else if (iCodePointAt == 43 && z10) {
                fmVar.ro(32);
            } else {
                fmVar.fm(iCodePointAt);
            }
            i10 += Character.charCount(iCodePointAt);
        }
    }

    public static boolean fm(String str, int i10, int i11) {
        int i12 = i10 + 2;
        return i12 < i11 && str.charAt(i10) == '%' && com.bytedance.sdk.component.ro.fm.ro.ef.fm(str.charAt(i10 + 1)) != -1 && com.bytedance.sdk.component.ro.fm.ro.ef.fm(str.charAt(i12)) != -1;
    }

    public static String fm(String str, int i10, int i11, String str2, boolean z10, boolean z11, boolean z12, boolean z13, Charset charset) {
        int iCharCount = i10;
        while (iCharCount < i11) {
            int iCodePointAt = str.codePointAt(iCharCount);
            if (iCodePointAt >= 32 && iCodePointAt != 127 && (iCodePointAt < 128 || !z13)) {
                if (str2.indexOf(iCodePointAt) == -1 && ((iCodePointAt != 37 || (z10 && (!z11 || fm(str, iCharCount, i11)))) && (iCodePointAt != 43 || !z12))) {
                    iCharCount += Character.charCount(iCodePointAt);
                } else {
                    com.bytedance.sdk.component.ro.fm.ro.fm fmVar = new com.bytedance.sdk.component.ro.fm.ro.fm();
                    fmVar.fm(str, i10, iCharCount);
                    fm(fmVar, str, iCharCount, i11, str2, z10, z11, z12, z13, charset);
                    return fmVar.lb();
                }
            } else {
                com.bytedance.sdk.component.ro.fm.ro.fm fmVar2 = new com.bytedance.sdk.component.ro.fm.ro.fm();
                fmVar2.fm(str, i10, iCharCount);
                fm(fmVar2, str, iCharCount, i11, str2, z10, z11, z12, z13, charset);
                return fmVar2.lb();
            }
        }
        return str.substring(i10, i11);
    }

    public static void fm(com.bytedance.sdk.component.ro.fm.ro.fm fmVar, String str, int i10, int i11, String str2, boolean z10, boolean z11, boolean z12, boolean z13, Charset charset) {
        com.bytedance.sdk.component.ro.fm.ro.fm fmVar2 = null;
        while (i10 < i11) {
            int iCodePointAt = str.codePointAt(i10);
            if (!z10 || (iCodePointAt != 9 && iCodePointAt != 10 && iCodePointAt != 12 && iCodePointAt != 13)) {
                if (iCodePointAt == 43 && z12) {
                    fmVar.fm(z10 ? "+" : "%2B");
                } else if (iCodePointAt >= 32 && iCodePointAt != 127 && ((iCodePointAt < 128 || !z13) && str2.indexOf(iCodePointAt) == -1 && (iCodePointAt != 37 || (z10 && (!z11 || fm(str, i10, i11)))))) {
                    fmVar.fm(iCodePointAt);
                } else {
                    if (fmVar2 == null) {
                        fmVar2 = new com.bytedance.sdk.component.ro.fm.ro.fm();
                    }
                    if (charset != null && !charset.equals(com.bytedance.sdk.component.ro.fm.ro.ef.fm)) {
                        fmVar2.fm(str, i10, Character.charCount(iCodePointAt) + i10, charset);
                    } else {
                        fmVar2.fm(iCodePointAt);
                    }
                    while (!fmVar2.fm()) {
                        byte bRo = fmVar2.ro();
                        fmVar.ro(37);
                        char[] cArr = yz;
                        fmVar.ro((int) cArr[((bRo & 255) >> 4) & 15]);
                        fmVar.ro((int) cArr[bRo & 15]);
                    }
                }
            }
            i10 += Character.charCount(iCodePointAt);
        }
    }

    public static String fm(String str, String str2, boolean z10, boolean z11, boolean z12, boolean z13) {
        return fm(str, 0, str.length(), str2, z10, z11, z12, z13, null);
    }
}
