package j2;

import B0.C0016c;
import X6.n;
import Z6.C0369i;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Looper;
import android.os.NetworkOnMainThreadException;
import android.webkit.MimeTypeMap;
import com.google.android.gms.internal.measurement.C1;
import d3.V0;
import g2.EnumC0977e;
import g2.o;
import h2.C1030c;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Map;
import k7.C;
import k7.C1111c;
import k7.E;
import k7.InterfaceC1112d;
import k7.p;
import k7.s;
import k7.w;
import k7.y;
import o2.C1336b;
import o2.C1337c;
import o2.C1338d;
import p2.C1423m;
import p2.EnumC1412b;
import x7.A;
import x7.B;
import x7.C1734j;
import x7.InterfaceC1733i;
import x7.x;

/* JADX INFO: loaded from: classes.dex */
public final class l implements g {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final C1111c f13692f = new C1111c(true, true, -1, -1, false, false, false, -1, -1, false, false, false, null);

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final C1111c f13693g = new C1111c(true, false, -1, -1, false, false, false, -1, -1, true, false, false, null);

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final A6.k f13696c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final A6.k f13697d;

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

    public l(String str, C1423m c1423m, A6.k kVar, A6.k kVar2, boolean z8) {
        this.f13694a = str;
        this.f13695b = c1423m;
        this.f13696c = kVar;
        this.f13697d = kVar2;
        this.f13698e = z8;
    }

    public static String d(String str, s sVar) {
        String strB;
        String str2 = sVar != null ? sVar.f14132a : null;
        if ((str2 == null || n.M(str2, "text/plain", false)) && (strB = u2.f.b(MimeTypeMap.getSingleton(), str)) != null) {
            return strB;
        }
        if (str2 == null) {
            return null;
        }
        int iS = X6.e.S(str2, ';', 0, 6);
        if (iS == -1) {
            return str2;
        }
        String strSubstring = str2.substring(0, iS);
        P6.g.d(strSubstring, "substring(...)");
        return strSubstring;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:103:0x0206 A[Catch: Exception -> 0x0203, TryCatch #4 {Exception -> 0x0203, blocks: (B:93:0x01d7, B:95:0x01dd, B:99:0x01ff, B:103:0x0206, B:104:0x020b), top: B:118:0x01d7 }] */
    /* JADX WARN: Removed duplicated region for block: B:34:0x009a  */
    /* JADX WARN: Removed duplicated region for block: B:36:0x009d A[Catch: Exception -> 0x00d5, TRY_ENTER, TryCatch #3 {Exception -> 0x00d5, blocks: (B:105:0x020c, B:106:0x020f, B:67:0x0157, B:107:0x0210, B:108:0x0215, B:36:0x009d, B:38:0x00a7, B:47:0x00d9, B:49:0x00dd, B:53:0x00f6, B:63:0x013e, B:55:0x010c, B:57:0x0118, B:58:0x0121, B:41:0x00bd, B:43:0x00c7, B:60:0x0129, B:61:0x0130, B:62:0x0131), top: B:117:0x002d }] */
    /* JADX WARN: Removed duplicated region for block: B:62:0x0131 A[Catch: Exception -> 0x00d5, TryCatch #3 {Exception -> 0x00d5, blocks: (B:105:0x020c, B:106:0x020f, B:67:0x0157, B:107:0x0210, B:108:0x0215, B:36:0x009d, B:38:0x00a7, B:47:0x00d9, B:49:0x00dd, B:53:0x00f6, B:63:0x013e, B:55:0x010c, B:57:0x0118, B:58:0x0121, B:41:0x00bd, B:43:0x00c7, B:60:0x0129, B:61:0x0130, B:62:0x0131), top: B:117:0x002d }] */
    /* JADX WARN: Removed duplicated region for block: B:65:0x0151  */
    /* JADX WARN: Removed duplicated region for block: B:66:0x0153  */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0017  */
    /* JADX WARN: Removed duplicated region for block: B:95:0x01dd A[Catch: Exception -> 0x0203, TryCatch #4 {Exception -> 0x0203, blocks: (B:93:0x01d7, B:95:0x01dd, B:99:0x01ff, B:103:0x0206, B:104:0x020b), top: B:118:0x01d7 }] */
    /* JADX WARN: Type inference failed for: r10v1 */
    /* JADX WARN: Type inference failed for: r10v11, types: [h2.h] */
    /* JADX WARN: Type inference failed for: r10v2, types: [java.io.Closeable] */
    /* JADX WARN: Type inference failed for: r11v4 */
    /* JADX WARN: Type inference failed for: r11v5, types: [j2.l] */
    /* JADX WARN: Type inference failed for: r11v7 */
    /* JADX WARN: Type inference failed for: r17v0, types: [j2.l] */
    /* JADX WARN: Type inference failed for: r2v10, types: [j2.l, java.lang.Object] */
    /* JADX WARN: Type inference failed for: r2v17 */
    /* JADX WARN: Type inference failed for: r2v7 */
    /* JADX WARN: Type inference failed for: r3v16, types: [A6.e, java.lang.Object] */
    /* JADX WARN: Type inference failed for: r3v4, types: [A6.e, java.lang.Object] */
    /* JADX WARN: Type inference failed for: r3v6, types: [A6.e, java.lang.Object] */
    /* JADX WARN: Type inference failed for: r4v0, types: [int] */
    /* JADX WARN: Type inference failed for: r4v10, types: [h2.h] */
    /* JADX WARN: Type inference failed for: r4v14 */
    /* JADX WARN: Type inference failed for: r4v16 */
    @Override // j2.g
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object a(E6.d dVar) throws Exception {
        k kVar;
        ?? r10;
        h2.h hVar;
        C1338d c1338dA;
        Object objB;
        C1338d c1338d;
        ?? r11;
        C1336b c1336b;
        h2.i iVar;
        ?? r42;
        C c3;
        ?? r22;
        C c8;
        E e8;
        if (dVar instanceof k) {
            kVar = (k) dVar;
            int i = kVar.f13691y;
            if ((i & Integer.MIN_VALUE) != 0) {
                kVar.f13691y = i - Integer.MIN_VALUE;
            } else {
                kVar = new k(this, (G6.c) dVar);
            }
        }
        Object objB2 = kVar.f13689w;
        F6.a aVar = F6.a.f2580q;
        ?? r43 = kVar.f13691y;
        EnumC0977e enumC0977e = EnumC0977e.f12900t;
        EnumC0977e enumC0977e2 = EnumC0977e.f12899s;
        try {
            if (r43 == 0) {
                C1.N(objB2);
                C1423m c1423m = this.f13695b;
                boolean z8 = c1423m.f16105n.f16029q;
                String str = this.f13694a;
                if (!z8 || (iVar = (h2.i) this.f13697d.getValue()) == null) {
                    hVar = null;
                    if (hVar == null) {
                        x7.m mVarC = c();
                        C1030c c1030c = hVar.f13391q;
                        if (c1030c.f13369r) {
                            throw new IllegalStateException("snapshot is closed");
                        }
                        Long l8 = (Long) mVarC.g((x) c1030c.f13368q.f13362c.get(0)).f12859e;
                        if (l8 != null && l8.longValue() == 0) {
                            return new m(g(hVar), d(str, null), enumC0977e2);
                        }
                        if (!this.f13698e) {
                            g2.l lVarG = g(hVar);
                            C1336b c1336bF = f(hVar);
                            return new m(lVarG, d(str, c1336bF != null ? (s) c1336bF.f15633b.getValue() : null), enumC0977e2);
                        }
                        c1338dA = new C1337c(e(), f(hVar)).a();
                        if (c1338dA.f15648a == null && (c1336b = c1338dA.f15649b) != null) {
                            return new m(g(hVar), d(str, (s) c1336b.f15633b.getValue()), enumC0977e2);
                        }
                    } else {
                        c1338dA = new C1337c(e(), null).a();
                    }
                    y yVar = c1338dA.f15648a;
                    P6.g.b(yVar);
                    kVar.f13686t = this;
                    kVar.f13687u = hVar;
                    kVar.f13688v = c1338dA;
                    kVar.f13691y = 1;
                    objB = b(yVar, kVar);
                    if (objB != aVar) {
                        return aVar;
                    }
                    c1338d = c1338dA;
                    objB2 = objB;
                    r11 = this;
                    r42 = hVar;
                } else {
                    String str2 = c1423m.i;
                    if (str2 == null) {
                        str2 = str;
                    }
                    C1734j c1734j = C1734j.f18766t;
                    C1030c c1030cG = iVar.f13393b.g(d3.E.h(str2).b("SHA-256").d());
                    if (c1030cG != null) {
                        hVar = new h2.h(c1030cG);
                    }
                    if (hVar == null) {
                    }
                    y yVar2 = c1338dA.f15648a;
                    P6.g.b(yVar2);
                    kVar.f13686t = this;
                    kVar.f13687u = hVar;
                    kVar.f13688v = c1338dA;
                    kVar.f13691y = 1;
                    objB = b(yVar2, kVar);
                    if (objB != aVar) {
                    }
                }
                u2.f.a(c3);
                throw e;
            }
            if (r43 == 1) {
                C1338d c1338d2 = (C1338d) kVar.f13688v;
                r10 = kVar.f13687u;
                l lVar = kVar.f13686t;
                try {
                    C1.N(objB2);
                    c1338d = c1338d2;
                    r42 = r10;
                    r11 = lVar;
                } catch (Exception e9) {
                    e = e9;
                    if (r10 != 0) {
                        u2.f.a(r10);
                    }
                    throw e;
                }
            } else {
                if (r43 != 2) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                c3 = (C) kVar.f13688v;
                h2.h hVar2 = kVar.f13687u;
                l lVar2 = kVar.f13686t;
                try {
                    C1.N(objB2);
                    r22 = lVar2;
                    c8 = (C) objB2;
                    try {
                        Bitmap.Config[] configArr = u2.f.f17441a;
                        e8 = c8.f14016w;
                        if (e8 != null) {
                            throw new IllegalStateException("response body == null");
                        }
                        r22.getClass();
                        InterfaceC1733i interfaceC1733iG = e8.g();
                        Context context = r22.f13695b.f16093a;
                        o oVar = new o(interfaceC1733iG, null);
                        String strD = d(r22.f13694a, e8.b());
                        if (c8.f14017x == null) {
                            enumC0977e = enumC0977e2;
                        }
                        return new m(oVar, strD, enumC0977e);
                    } catch (Exception e10) {
                        e = e10;
                        c3 = c8;
                    }
                } catch (Exception e11) {
                    e = e11;
                }
            }
            C c9 = (C) objB2;
            Bitmap.Config[] configArr2 = u2.f.f17441a;
            E e12 = c9.f14016w;
            if (e12 == null) {
                throw new IllegalStateException("response body == null");
            }
            try {
                h2.h hVarH = r11.h(r42, c1338d.f15648a, c9, c1338d.f15649b);
                String str3 = r11.f13694a;
                if (hVarH != null) {
                    g2.l lVarG2 = r11.g(hVarH);
                    C1336b c1336bF2 = r11.f(hVarH);
                    return new m(lVarG2, d(str3, c1336bF2 != null ? (s) c1336bF2.f15633b.getValue() : null), enumC0977e);
                }
                if (e12.g().C(1L)) {
                    InterfaceC1733i interfaceC1733iG2 = e12.g();
                    Context context2 = r11.f13695b.f16093a;
                    o oVar2 = new o(interfaceC1733iG2, null);
                    String strD2 = d(str3, e12.b());
                    if (c9.f14017x == null) {
                        enumC0977e = enumC0977e2;
                    }
                    return new m(oVar2, strD2, enumC0977e);
                }
                u2.f.a(c9);
                y yVarE = r11.e();
                kVar.f13686t = r11;
                kVar.f13687u = hVarH;
                kVar.f13688v = c9;
                kVar.f13691y = 2;
                objB2 = r11.b(yVarE, kVar);
                if (objB2 != aVar) {
                    r22 = r11;
                    c3 = c9;
                    c8 = (C) objB2;
                    Bitmap.Config[] configArr3 = u2.f.f17441a;
                    e8 = c8.f14016w;
                    if (e8 != null) {
                    }
                }
                return aVar;
            } catch (Exception e13) {
                e = e13;
                c3 = c9;
            }
        } catch (Exception e14) {
            e = e14;
            r10 = r43;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object b(y yVar, G6.c cVar) throws Throwable {
        j jVar;
        C cI;
        if (cVar instanceof j) {
            jVar = (j) cVar;
            int i = jVar.f13685v;
            if ((i & Integer.MIN_VALUE) != 0) {
                jVar.f13685v = i - Integer.MIN_VALUE;
            } else {
                jVar = new j(this, cVar);
            }
        }
        Object objU = jVar.f13683t;
        F6.a aVar = F6.a.f2580q;
        int i5 = jVar.f13685v;
        if (i5 == 0) {
            C1.N(objU);
            Bitmap.Config[] configArr = u2.f.f17441a;
            if (P6.g.a(Looper.myLooper(), Looper.getMainLooper())) {
                if (this.f13695b.f16106o.f16029q) {
                    throw new NetworkOnMainThreadException();
                }
                w wVar = (w) ((InterfaceC1112d) this.f13696c.getValue());
                wVar.getClass();
                P6.g.e(yVar, "request");
                o7.i iVar = new o7.i(wVar, yVar);
                if (!iVar.f15973u.compareAndSet(false, true)) {
                    throw new IllegalStateException("Already Executed");
                }
                iVar.f15972t.h();
                s7.n nVar = s7.n.f16942a;
                iVar.f15974v = s7.n.f16942a.g();
                try {
                    C2.a aVar2 = wVar.f14184q;
                    synchronized (aVar2) {
                        ((ArrayDeque) aVar2.f1160u).add(iVar);
                    }
                    cI = iVar.i();
                    C2.a aVar3 = wVar.f14184q;
                    aVar3.o((ArrayDeque) aVar3.f1160u, iVar);
                    if (!cI.b() || cI.f14013t == 304) {
                        return cI;
                    }
                    E e8 = cI.f14016w;
                    if (e8 != null) {
                        u2.f.a(e8);
                    }
                    throw new A6.b("HTTP " + cI.f14013t + ": " + cI.f14012s);
                } catch (Throwable th) {
                    C2.a aVar4 = iVar.f15969q.f14184q;
                    aVar4.o((ArrayDeque) aVar4.f1160u, iVar);
                    throw th;
                }
            }
            w wVar2 = (w) ((InterfaceC1112d) this.f13696c.getValue());
            wVar2.getClass();
            P6.g.e(yVar, "request");
            o7.i iVar2 = new o7.i(wVar2, yVar);
            jVar.f13685v = 1;
            C0369i c0369i = new C0369i(1, C1.A(jVar));
            c0369i.v();
            u2.g gVar = new u2.g(iVar2, c0369i);
            iVar2.f(gVar);
            c0369i.x(gVar);
            objU = c0369i.u();
            if (objU == aVar) {
                return aVar;
            }
        } else {
            if (i5 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            C1.N(objU);
        }
        cI = (C) objU;
        if (cI.b()) {
        }
        return cI;
    }

    public final x7.m c() {
        Object value = this.f13697d.getValue();
        P6.g.b(value);
        return ((h2.i) value).f13392a;
    }

    public final y e() {
        K2.e eVar = new K2.e();
        String strConcat = this.f13694a;
        P6.g.e(strConcat, "url");
        if (n.M(strConcat, "ws:", true)) {
            String strSubstring = strConcat.substring(3);
            P6.g.d(strSubstring, "this as java.lang.String).substring(startIndex)");
            strConcat = "http:".concat(strSubstring);
        } else if (n.M(strConcat, "wss:", true)) {
            String strSubstring2 = strConcat.substring(4);
            P6.g.d(strSubstring2, "this as java.lang.String).substring(startIndex)");
            strConcat = "https:".concat(strSubstring2);
        }
        P6.g.e(strConcat, "<this>");
        p pVar = new p();
        pVar.c(null, strConcat);
        eVar.f3552q = pVar.a();
        C1423m c1423m = this.f13695b;
        k7.o oVar = c1423m.f16101j;
        P6.g.e(oVar, "headers");
        eVar.f3554s = oVar.f();
        for (Map.Entry entry : c1423m.f16102k.f16117a.entrySet()) {
            Object key = entry.getKey();
            P6.g.c(key, "null cannot be cast to non-null type java.lang.Class<kotlin.Any>");
            eVar.T((Class) key, entry.getValue());
        }
        EnumC1412b enumC1412b = c1423m.f16105n;
        boolean z8 = enumC1412b.f16029q;
        boolean z9 = c1423m.f16106o.f16029q;
        if (!z9 && z8) {
            eVar.r(C1111c.f14045o);
        } else if (!z9 || z8) {
            if (!z9 && !z8) {
                eVar.r(f13693g);
            }
        } else if (enumC1412b.f16030r) {
            eVar.r(C1111c.f14044n);
        } else {
            eVar.r(f13692f);
        }
        return eVar.q();
    }

    public final C1336b f(h2.h hVar) throws Throwable {
        Throwable th;
        C1336b c1336b;
        try {
            x7.m mVarC = c();
            C1030c c1030c = hVar.f13391q;
            if (c1030c.f13369r) {
                throw new IllegalStateException("snapshot is closed");
            }
            B bC = s7.l.c(mVarC.k((x) c1030c.f13368q.f13362c.get(0)));
            try {
                c1336b = new C1336b(bC);
                try {
                    bC.close();
                    th = null;
                } catch (Throwable th2) {
                    th = th2;
                }
            } catch (Throwable th3) {
                try {
                    bC.close();
                } catch (Throwable th4) {
                    android.support.v4.media.session.a.a(th3, th4);
                }
                th = th3;
                c1336b = null;
            }
            if (th == null) {
                return c1336b;
            }
            throw th;
        } catch (IOException unused) {
            return null;
        }
    }

    public final g2.l g(h2.h hVar) {
        C1030c c1030c = hVar.f13391q;
        if (c1030c.f13369r) {
            throw new IllegalStateException("snapshot is closed");
        }
        x xVar = (x) c1030c.f13368q.f13362c.get(1);
        x7.m mVarC = c();
        String str = this.f13695b.i;
        if (str == null) {
            str = this.f13694a;
        }
        return new g2.l(xVar, mVarC, str, hVar);
    }

    /* JADX WARN: Removed duplicated region for block: B:33:0x0087  */
    /* JADX WARN: Removed duplicated region for block: B:36:0x008c  */
    /* JADX WARN: Removed duplicated region for block: B:98:0x0162  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final h2.h h(h2.h hVar, y yVar, C c3, C1336b c1336b) {
        V0 v02;
        Throwable th;
        C0016c c0016cB;
        int i = 18;
        Throwable th2 = null;
        if (this.f13695b.f16105n.f16030r) {
            if (this.f13698e) {
                if (!yVar.a().f14047b) {
                    C1111c c1111cV = c3.D;
                    if (c1111cV == null) {
                        C1111c c1111c = C1111c.f14044n;
                        c1111cV = s7.l.v(c3.f14015v);
                        c3.D = c1111cV;
                    }
                    if (c1111cV.f14047b || P6.g.a(c3.f14015v.a("Vary"), "*")) {
                    }
                }
                if (hVar != null) {
                }
            }
            if (hVar != null) {
                C1030c c1030c = hVar.f13391q;
                h2.f fVar = c1030c.f13370s;
                synchronized (fVar) {
                    c1030c.close();
                    c0016cB = fVar.b(c1030c.f13368q.f13360a);
                }
                v02 = c0016cB != null ? new V0(i, c0016cB) : null;
                if (v02 != null) {
                    try {
                        try {
                            if (c3.f14013t != 304 || c1336b == null) {
                                A aB = s7.l.b(c().j(((C0016c) v02.f11784r).c(0)));
                                try {
                                    new C1336b(c3).a(aB);
                                    try {
                                        aB.close();
                                        th = null;
                                    } catch (Throwable th3) {
                                        th = th3;
                                    }
                                } catch (Throwable th4) {
                                    try {
                                        aB.close();
                                    } catch (Throwable th5) {
                                        android.support.v4.media.session.a.a(th4, th5);
                                    }
                                    th = th4;
                                }
                                if (th != null) {
                                    throw th;
                                }
                                A aB2 = s7.l.b(c().j(((C0016c) v02.f11784r).c(1)));
                                try {
                                    E e8 = c3.f14016w;
                                    P6.g.b(e8);
                                    e8.g().L(aB2);
                                    try {
                                        aB2.close();
                                    } catch (Throwable th6) {
                                        th2 = th6;
                                    }
                                } catch (Throwable th7) {
                                    th2 = th7;
                                    try {
                                        aB2.close();
                                    } catch (Throwable th8) {
                                        android.support.v4.media.session.a.a(th2, th8);
                                    }
                                }
                                if (th2 != null) {
                                    throw th2;
                                }
                            } else {
                                k7.B bG = c3.g();
                                bG.f14000f = U2.a.e(c1336b.f15637f, c3.f14015v).f();
                                C cA = bG.a();
                                A aB3 = s7.l.b(c().j(((C0016c) v02.f11784r).c(0)));
                                try {
                                    new C1336b(cA).a(aB3);
                                    try {
                                        aB3.close();
                                    } catch (Throwable th9) {
                                        th2 = th9;
                                    }
                                } catch (Throwable th10) {
                                    th2 = th10;
                                    try {
                                        aB3.close();
                                    } catch (Throwable th11) {
                                        android.support.v4.media.session.a.a(th2, th11);
                                    }
                                }
                                if (th2 != null) {
                                    throw th2;
                                }
                            }
                            h2.h hVarM = v02.m();
                            u2.f.a(c3);
                            return hVarM;
                        } catch (Exception e9) {
                            Bitmap.Config[] configArr = u2.f.f17441a;
                            try {
                                ((C0016c) v02.f11784r).a(false);
                            } catch (Exception unused) {
                            }
                            throw e9;
                        }
                    } catch (Throwable th12) {
                        u2.f.a(c3);
                        throw th12;
                    }
                }
            } else {
                h2.i iVar = (h2.i) this.f13697d.getValue();
                if (iVar != null) {
                    String str = this.f13695b.i;
                    if (str == null) {
                        str = this.f13694a;
                    }
                    h2.f fVar2 = iVar.f13393b;
                    C1734j c1734j = C1734j.f18766t;
                    C0016c c0016cB2 = fVar2.b(d3.E.h(str).b("SHA-256").d());
                    if (c0016cB2 != null) {
                        v02 = new V0(i, c0016cB2);
                    }
                    if (v02 != null) {
                    }
                }
            }
        } else if (hVar != null) {
            u2.f.a(hVar);
        }
        return null;
    }
}
