package P4;

import K4.l;
import K4.m;
import K4.o;
import K4.p;
import K4.r;
import K4.t;
import K4.u;
import K4.v;
import K4.w;
import L1.Y;
import O4.h;
import R4.C0182a;
import X4.k;
import e4.C0574r;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import javax.net.ssl.SSLSocketFactory;
import r4.AbstractC0983h;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ int f2548a = 0;

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

    public a(K4.b bVar) {
        AbstractC0983h.e("cookieJar", bVar);
        this.f2549b = bVar;
    }

    public static int d(u uVar, int i) {
        String strA = u.a(uVar, "Retry-After");
        if (strA == null) {
            return i;
        }
        Pattern patternCompile = Pattern.compile("\\d+");
        AbstractC0983h.d("compile(...)", patternCompile);
        if (!patternCompile.matcher(strA).matches()) {
            return Integer.MAX_VALUE;
        }
        Integer numValueOf = Integer.valueOf(strA);
        AbstractC0983h.d("valueOf(header)", numValueOf);
        return numValueOf.intValue();
    }

    @Override // K4.p
    public final u a(f fVar) {
        w wVar;
        SSLSocketFactory sSLSocketFactory;
        W4.c cVar;
        K4.e eVar;
        switch (this.f2548a) {
            case 0:
                F3.e eVar2 = fVar.f2555e;
                com.google.firebase.messaging.w wVarJ = eVar2.j();
                m mVar = (m) eVar2.f716n;
                String strA = mVar.a("Host");
                boolean z5 = false;
                o oVar = (o) eVar2.f714l;
                if (strA == null) {
                    wVarJ.z("Host", L4.b.t(oVar, false));
                }
                if (mVar.a("Connection") == null) {
                    wVarJ.z("Connection", "Keep-Alive");
                }
                if (mVar.a("Accept-Encoding") == null && mVar.a("Range") == null) {
                    wVarJ.z("Accept-Encoding", "gzip");
                    z5 = true;
                }
                K4.b bVar = (K4.b) this.f2549b;
                bVar.getClass();
                AbstractC0983h.e("url", oVar);
                if (mVar.a("User-Agent") == null) {
                    wVarJ.z("User-Agent", "okhttp/4.11.0");
                }
                u uVarB = fVar.b(wVarJ.g());
                m mVar2 = uVarB.p;
                e.b(bVar, oVar, mVar2);
                t tVarE = uVarB.e();
                tVarE.f1211a = eVar2;
                if (z5 && "gzip".equalsIgnoreCase(u.a(uVarB, "Content-Encoding")) && e.a(uVarB) && (wVar = uVarB.q) != null) {
                    k kVar = new k(wVar.e());
                    l lVarC = mVar2.c();
                    lVarC.h("Content-Encoding");
                    lVarC.h("Content-Length");
                    tVarE.f1216f = lVarC.b().c();
                    u.a(uVarB, "Content-Type");
                    tVarE.f1217g = new v(-1L, new X4.o(kVar), 1);
                }
                return tVarE.a();
            default:
                F3.e eVar3 = fVar.f2555e;
                h hVar = fVar.f2551a;
                List list = C0574r.f6259k;
                u uVar = null;
                int i = 0;
                F3.e eVarB = eVar3;
                while (true) {
                    boolean z6 = true;
                    while (true) {
                        hVar.getClass();
                        if (hVar.f2343s != null) {
                            throw new IllegalStateException("Check failed.");
                        }
                        synchronized (hVar) {
                            try {
                                if (hVar.f2345u) {
                                    throw new IllegalStateException("cannot make a new request because the previous response is still open: please call response.close()");
                                }
                                if (hVar.f2344t) {
                                    throw new IllegalStateException("Check failed.");
                                }
                            } catch (Throwable th) {
                                throw th;
                            }
                        }
                        if (z6) {
                            Y y5 = hVar.f2339m;
                            o oVar2 = (o) eVarB.f714l;
                            boolean z7 = oVar2.i;
                            r rVar = hVar.f2337k;
                            if (z7) {
                                SSLSocketFactory sSLSocketFactory2 = rVar.f1203y;
                                if (sSLSocketFactory2 == null) {
                                    throw new IllegalStateException("CLEARTEXT-only client");
                                }
                                W4.c cVar2 = rVar.f1185C;
                                eVar = rVar.f1186D;
                                sSLSocketFactory = sSLSocketFactory2;
                                cVar = cVar2;
                            } else {
                                sSLSocketFactory = null;
                                cVar = null;
                                eVar = null;
                            }
                            hVar.q = new O4.d(y5, new K4.a(oVar2.f1161d, oVar2.f1162e, rVar.f1199u, rVar.f1202x, sSLSocketFactory, cVar, eVar, rVar.f1201w, rVar.f1184B, rVar.f1183A, rVar.f1200v), hVar);
                        }
                        try {
                            if (hVar.f2347w) {
                                throw new IOException("Canceled");
                            }
                            try {
                                try {
                                    u uVarB2 = fVar.b(eVarB);
                                    if (uVar != null) {
                                        t tVarE2 = uVarB2.e();
                                        t tVarE3 = uVar.e();
                                        tVarE3.f1217g = null;
                                        u uVarA = tVarE3.a();
                                        if (uVarA.q != null) {
                                            throw new IllegalArgumentException("priorResponse.body != null");
                                        }
                                        tVarE2.f1219j = uVarA;
                                        uVarB2 = tVarE2.a();
                                    }
                                    uVar = uVarB2;
                                    eVarB = b(uVar, hVar.f2343s);
                                    if (eVarB == null) {
                                        hVar.f(false);
                                        return uVar;
                                    }
                                    w wVar2 = uVar.q;
                                    if (wVar2 != null) {
                                        L4.b.b(wVar2);
                                    }
                                    i++;
                                    if (i > 20) {
                                        throw new ProtocolException(AbstractC0983h.h("Too many follow-up requests: ", Integer.valueOf(i)));
                                    }
                                    hVar.f(true);
                                } catch (O4.k e2) {
                                    if (!c(e2.f2365l, hVar, eVarB, false)) {
                                        IOException iOException = e2.f2364k;
                                        L4.b.x(iOException, list);
                                        throw iOException;
                                    }
                                    IOException iOException2 = e2.f2364k;
                                    ArrayList arrayList = new ArrayList(list.size() + 1);
                                    arrayList.addAll(list);
                                    arrayList.add(iOException2);
                                    hVar.f(true);
                                    list = arrayList;
                                    z6 = false;
                                }
                            } catch (IOException e6) {
                                if (!c(e6, hVar, eVarB, !(e6 instanceof C0182a))) {
                                    L4.b.x(e6, list);
                                    throw e6;
                                }
                                ArrayList arrayList2 = new ArrayList(list.size() + 1);
                                arrayList2.addAll(list);
                                arrayList2.add(e6);
                                hVar.f(true);
                                list = arrayList2;
                                z6 = false;
                            }
                        } catch (Throwable th2) {
                            hVar.f(true);
                            throw th2;
                        }
                    }
                }
                break;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:67:0x00c5  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public F3.e b(K4.u r12, O4.c r13) throws java.net.ProtocolException {
        /*
            Method dump skipped, instruction units count: 376
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: P4.a.b(K4.u, O4.c):F3.e");
    }

    /* JADX WARN: Removed duplicated region for block: B:33:0x0049  */
    /* JADX WARN: Removed duplicated region for block: B:36:0x004e  */
    /* JADX WARN: Removed duplicated region for block: B:63:0x0085  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public boolean c(java.io.IOException r4, O4.h r5, F3.e r6, boolean r7) {
        /*
            r3 = this;
            java.lang.Object r6 = r3.f2549b
            K4.r r6 = (K4.r) r6
            boolean r6 = r6.p
            r0 = 0
            if (r6 != 0) goto Lb
            goto L9e
        Lb:
            if (r7 == 0) goto L12
            boolean r6 = r4 instanceof java.io.FileNotFoundException
            if (r6 == 0) goto L12
            return r0
        L12:
            boolean r6 = r4 instanceof java.net.ProtocolException
            if (r6 == 0) goto L17
            return r0
        L17:
            boolean r6 = r4 instanceof java.io.InterruptedIOException
            if (r6 == 0) goto L22
            boolean r4 = r4 instanceof java.net.SocketTimeoutException
            if (r4 == 0) goto L9e
            if (r7 != 0) goto L9e
            goto L35
        L22:
            boolean r6 = r4 instanceof javax.net.ssl.SSLHandshakeException
            if (r6 == 0) goto L30
            java.lang.Throwable r6 = r4.getCause()
            boolean r6 = r6 instanceof java.security.cert.CertificateException
            if (r6 == 0) goto L30
            goto L9e
        L30:
            boolean r4 = r4 instanceof javax.net.ssl.SSLPeerUnverifiedException
            if (r4 == 0) goto L35
            return r0
        L35:
            O4.d r4 = r5.q
            r4.AbstractC0983h.b(r4)
            int r5 = r4.f2329f
            r6 = 1
            if (r5 != 0) goto L49
            int r7 = r4.f2330g
            if (r7 != 0) goto L49
            int r7 = r4.f2331h
            if (r7 != 0) goto L49
            r4 = r0
            goto L9c
        L49:
            K4.x r7 = r4.i
            if (r7 == 0) goto L4e
            goto L97
        L4e:
            r7 = 0
            if (r5 > r6) goto L81
            int r5 = r4.f2330g
            if (r5 > r6) goto L81
            int r5 = r4.f2331h
            if (r5 <= 0) goto L5a
            goto L81
        L5a:
            O4.h r5 = r4.f2326c
            O4.j r5 = r5.f2342r
            if (r5 != 0) goto L61
            goto L81
        L61:
            monitor-enter(r5)
            int r1 = r5.f2360l     // Catch: java.lang.Throwable -> L7e
            if (r1 == 0) goto L68
            monitor-exit(r5)
            goto L81
        L68:
            K4.x r1 = r5.f2351b     // Catch: java.lang.Throwable -> L7e
            K4.a r1 = r1.f1237a     // Catch: java.lang.Throwable -> L7e
            K4.o r1 = r1.f1079h     // Catch: java.lang.Throwable -> L7e
            K4.a r2 = r4.f2325b     // Catch: java.lang.Throwable -> L7e
            K4.o r2 = r2.f1079h     // Catch: java.lang.Throwable -> L7e
            boolean r1 = L4.b.a(r1, r2)     // Catch: java.lang.Throwable -> L7e
            if (r1 != 0) goto L7a
            monitor-exit(r5)
            goto L81
        L7a:
            K4.x r7 = r5.f2351b     // Catch: java.lang.Throwable -> L7e
            monitor-exit(r5)
            goto L81
        L7e:
            r4 = move-exception
            monitor-exit(r5)
            throw r4
        L81:
            if (r7 == 0) goto L87
            r4.i = r7
        L85:
            r4 = r6
            goto L9c
        L87:
            M.j r5 = r4.f2327d
            if (r5 != 0) goto L8c
            goto L93
        L8c:
            boolean r5 = r5.a()
            if (r5 != r6) goto L93
            goto L97
        L93:
            O4.l r4 = r4.f2328e
            if (r4 != 0) goto L98
        L97:
            goto L85
        L98:
            boolean r4 = r4.h()
        L9c:
            if (r4 != 0) goto L9f
        L9e:
            return r0
        L9f:
            return r6
        */
        throw new UnsupportedOperationException("Method not decompiled: P4.a.c(java.io.IOException, O4.h, F3.e, boolean):boolean");
    }

    public a(r rVar) {
        AbstractC0983h.e("client", rVar);
        this.f2549b = rVar;
    }
}
