package m4;

import C0.C0109j;
import I2.C0334s;
import androidx.media3.extractor.text.ttml.TtmlNode;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import k4.AbstractC2400m;
import k4.C2389b;
import k4.C2393f;
import k4.C2394g;
import k4.C2401n;
import k4.C2407u;
import k4.C2408v;
import k4.C2410x;
import k4.InterfaceC2402o;
import r4.C2888a;

/* JADX INFO: loaded from: classes3.dex */
public final class H0 implements InterfaceC2547u {

    /* JADX INFO: renamed from: E, reason: collision with root package name */
    public static final k4.b0 f20604E;

    /* JADX INFO: renamed from: F, reason: collision with root package name */
    public static final k4.b0 f20605F;

    /* JADX INFO: renamed from: G, reason: collision with root package name */
    public static final k4.r0 f20606G;

    /* JADX INFO: renamed from: H, reason: collision with root package name */
    public static final Random f20607H;

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public final /* synthetic */ k4.i0 f20608A;

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    public final /* synthetic */ C2394g f20609B;

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    public final /* synthetic */ C2407u f20610C;

    /* JADX INFO: renamed from: D, reason: collision with root package name */
    public final /* synthetic */ J3.v f20611D;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final k4.i0 f20612a;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final k4.w0 f20614c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final ScheduledExecutorService f20615d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final k4.g0 f20616e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final R1 f20617f;

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

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final long f20622k;
    public final long l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final Q1 f20623m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final C0334s f20624n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public volatile M1 f20625o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final AtomicBoolean f20626p;

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

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final AtomicInteger f20628r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public C4.f f20629s;

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

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public InterfaceC2553w f20631u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public C0109j f20632v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public C0109j f20633w;

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

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public k4.r0 f20635y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public boolean f20636z;

    static {
        C2401n c2401n = k4.g0.f19867d;
        BitSet bitSet = k4.d0.f19844d;
        f20604E = new k4.b0("grpc-previous-rpc-attempts", c2401n);
        f20605F = new k4.b0("grpc-retry-pushback-ms", c2401n);
        f20606G = k4.r0.f19939f.g("Stream thrown away because RetriableStream committed");
        f20607H = new Random();
    }

    public H0(J3.v vVar, k4.i0 i0Var, k4.g0 g0Var, C2394g c2394g, R1 r12, C2510h0 c2510h0, C2407u c2407u) {
        this.f20611D = vVar;
        this.f20608A = i0Var;
        this.f20609B = c2394g;
        this.f20610C = c2407u;
        S0 s02 = (S0) vVar.f3543a;
        C2500e c2500e = s02.f20782U;
        long j4 = s02.f20783V;
        long j6 = s02.f20784W;
        Executor executor = c2394g.f19859b;
        executor = executor == null ? s02.f20796h : executor;
        ScheduledExecutorService scheduledExecutorService = s02.f20794f.f21089a.f21653d;
        Q1 q12 = (Q1) vVar.f3544b;
        this.f20614c = new k4.w0(new B1());
        this.f20620i = new Object();
        this.f20624n = new C0334s(3, (byte) 0);
        this.f20625o = new M1(new ArrayList(8), Collections.EMPTY_LIST, null, null, false, false, false, 0);
        this.f20626p = new AtomicBoolean();
        this.f20627q = new AtomicInteger();
        this.f20628r = new AtomicInteger();
        this.f20612a = i0Var;
        this.f20621j = c2500e;
        this.f20622k = j4;
        this.l = j6;
        this.f20613b = executor;
        this.f20615d = scheduledExecutorService;
        this.f20616e = g0Var;
        this.f20617f = r12;
        if (r12 != null) {
            this.f20634x = r12.f20751b;
        }
        this.f20618g = c2510h0;
        A4.g.l(r12 == null || c2510h0 == null, "Should not provide both retryPolicy and hedgingPolicy");
        this.f20619h = c2510h0 != null;
        this.f20623m = q12;
    }

    public static void o(H0 h0, Integer num) {
        h0.getClass();
        if (num == null) {
            return;
        }
        if (num.intValue() < 0) {
            h0.t();
            return;
        }
        synchronized (h0.f20620i) {
            try {
                C0109j c0109j = h0.f20633w;
                if (c0109j == null) {
                    return;
                }
                c0109j.f606b = true;
                ScheduledFuture scheduledFuture = (ScheduledFuture) c0109j.f608d;
                C0109j c0109j2 = new C0109j(h0.f20620i);
                h0.f20633w = c0109j2;
                if (scheduledFuture != null) {
                    scheduledFuture.cancel(false);
                }
                c0109j2.f(h0.f20615d.schedule(new E(21, h0, c0109j2), num.intValue(), TimeUnit.MILLISECONDS));
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // m4.h2
    public final void a(boolean z6) {
        r(new G1(z6));
    }

    @Override // m4.InterfaceC2547u
    public final C2389b b() {
        return this.f20625o.f20686f != null ? this.f20625o.f20686f.f20722a.b() : C2389b.f19841b;
    }

    @Override // m4.InterfaceC2547u
    public final void c(int i6) {
        r(new F1(i6, 1));
    }

    @Override // m4.h2
    public final void d(InterfaceC2402o interfaceC2402o) {
        r(new D1(interfaceC2402o, 0));
    }

    @Override // m4.InterfaceC2547u
    public final void e(C0334s c0334s) {
        M1 m12;
        synchronized (this.f20620i) {
            c0334s.g(this.f20624n, "closed");
            m12 = this.f20625o;
        }
        if (m12.f20686f != null) {
            C0334s c0334s2 = new C0334s(3, (byte) 0);
            m12.f20686f.f20722a.e(c0334s2);
            c0334s.g(c0334s2, "committed");
            return;
        }
        C0334s c0334s3 = new C0334s(3, (byte) 0);
        for (P1 p12 : m12.f20683c) {
            C0334s c0334s4 = new C0334s(3, (byte) 0);
            p12.f20722a.e(c0334s4);
            c0334s3.f2658b.add(String.valueOf(c0334s4));
        }
        c0334s.g(c0334s3, TtmlNode.TEXT_EMPHASIS_MARK_OPEN);
    }

    @Override // m4.h2
    public final void f(C2888a c2888a) {
        throw new IllegalStateException("RetriableStream.writeMessage() should not be called directly");
    }

    @Override // m4.h2
    public final void flush() {
        M1 m12 = this.f20625o;
        if (m12.f20681a) {
            m12.f20686f.f20722a.flush();
        } else {
            r(new E1(0));
        }
    }

    @Override // m4.InterfaceC2547u
    public final void g(k4.r0 r0Var) {
        P1 p12;
        P1 p13 = new P1(0);
        p13.f20722a = new C2511h1();
        C1 c1P = p(p13);
        if (c1P != null) {
            synchronized (this.f20620i) {
                this.f20625o = this.f20625o.e(p13);
            }
            c1P.run();
            v(r0Var, EnumC2550v.f21201a, new k4.g0());
            return;
        }
        synchronized (this.f20620i) {
            try {
                if (this.f20625o.f20683c.contains(this.f20625o.f20686f)) {
                    p12 = this.f20625o.f20686f;
                } else {
                    this.f20635y = r0Var;
                    p12 = null;
                }
                M1 m12 = this.f20625o;
                this.f20625o = new M1(m12.f20682b, m12.f20683c, m12.f20684d, m12.f20686f, true, m12.f20681a, m12.f20688h, m12.f20685e);
            } catch (Throwable th) {
                throw th;
            }
        }
        if (p12 != null) {
            p12.f20722a.g(r0Var);
        }
    }

    @Override // m4.InterfaceC2547u
    public final void h() {
        r(new E1(1));
    }

    @Override // m4.h2
    public final void i(int i6) {
        M1 m12 = this.f20625o;
        if (m12.f20681a) {
            m12.f20686f.f20722a.i(i6);
        } else {
            r(new F1(i6, 2));
        }
    }

    @Override // m4.h2
    public final boolean isReady() {
        Iterator it = this.f20625o.f20683c.iterator();
        while (it.hasNext()) {
            if (((P1) it.next()).f20722a.isReady()) {
                return true;
            }
        }
        return false;
    }

    @Override // m4.InterfaceC2547u
    public final void j(int i6) {
        r(new F1(i6, 0));
    }

    @Override // m4.InterfaceC2547u
    public final void k(C2410x c2410x) {
        r(new D1(c2410x, 2));
    }

    /* JADX WARN: Removed duplicated region for block: B:35:0x0068 A[Catch: all -> 0x0072, TryCatch #0 {all -> 0x0072, blocks: (B:27:0x0047, B:29:0x0057, B:31:0x005b, B:35:0x0068, B:38:0x0074), top: B:50:0x0047 }] */
    @Override // m4.InterfaceC2547u
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void l(InterfaceC2553w interfaceC2553w) {
        k4.r0 r0Var;
        C0109j c0109j;
        this.f20631u = interfaceC2553w;
        C0.J j4 = ((S0) this.f20611D.f3543a).f20767F;
        synchronized (j4.f533b) {
            try {
                r0Var = (k4.r0) j4.f535d;
                c0109j = null;
                if (r0Var == null) {
                    ((HashSet) j4.f534c).add(this);
                    r0Var = null;
                }
            } finally {
            }
        }
        if (r0Var != null) {
            g(r0Var);
            return;
        }
        synchronized (this.f20620i) {
            this.f20625o.f20682b.add(new L1(this));
        }
        P1 p1Q = q(0, false);
        if (p1Q == null) {
            return;
        }
        if (this.f20619h) {
            synchronized (this.f20620i) {
                try {
                    this.f20625o = this.f20625o.a(p1Q);
                    if (u(this.f20625o)) {
                        Q1 q12 = this.f20623m;
                        if (q12 == null) {
                            c0109j = new C0109j(this.f20620i);
                            this.f20633w = c0109j;
                        } else {
                            if (q12.f20739d.get() > q12.f20737b) {
                            }
                        }
                    }
                } finally {
                }
            }
            if (c0109j != null) {
                c0109j.f(this.f20615d.schedule(new E(21, this, c0109j), this.f20618g.f21045b, TimeUnit.NANOSECONDS));
            }
        }
        s(p1Q);
    }

    @Override // m4.h2
    public final void m() {
        r(new E1(2));
    }

    @Override // m4.InterfaceC2547u
    public final void n(C2408v c2408v) {
        r(new D1(c2408v, 1));
    }

    public final C1 p(P1 p12) {
        Collection collectionSingleton;
        boolean z6;
        List list;
        ScheduledFuture scheduledFuture;
        ScheduledFuture scheduledFuture2;
        synchronized (this.f20620i) {
            try {
                if (this.f20625o.f20686f != null) {
                    return null;
                }
                Collection collection = this.f20625o.f20683c;
                M1 m12 = this.f20625o;
                A4.g.t(m12.f20686f == null, "Already committed");
                if (m12.f20683c.contains(p12)) {
                    collectionSingleton = Collections.singleton(p12);
                    list = null;
                    z6 = true;
                } else {
                    collectionSingleton = Collections.EMPTY_LIST;
                    z6 = false;
                    list = m12.f20682b;
                }
                this.f20625o = new M1(list, collectionSingleton, m12.f20684d, p12, m12.f20687g, z6, m12.f20688h, m12.f20685e);
                this.f20621j.f20986a.addAndGet(-this.f20630t);
                C0109j c0109j = this.f20632v;
                if (c0109j != null) {
                    c0109j.f606b = true;
                    ScheduledFuture scheduledFuture3 = (ScheduledFuture) c0109j.f608d;
                    this.f20632v = null;
                    scheduledFuture = scheduledFuture3;
                } else {
                    scheduledFuture = null;
                }
                C0109j c0109j2 = this.f20633w;
                if (c0109j2 != null) {
                    c0109j2.f606b = true;
                    ScheduledFuture scheduledFuture4 = (ScheduledFuture) c0109j2.f608d;
                    this.f20633w = null;
                    scheduledFuture2 = scheduledFuture4;
                } else {
                    scheduledFuture2 = null;
                }
                return new C1(this, collection, p12, scheduledFuture, scheduledFuture2);
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final P1 q(int i6, boolean z6) {
        AtomicInteger atomicInteger;
        int i7;
        do {
            atomicInteger = this.f20628r;
            i7 = atomicInteger.get();
            if (i7 < 0) {
                return null;
            }
        } while (!atomicInteger.compareAndSet(i7, i7 + 1));
        P1 p12 = new P1(i6);
        I1 i12 = new I1(new K1(this, p12));
        k4.g0 g0Var = new k4.g0();
        g0Var.d(this.f20616e);
        if (i6 > 0) {
            g0Var.f(f20604E, String.valueOf(i6));
        }
        C2394g c2394g = this.f20609B;
        c2394g.getClass();
        List list = c2394g.f19862e;
        ArrayList arrayList = new ArrayList(list.size() + 1);
        arrayList.addAll(list);
        arrayList.add(i12);
        C2393f c2393fB = C2394g.b(c2394g);
        c2393fB.f19853e = Collections.unmodifiableList(arrayList);
        C2394g c2394g2 = new C2394g(c2393fB);
        AbstractC2400m[] abstractC2400mArrC = AbstractC2504f0.c(c2394g2, g0Var, i6, z6);
        k4.i0 i0Var = this.f20608A;
        InterfaceC2556x interfaceC2556xB = this.f20611D.b(new C2546t1(i0Var, g0Var, c2394g2));
        C2407u c2407u = this.f20610C;
        C2407u c2407uA = c2407u.a();
        try {
            InterfaceC2547u interfaceC2547uC = interfaceC2556xB.c(i0Var, g0Var, c2394g2, abstractC2400mArrC);
            c2407u.c(c2407uA);
            p12.f20722a = interfaceC2547uC;
            return p12;
        } catch (Throwable th) {
            c2407u.c(c2407uA);
            throw th;
        }
    }

    public final void r(J1 j12) {
        Collection collection;
        synchronized (this.f20620i) {
            try {
                if (!this.f20625o.f20681a) {
                    this.f20625o.f20682b.add(j12);
                }
                collection = this.f20625o.f20683c;
            } catch (Throwable th) {
                throw th;
            }
        }
        Iterator it = collection.iterator();
        while (it.hasNext()) {
            j12.a((P1) it.next());
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:24:0x0038, code lost:
    
        if (r1 == null) goto L27;
     */
    /* JADX WARN: Code restructure failed: missing block: B:25:0x003a, code lost:
    
        r8.f20614c.execute(r1);
     */
    /* JADX WARN: Code restructure failed: missing block: B:26:0x003f, code lost:
    
        return;
     */
    /* JADX WARN: Code restructure failed: missing block: B:27:0x0040, code lost:
    
        if (r2 != false) goto L29;
     */
    /* JADX WARN: Code restructure failed: missing block: B:28:0x0042, code lost:
    
        r9.f20722a.l(new m4.k2(4, r8, r9));
     */
    /* JADX WARN: Code restructure failed: missing block: B:29:0x004d, code lost:
    
        r0 = r9.f20722a;
     */
    /* JADX WARN: Code restructure failed: missing block: B:30:0x0053, code lost:
    
        if (r8.f20625o.f20686f != r9) goto L32;
     */
    /* JADX WARN: Code restructure failed: missing block: B:31:0x0055, code lost:
    
        r9 = r8.f20635y;
     */
    /* JADX WARN: Code restructure failed: missing block: B:32:0x0058, code lost:
    
        r9 = m4.H0.f20606G;
     */
    /* JADX WARN: Code restructure failed: missing block: B:33:0x005a, code lost:
    
        r0.g(r9);
     */
    /* JADX WARN: Code restructure failed: missing block: B:34:0x005d, code lost:
    
        return;
     */
    /* JADX WARN: Code restructure failed: missing block: B:44:0x008b, code lost:
    
        r0 = r3.iterator();
     */
    /* JADX WARN: Code restructure failed: missing block: B:46:0x0093, code lost:
    
        if (r0.hasNext() == false) goto L64;
     */
    /* JADX WARN: Code restructure failed: missing block: B:47:0x0095, code lost:
    
        r4 = (m4.J1) r0.next();
        r4.a(r9);
     */
    /* JADX WARN: Code restructure failed: missing block: B:48:0x00a0, code lost:
    
        if ((r4 instanceof m4.L1) == false) goto L50;
     */
    /* JADX WARN: Code restructure failed: missing block: B:49:0x00a2, code lost:
    
        r2 = true;
     */
    /* JADX WARN: Code restructure failed: missing block: B:50:0x00a3, code lost:
    
        r4 = r8.f20625o;
        r5 = r4.f20686f;
     */
    /* JADX WARN: Code restructure failed: missing block: B:51:0x00a7, code lost:
    
        if (r5 == null) goto L54;
     */
    /* JADX WARN: Code restructure failed: missing block: B:52:0x00a9, code lost:
    
        if (r5 == r9) goto L54;
     */
    /* JADX WARN: Code restructure failed: missing block: B:55:0x00ae, code lost:
    
        if (r4.f20687g == false) goto L67;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void s(P1 p12) {
        int iMin;
        int i6 = 0;
        I2.K k6 = null;
        boolean z6 = false;
        ArrayList arrayList = null;
        while (true) {
            synchronized (this.f20620i) {
                try {
                    M1 m12 = this.f20625o;
                    P1 p13 = m12.f20686f;
                    if (p13 == null || p13 == p12) {
                        if (!m12.f20687g) {
                            if (i6 == m12.f20682b.size()) {
                                this.f20625o = m12.e(p12);
                                if (!isReady()) {
                                    return;
                                } else {
                                    k6 = new I2.K(this, 13);
                                }
                            } else {
                                if (p12.f20723b) {
                                    return;
                                }
                                iMin = Math.min(i6 + 128, m12.f20682b.size());
                                if (arrayList == null) {
                                    arrayList = new ArrayList(m12.f20682b.subList(i6, iMin));
                                } else {
                                    arrayList.clear();
                                    arrayList.addAll(m12.f20682b.subList(i6, iMin));
                                }
                            }
                        }
                    }
                } finally {
                }
            }
            i6 = iMin;
        }
    }

    public final void t() {
        ScheduledFuture scheduledFuture;
        synchronized (this.f20620i) {
            try {
                C0109j c0109j = this.f20633w;
                scheduledFuture = null;
                if (c0109j != null) {
                    c0109j.f606b = true;
                    ScheduledFuture scheduledFuture2 = (ScheduledFuture) c0109j.f608d;
                    this.f20633w = null;
                    scheduledFuture = scheduledFuture2;
                }
                M1 m12 = this.f20625o;
                if (!m12.f20688h) {
                    m12 = new M1(m12.f20682b, m12.f20683c, m12.f20684d, m12.f20686f, m12.f20687g, m12.f20681a, true, m12.f20685e);
                }
                this.f20625o = m12;
            } catch (Throwable th) {
                throw th;
            }
        }
        if (scheduledFuture != null) {
            scheduledFuture.cancel(false);
        }
    }

    public final boolean u(M1 m12) {
        if (m12.f20686f == null) {
            return m12.f20685e < this.f20618g.f21044a && !m12.f20688h;
        }
        return false;
    }

    public final void v(k4.r0 r0Var, EnumC2550v enumC2550v, k4.g0 g0Var) {
        this.f20629s = new C4.f(r0Var, enumC2550v, g0Var, 28);
        if (this.f20628r.addAndGet(Integer.MIN_VALUE) == Integer.MIN_VALUE) {
            this.f20614c.execute(new F1.O(this, r0Var, enumC2550v, g0Var, 3));
        }
    }

    public final void w(Object obj) {
        M1 m12 = this.f20625o;
        if (m12.f20681a) {
            m12.f20686f.f20722a.f(this.f20612a.d(obj));
        } else {
            r(new H1(this, obj));
        }
    }
}
