package R4;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.ArrayDeque;
import r4.AbstractC0983h;

/* JADX INFO: loaded from: classes.dex */
public final class y {

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public long f2756c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public long f2757d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public long f2758e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public long f2759f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final ArrayDeque f2760g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public boolean f2761h;
    public final w i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final v f2762j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final x f2763k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final x f2764l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public EnumC0183b f2765m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public IOException f2766n;

    public y(int i, q qVar, boolean z5, boolean z6, K4.m mVar) {
        AbstractC0983h.e("connection", qVar);
        this.f2754a = i;
        this.f2755b = qVar;
        this.f2759f = qVar.f2705A.a();
        ArrayDeque arrayDeque = new ArrayDeque();
        this.f2760g = arrayDeque;
        this.i = new w(this, qVar.f2726z.a(), z6);
        this.f2762j = new v(this, z5);
        this.f2763k = new x(this);
        this.f2764l = new x(this);
        if (mVar == null) {
            if (!g()) {
                throw new IllegalStateException("remotely-initiated streams should have headers");
            }
        } else {
            if (g()) {
                throw new IllegalStateException("locally-initiated streams shouldn't have headers yet");
            }
            arrayDeque.add(mVar);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:13:0x0019  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public final void a() {
        /*
            r2 = this;
            byte[] r0 = L4.b.f2100a
            monitor-enter(r2)
            R4.w r0 = r2.i     // Catch: java.lang.Throwable -> L32
            boolean r1 = r0.f2749l     // Catch: java.lang.Throwable -> L32
            if (r1 != 0) goto L19
            boolean r0 = r0.f2752o     // Catch: java.lang.Throwable -> L32
            if (r0 == 0) goto L19
            R4.v r0 = r2.f2762j     // Catch: java.lang.Throwable -> L32
            boolean r1 = r0.f2744k     // Catch: java.lang.Throwable -> L32
            if (r1 != 0) goto L17
            boolean r0 = r0.f2746m     // Catch: java.lang.Throwable -> L32
            if (r0 == 0) goto L19
        L17:
            r0 = 1
            goto L1a
        L19:
            r0 = 0
        L1a:
            boolean r1 = r2.h()     // Catch: java.lang.Throwable -> L32
            monitor-exit(r2)
            if (r0 == 0) goto L28
            R4.b r0 = R4.EnumC0183b.CANCEL
            r1 = 0
            r2.c(r0, r1)
            return
        L28:
            if (r1 != 0) goto L31
            R4.q r0 = r2.f2755b
            int r1 = r2.f2754a
            r0.h(r1)
        L31:
            return
        L32:
            r0 = move-exception
            monitor-exit(r2)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: R4.y.a():void");
    }

    public final void b() throws IOException {
        v vVar = this.f2762j;
        if (vVar.f2746m) {
            throw new IOException("stream closed");
        }
        if (vVar.f2744k) {
            throw new IOException("stream finished");
        }
        if (this.f2765m != null) {
            IOException iOException = this.f2766n;
            if (iOException != null) {
                throw iOException;
            }
            EnumC0183b enumC0183b = this.f2765m;
            AbstractC0983h.b(enumC0183b);
            throw new E(enumC0183b);
        }
    }

    public final void c(EnumC0183b enumC0183b, IOException iOException) {
        if (d(enumC0183b, iOException)) {
            this.f2755b.f2711G.m(this.f2754a, enumC0183b);
        }
    }

    public final boolean d(EnumC0183b enumC0183b, IOException iOException) {
        byte[] bArr = L4.b.f2100a;
        synchronized (this) {
            if (f() != null) {
                return false;
            }
            if (this.i.f2749l && this.f2762j.f2744k) {
                return false;
            }
            this.f2765m = enumC0183b;
            this.f2766n = iOException;
            notifyAll();
            this.f2755b.h(this.f2754a);
            return true;
        }
    }

    public final void e(EnumC0183b enumC0183b) {
        if (d(enumC0183b, null)) {
            this.f2755b.n(this.f2754a, enumC0183b);
        }
    }

    public final synchronized EnumC0183b f() {
        return this.f2765m;
    }

    public final boolean g() {
        boolean z5 = (this.f2754a & 1) == 1;
        this.f2755b.getClass();
        return true == z5;
    }

    public final synchronized boolean h() {
        if (this.f2765m != null) {
            return false;
        }
        w wVar = this.i;
        if (wVar.f2749l || wVar.f2752o) {
            v vVar = this.f2762j;
            if (vVar.f2744k || vVar.f2746m) {
                if (this.f2761h) {
                    return false;
                }
            }
        }
        return true;
    }

    public final void i(K4.m mVar, boolean z5) {
        boolean zH;
        AbstractC0983h.e("headers", mVar);
        byte[] bArr = L4.b.f2100a;
        synchronized (this) {
            try {
                if (this.f2761h && z5) {
                    this.i.getClass();
                } else {
                    this.f2761h = true;
                    this.f2760g.add(mVar);
                }
                if (z5) {
                    this.i.f2749l = true;
                }
                zH = h();
                notifyAll();
            } catch (Throwable th) {
                throw th;
            }
        }
        if (zH) {
            return;
        }
        this.f2755b.h(this.f2754a);
    }

    public final synchronized void j(EnumC0183b enumC0183b) {
        if (this.f2765m == null) {
            this.f2765m = enumC0183b;
            notifyAll();
        }
    }

    public final void k() throws InterruptedIOException {
        try {
            wait();
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
            throw new InterruptedIOException();
        }
    }
}
