package R4;

import e4.AbstractC0566j;
import e4.AbstractC0571o;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import r4.AbstractC0983h;

/* JADX INFO: loaded from: classes.dex */
public final class r implements P4.d {

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final List f2727g = L4.b.j("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade", ":method", ":path", ":scheme", ":authority");

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final List f2728h = L4.b.j("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade");

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final O4.j f2729a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final P4.f f2730b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final q f2731c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public volatile y f2732d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final K4.s f2733e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public volatile boolean f2734f;

    public r(K4.r rVar, O4.j jVar, P4.f fVar, q qVar) {
        AbstractC0983h.e("client", rVar);
        AbstractC0983h.e("connection", jVar);
        AbstractC0983h.e("http2Connection", qVar);
        this.f2729a = jVar;
        this.f2730b = fVar;
        this.f2731c = qVar;
        K4.s sVar = K4.s.H2_PRIOR_KNOWLEDGE;
        this.f2733e = rVar.f1184B.contains(sVar) ? sVar : K4.s.HTTP_2;
    }

    @Override // P4.d
    public final X4.u a(K4.u uVar) {
        y yVar = this.f2732d;
        AbstractC0983h.b(yVar);
        return yVar.i;
    }

    @Override // P4.d
    public final long b(K4.u uVar) {
        if (P4.e.a(uVar)) {
            return L4.b.i(uVar);
        }
        return 0L;
    }

    @Override // P4.d
    public final void c() {
        y yVar = this.f2732d;
        AbstractC0983h.b(yVar);
        synchronized (yVar) {
            if (!yVar.f2761h && !yVar.g()) {
                throw new IllegalStateException("reply before requesting the sink");
            }
        }
        yVar.f2762j.close();
    }

    @Override // P4.d
    public final void cancel() {
        this.f2734f = true;
        y yVar = this.f2732d;
        if (yVar == null) {
            return;
        }
        yVar.e(EnumC0183b.CANCEL);
    }

    @Override // P4.d
    public final void d() {
        this.f2731c.flush();
    }

    @Override // P4.d
    public final void e(F3.e eVar) throws IOException {
        int i;
        y yVar;
        if (this.f2732d != null) {
            return;
        }
        eVar.getClass();
        K4.m mVar = (K4.m) eVar.f716n;
        ArrayList arrayList = new ArrayList(mVar.size() + 4);
        arrayList.add(new C0184c(C0184c.f2656f, (String) eVar.f715m));
        X4.h hVar = C0184c.f2657g;
        K4.o oVar = (K4.o) eVar.f714l;
        AbstractC0983h.e("url", oVar);
        String strB = oVar.b();
        String strD = oVar.d();
        if (strD != null) {
            strB = strB + '?' + ((Object) strD);
        }
        arrayList.add(new C0184c(hVar, strB));
        String strA = ((K4.m) eVar.f716n).a("Host");
        if (strA != null) {
            arrayList.add(new C0184c(C0184c.i, strA));
        }
        arrayList.add(new C0184c(C0184c.f2658h, oVar.f1158a));
        int size = mVar.size();
        int i5 = 0;
        while (i5 < size) {
            int i6 = i5 + 1;
            String strB2 = mVar.b(i5);
            Locale locale = Locale.US;
            AbstractC0983h.d("US", locale);
            String lowerCase = strB2.toLowerCase(locale);
            AbstractC0983h.d("this as java.lang.String).toLowerCase(locale)", lowerCase);
            if (!f2727g.contains(lowerCase) || (lowerCase.equals("te") && AbstractC0983h.a(mVar.d(i5), "trailers"))) {
                arrayList.add(new C0184c(lowerCase, mVar.d(i5)));
            }
            i5 = i6;
        }
        q qVar = this.f2731c;
        qVar.getClass();
        boolean z5 = !false;
        synchronized (qVar.f2711G) {
            synchronized (qVar) {
                try {
                    if (qVar.f2717o > 1073741823) {
                        qVar.k(EnumC0183b.REFUSED_STREAM);
                    }
                    if (qVar.p) {
                        throw new C0182a();
                    }
                    i = qVar.f2717o;
                    qVar.f2717o = i + 2;
                    yVar = new y(i, qVar, z5, false, null);
                    if (yVar.h()) {
                        qVar.f2714l.put(Integer.valueOf(i), yVar);
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            qVar.f2711G.k(z5, i, arrayList);
        }
        qVar.f2711G.flush();
        this.f2732d = yVar;
        if (this.f2734f) {
            y yVar2 = this.f2732d;
            AbstractC0983h.b(yVar2);
            yVar2.e(EnumC0183b.CANCEL);
            throw new IOException("Canceled");
        }
        y yVar3 = this.f2732d;
        AbstractC0983h.b(yVar3);
        x xVar = yVar3.f2763k;
        long j5 = this.f2730b.f2557g;
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        xVar.g(j5);
        y yVar4 = this.f2732d;
        AbstractC0983h.b(yVar4);
        yVar4.f2764l.g(this.f2730b.f2558h);
    }

    @Override // P4.d
    public final K4.t f(boolean z5) throws IOException {
        K4.m mVar;
        y yVar = this.f2732d;
        if (yVar == null) {
            throw new IOException("stream wasn't created");
        }
        synchronized (yVar) {
            yVar.f2763k.h();
            while (yVar.f2760g.isEmpty() && yVar.f2765m == null) {
                try {
                    yVar.k();
                } catch (Throwable th) {
                    yVar.f2763k.k();
                    throw th;
                }
            }
            yVar.f2763k.k();
            if (yVar.f2760g.isEmpty()) {
                IOException iOException = yVar.f2766n;
                if (iOException != null) {
                    throw iOException;
                }
                EnumC0183b enumC0183b = yVar.f2765m;
                AbstractC0983h.b(enumC0183b);
                throw new E(enumC0183b);
            }
            Object objRemoveFirst = yVar.f2760g.removeFirst();
            AbstractC0983h.d("headersQueue.removeFirst()", objRemoveFirst);
            mVar = (K4.m) objRemoveFirst;
        }
        K4.s sVar = this.f2733e;
        AbstractC0983h.e("protocol", sVar);
        ArrayList arrayList = new ArrayList(20);
        int size = mVar.size();
        P4.g gVarT = null;
        int i = 0;
        while (i < size) {
            int i5 = i + 1;
            String strB = mVar.b(i);
            String strD = mVar.d(i);
            if (AbstractC0983h.a(strB, ":status")) {
                gVarT = AbstractC0566j.t(AbstractC0983h.h("HTTP/1.1 ", strD));
            } else if (!f2728h.contains(strB)) {
                AbstractC0983h.e("name", strB);
                AbstractC0983h.e("value", strD);
                arrayList.add(strB);
                arrayList.add(A4.i.c0(strD).toString());
            }
            i = i5;
        }
        if (gVarT == null) {
            throw new ProtocolException("Expected ':status' header not present");
        }
        K4.t tVar = new K4.t();
        tVar.f1212b = sVar;
        tVar.f1213c = gVarT.f2560b;
        tVar.f1214d = (String) gVarT.f2562d;
        Object[] array = arrayList.toArray(new String[0]);
        if (array == null) {
            throw new NullPointerException("null cannot be cast to non-null type kotlin.Array<T of kotlin.collections.ArraysKt__ArraysJVMKt.toTypedArray>");
        }
        K4.l lVar = new K4.l(0);
        AbstractC0571o.b0(lVar.f1147a, (String[]) array);
        tVar.f1216f = lVar;
        if (z5 && tVar.f1213c == 100) {
            return null;
        }
        return tVar;
    }

    @Override // P4.d
    public final O4.j g() {
        return this.f2729a;
    }
}
