package m7;

import P6.g;
import X6.e;
import X6.n;
import e0.C0853a;
import java.util.ArrayList;
import k7.B;
import k7.C;
import k7.E;
import k7.o;
import k7.r;
import k7.x;
import k7.y;
import o7.i;
import p7.d;

/* JADX INFO: loaded from: classes.dex */
public final class b implements r {
    @Override // k7.r
    public final C a(d dVar) throws Throwable {
        Throwable th;
        int i;
        System.currentTimeMillis();
        y yVar = dVar.f16161e;
        g.e(yVar, "request");
        Throwable th2 = null;
        C0853a c0853a = new C0853a(yVar, 13, th2);
        if (yVar.a().f14054j) {
            c0853a = new C0853a(th2, 13, th2);
        }
        i iVar = dVar.f16157a;
        y yVar2 = (y) c0853a.f12397r;
        C c3 = (C) c0853a.f12398s;
        if (yVar2 == null && c3 == null) {
            C c8 = new C(yVar, x.f14195s, "Unsatisfiable Request (only-if-cached)", 504, null, new o((String[]) new ArrayList(20).toArray(new String[0])), l7.b.f14414c, null, null, null, -1L, System.currentTimeMillis(), null);
            g.e(iVar, "call");
            return c8;
        }
        if (yVar2 == null) {
            g.b(c3);
            B bG = c3.g();
            C cA = a.a(c3);
            B.b("cacheResponse", cA);
            bG.i = cA;
            C cA2 = bG.a();
            g.e(iVar, "call");
            return cA2;
        }
        if (c3 != null) {
            g.e(iVar, "call");
        }
        C cB = dVar.b(yVar2);
        if (c3 != null) {
            if (cB.f14013t == 304) {
                B bG2 = c3.g();
                o oVar = cB.f14015v;
                ArrayList arrayList = new ArrayList(20);
                o oVar2 = c3.f14015v;
                int size = oVar2.size();
                int i5 = 0;
                while (true) {
                    th = th2;
                    if (i5 >= size) {
                        break;
                    }
                    String strC = oVar2.c(i5);
                    int i8 = size;
                    String strG = oVar2.g(i5);
                    o oVar3 = oVar2;
                    if ("Warning".equalsIgnoreCase(strC)) {
                        i = i5;
                        if (n.M(strG, "1", false)) {
                        }
                        i5 = i + 1;
                        size = i8;
                        th2 = th;
                        oVar2 = oVar3;
                    } else {
                        i = i5;
                    }
                    if ("Content-Length".equalsIgnoreCase(strC) || "Content-Encoding".equalsIgnoreCase(strC) || "Content-Type".equalsIgnoreCase(strC) || !a.b(strC) || oVar.a(strC) == null) {
                        g.e(strC, "name");
                        g.e(strG, "value");
                        arrayList.add(strC);
                        arrayList.add(e.j0(strG).toString());
                    }
                    i5 = i + 1;
                    size = i8;
                    th2 = th;
                    oVar2 = oVar3;
                }
                int size2 = oVar.size();
                for (int i9 = 0; i9 < size2; i9++) {
                    String strC2 = oVar.c(i9);
                    if (!"Content-Length".equalsIgnoreCase(strC2) && !"Content-Encoding".equalsIgnoreCase(strC2) && !"Content-Type".equalsIgnoreCase(strC2) && a.b(strC2)) {
                        String strG2 = oVar.g(i9);
                        g.e(strC2, "name");
                        g.e(strG2, "value");
                        arrayList.add(strC2);
                        arrayList.add(e.j0(strG2).toString());
                    }
                }
                bG2.f14000f = new o((String[]) arrayList.toArray(new String[0])).f();
                bG2.f14004k = cB.f14007A;
                bG2.f14005l = cB.f14008B;
                C cA3 = a.a(c3);
                B.b("cacheResponse", cA3);
                bG2.i = cA3;
                C cA4 = a.a(cB);
                B.b("networkResponse", cA4);
                bG2.f14002h = cA4;
                bG2.a();
                E e8 = cB.f14016w;
                g.b(e8);
                e8.close();
                g.b(th);
                throw th;
            }
            E e9 = c3.f14016w;
            if (e9 != null) {
                l7.b.d(e9);
            }
        }
        B bG3 = cB.g();
        C cA5 = a.a(c3);
        B.b("cacheResponse", cA5);
        bG3.i = cA5;
        C cA6 = a.a(cB);
        B.b("networkResponse", cA6);
        bG3.f14002h = cA6;
        return bG3.a();
    }
}
