package F1;

import F1.n0;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import kotlinx.coroutines.internal.i;
import n1.AbstractC0645a;
import q1.g;
import r1.AbstractC0707b;

/* JADX INFO: loaded from: classes.dex */
public class u0 implements n0, InterfaceC0137s, C0 {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static final /* synthetic */ AtomicReferenceFieldUpdater f639b = AtomicReferenceFieldUpdater.newUpdater(u0.class, Object.class, "_state");
    private volatile /* synthetic */ Object _parentHandle;
    private volatile /* synthetic */ Object _state;

    private static final class a extends C0132m {

        /* JADX INFO: renamed from: M, reason: collision with root package name */
        private final u0 f640M;

        public a(q1.d dVar, u0 u0Var) {
            super(dVar, 1);
            this.f640M = u0Var;
        }

        @Override // F1.C0132m
        protected String B() {
            return "AwaitContinuation";
        }

        @Override // F1.C0132m
        public Throwable r(n0 n0Var) {
            Throwable thE;
            Object objN = this.f640M.N();
            return (!(objN instanceof c) || (thE = ((c) objN).e()) == null) ? objN instanceof C0143y ? ((C0143y) objN).f667a : n0Var.e() : thE;
        }
    }

    private static final class b extends t0 {

        /* JADX INFO: renamed from: H, reason: collision with root package name */
        private final c f641H;

        /* JADX INFO: renamed from: I, reason: collision with root package name */
        private final r f642I;

        /* JADX INFO: renamed from: L, reason: collision with root package name */
        private final Object f643L;

        /* JADX INFO: renamed from: y, reason: collision with root package name */
        private final u0 f644y;

        public b(u0 u0Var, c cVar, r rVar, Object obj) {
            this.f644y = u0Var;
            this.f641H = cVar;
            this.f642I = rVar;
            this.f643L = obj;
        }

        @Override // y1.l
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            p((Throwable) obj);
            return n1.s.f5871a;
        }

        @Override // F1.A
        public void p(Throwable th) {
            this.f644y.D(this.f641H, this.f642I, this.f643L);
        }
    }

    private static final class c implements InterfaceC0126i0 {
        private volatile /* synthetic */ Object _exceptionsHolder = null;
        private volatile /* synthetic */ int _isCompleting;
        private volatile /* synthetic */ Object _rootCause;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final z0 f645b;

        public c(z0 z0Var, boolean z2, Throwable th) {
            this.f645b = z0Var;
            this._isCompleting = z2 ? 1 : 0;
            this._rootCause = th;
        }

        private final ArrayList c() {
            return new ArrayList(4);
        }

        private final Object d() {
            return this._exceptionsHolder;
        }

        private final void k(Object obj) {
            this._exceptionsHolder = obj;
        }

        @Override // F1.InterfaceC0126i0
        public z0 a() {
            return this.f645b;
        }

        public final void b(Throwable th) {
            Throwable thE = e();
            if (thE == null) {
                l(th);
                return;
            }
            if (th == thE) {
                return;
            }
            Object objD = d();
            if (objD == null) {
                k(th);
                return;
            }
            if (!(objD instanceof Throwable)) {
                if (!(objD instanceof ArrayList)) {
                    throw new IllegalStateException(kotlin.jvm.internal.l.l("State is ", objD).toString());
                }
                ((ArrayList) objD).add(th);
            } else {
                if (th == objD) {
                    return;
                }
                ArrayList arrayListC = c();
                arrayListC.add(objD);
                arrayListC.add(th);
                n1.s sVar = n1.s.f5871a;
                k(arrayListC);
            }
        }

        public final Throwable e() {
            return (Throwable) this._rootCause;
        }

        public final boolean f() {
            return e() != null;
        }

        /* JADX WARN: Type inference failed for: r0v0, types: [boolean, int] */
        public final boolean g() {
            return this._isCompleting;
        }

        public final boolean h() {
            return d() == v0.f656e;
        }

        public final List i(Throwable th) {
            ArrayList arrayListC;
            Object objD = d();
            if (objD == null) {
                arrayListC = c();
            } else if (objD instanceof Throwable) {
                ArrayList arrayListC2 = c();
                arrayListC2.add(objD);
                arrayListC = arrayListC2;
            } else {
                if (!(objD instanceof ArrayList)) {
                    throw new IllegalStateException(kotlin.jvm.internal.l.l("State is ", objD).toString());
                }
                arrayListC = (ArrayList) objD;
            }
            Throwable thE = e();
            if (thE != null) {
                arrayListC.add(0, thE);
            }
            if (th != null && !kotlin.jvm.internal.l.a(th, thE)) {
                arrayListC.add(th);
            }
            k(v0.f656e);
            return arrayListC;
        }

        @Override // F1.InterfaceC0126i0
        public boolean isActive() {
            return e() == null;
        }

        public final void j(boolean z2) {
            this._isCompleting = z2 ? 1 : 0;
        }

        public final void l(Throwable th) {
            this._rootCause = th;
        }

        public String toString() {
            return "Finishing[cancelling=" + f() + ", completing=" + g() + ", rootCause=" + e() + ", exceptions=" + d() + ", list=" + a() + ']';
        }
    }

    public static final class d extends i.a {

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        final /* synthetic */ kotlinx.coroutines.internal.i f646d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        final /* synthetic */ u0 f647e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        final /* synthetic */ Object f648f;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public d(kotlinx.coroutines.internal.i iVar, u0 u0Var, Object obj) {
            super(iVar);
            this.f646d = iVar;
            this.f647e = u0Var;
            this.f648f = obj;
        }

        @Override // kotlinx.coroutines.internal.c
        /* JADX INFO: renamed from: i, reason: merged with bridge method [inline-methods] */
        public Object g(kotlinx.coroutines.internal.i iVar) {
            if (this.f647e.N() == this.f648f) {
                return null;
            }
            return kotlinx.coroutines.internal.h.a();
        }
    }

    public u0(boolean z2) {
        this._state = z2 ? v0.f658g : v0.f657f;
        this._parentHandle = null;
    }

    private final void C(InterfaceC0126i0 interfaceC0126i0, Object obj) throws Throwable {
        InterfaceC0136q interfaceC0136qM = M();
        if (interfaceC0136qM != null) {
            interfaceC0136qM.dispose();
            h0(A0.f573b);
        }
        C0143y c0143y = obj instanceof C0143y ? (C0143y) obj : null;
        Throwable th = c0143y != null ? c0143y.f667a : null;
        if (!(interfaceC0126i0 instanceof t0)) {
            z0 z0VarA = interfaceC0126i0.a();
            if (z0VarA == null) {
                return;
            }
            a0(z0VarA, th);
            return;
        }
        try {
            ((t0) interfaceC0126i0).p(th);
        } catch (Throwable th2) {
            P(new B("Exception in completion handler " + interfaceC0126i0 + " for " + this, th2));
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void D(c cVar, r rVar, Object obj) {
        r rVarY = Y(rVar);
        if (rVarY == null || !r0(cVar, rVarY, obj)) {
            p(F(cVar, obj));
        }
    }

    private final Throwable E(Object obj) {
        if (obj == null ? true : obj instanceof Throwable) {
            Throwable th = (Throwable) obj;
            return th == null ? new o0(A(), null, this) : th;
        }
        if (obj != null) {
            return ((C0) obj).n();
        }
        throw new NullPointerException("null cannot be cast to non-null type kotlinx.coroutines.ParentJob");
    }

    private final Object F(c cVar, Object obj) throws Throwable {
        boolean zF;
        Throwable thI;
        C0143y c0143y = obj instanceof C0143y ? (C0143y) obj : null;
        Throwable th = c0143y == null ? null : c0143y.f667a;
        synchronized (cVar) {
            zF = cVar.f();
            List listI = cVar.i(th);
            thI = I(cVar, listI);
            if (thI != null) {
                o(thI, listI);
            }
        }
        if (thI != null && thI != th) {
            obj = new C0143y(thI, false, 2, null);
        }
        if (thI != null && (z(thI) || O(thI))) {
            if (obj == null) {
                throw new NullPointerException("null cannot be cast to non-null type kotlinx.coroutines.CompletedExceptionally");
            }
            ((C0143y) obj).b();
        }
        if (!zF) {
            b0(thI);
        }
        c0(obj);
        androidx.concurrent.futures.a.a(f639b, this, cVar, v0.g(obj));
        C(cVar, obj);
        return obj;
    }

    private final r G(InterfaceC0126i0 interfaceC0126i0) {
        r rVar = interfaceC0126i0 instanceof r ? (r) interfaceC0126i0 : null;
        if (rVar != null) {
            return rVar;
        }
        z0 z0VarA = interfaceC0126i0.a();
        if (z0VarA == null) {
            return null;
        }
        return Y(z0VarA);
    }

    private final Throwable H(Object obj) {
        C0143y c0143y = obj instanceof C0143y ? (C0143y) obj : null;
        if (c0143y == null) {
            return null;
        }
        return c0143y.f667a;
    }

    private final Throwable I(c cVar, List list) {
        Object obj = null;
        if (list.isEmpty()) {
            if (cVar.f()) {
                return new o0(A(), null, this);
            }
            return null;
        }
        Iterator it = list.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            Object next = it.next();
            if (!(((Throwable) next) instanceof CancellationException)) {
                obj = next;
                break;
            }
        }
        Throwable th = (Throwable) obj;
        return th != null ? th : (Throwable) list.get(0);
    }

    private final z0 L(InterfaceC0126i0 interfaceC0126i0) {
        z0 z0VarA = interfaceC0126i0.a();
        if (z0VarA != null) {
            return z0VarA;
        }
        if (interfaceC0126i0 instanceof Z) {
            return new z0();
        }
        if (!(interfaceC0126i0 instanceof t0)) {
            throw new IllegalStateException(kotlin.jvm.internal.l.l("State should have list: ", interfaceC0126i0).toString());
        }
        f0((t0) interfaceC0126i0);
        return null;
    }

    private final Object U(Object obj) throws Throwable {
        Throwable thE = null;
        while (true) {
            Object objN = N();
            if (objN instanceof c) {
                synchronized (objN) {
                    if (((c) objN).h()) {
                        return v0.f655d;
                    }
                    boolean zF = ((c) objN).f();
                    if (obj != null || !zF) {
                        if (thE == null) {
                            thE = E(obj);
                        }
                        ((c) objN).b(thE);
                    }
                    Throwable thE2 = zF ? null : ((c) objN).e();
                    if (thE2 != null) {
                        Z(((c) objN).a(), thE2);
                    }
                    return v0.f652a;
                }
            }
            if (!(objN instanceof InterfaceC0126i0)) {
                return v0.f655d;
            }
            if (thE == null) {
                thE = E(obj);
            }
            InterfaceC0126i0 interfaceC0126i0 = (InterfaceC0126i0) objN;
            if (!interfaceC0126i0.isActive()) {
                Object objP0 = p0(objN, new C0143y(thE, false, 2, null));
                if (objP0 == v0.f652a) {
                    throw new IllegalStateException(kotlin.jvm.internal.l.l("Cannot happen in ", objN).toString());
                }
                if (objP0 != v0.f654c) {
                    return objP0;
                }
            } else if (o0(interfaceC0126i0, thE)) {
                return v0.f652a;
            }
        }
    }

    private final t0 W(y1.l lVar, boolean z2) {
        t0 m0Var;
        if (z2) {
            m0Var = lVar instanceof p0 ? (p0) lVar : null;
            if (m0Var == null) {
                m0Var = new l0(lVar);
            }
        } else {
            t0 t0Var = lVar instanceof t0 ? (t0) lVar : null;
            m0Var = t0Var != null ? t0Var : null;
            if (m0Var == null) {
                m0Var = new m0(lVar);
            }
        }
        m0Var.r(this);
        return m0Var;
    }

    private final r Y(kotlinx.coroutines.internal.i iVar) {
        while (iVar.k()) {
            iVar = iVar.j();
        }
        while (true) {
            iVar = iVar.i();
            if (!iVar.k()) {
                if (iVar instanceof r) {
                    return (r) iVar;
                }
                if (iVar instanceof z0) {
                    return null;
                }
            }
        }
    }

    private final void Z(z0 z0Var, Throwable th) throws Throwable {
        B b2;
        b0(th);
        B b3 = null;
        for (kotlinx.coroutines.internal.i iVarI = (kotlinx.coroutines.internal.i) z0Var.h(); !kotlin.jvm.internal.l.a(iVarI, z0Var); iVarI = iVarI.i()) {
            if (iVarI instanceof p0) {
                t0 t0Var = (t0) iVarI;
                try {
                    t0Var.p(th);
                } catch (Throwable th2) {
                    if (b3 == null) {
                        b2 = null;
                    } else {
                        AbstractC0645a.a(b3, th2);
                        b2 = b3;
                    }
                    if (b2 == null) {
                        b3 = new B("Exception in completion handler " + t0Var + " for " + this, th2);
                    }
                }
            }
        }
        if (b3 != null) {
            P(b3);
        }
        z(th);
    }

    private final void a0(z0 z0Var, Throwable th) throws Throwable {
        B b2;
        B b3 = null;
        for (kotlinx.coroutines.internal.i iVarI = (kotlinx.coroutines.internal.i) z0Var.h(); !kotlin.jvm.internal.l.a(iVarI, z0Var); iVarI = iVarI.i()) {
            if (iVarI instanceof t0) {
                t0 t0Var = (t0) iVarI;
                try {
                    t0Var.p(th);
                } catch (Throwable th2) {
                    if (b3 == null) {
                        b2 = null;
                    } else {
                        AbstractC0645a.a(b3, th2);
                        b2 = b3;
                    }
                    if (b2 == null) {
                        b3 = new B("Exception in completion handler " + t0Var + " for " + this, th2);
                    }
                }
            }
        }
        if (b3 == null) {
            return;
        }
        P(b3);
    }

    private final void e0(Z z2) {
        z0 z0Var = new z0();
        Object c0124h0 = z0Var;
        if (!z2.isActive()) {
            c0124h0 = new C0124h0(z0Var);
        }
        androidx.concurrent.futures.a.a(f639b, this, z2, c0124h0);
    }

    private final void f0(t0 t0Var) {
        t0Var.d(new z0());
        androidx.concurrent.futures.a.a(f639b, this, t0Var, t0Var.i());
    }

    private final int i0(Object obj) {
        if (obj instanceof Z) {
            if (((Z) obj).isActive()) {
                return 0;
            }
            if (!androidx.concurrent.futures.a.a(f639b, this, obj, v0.f658g)) {
                return -1;
            }
            d0();
            return 1;
        }
        if (!(obj instanceof C0124h0)) {
            return 0;
        }
        if (!androidx.concurrent.futures.a.a(f639b, this, obj, ((C0124h0) obj).a())) {
            return -1;
        }
        d0();
        return 1;
    }

    private final String j0(Object obj) {
        if (!(obj instanceof c)) {
            return obj instanceof InterfaceC0126i0 ? ((InterfaceC0126i0) obj).isActive() ? "Active" : "New" : obj instanceof C0143y ? "Cancelled" : "Completed";
        }
        c cVar = (c) obj;
        return cVar.f() ? "Cancelling" : cVar.g() ? "Completing" : "Active";
    }

    private final boolean l(Object obj, z0 z0Var, t0 t0Var) {
        int iO;
        d dVar = new d(t0Var, this, obj);
        do {
            iO = z0Var.j().o(t0Var, z0Var, dVar);
            if (iO == 1) {
                return true;
            }
        } while (iO != 2);
        return false;
    }

    public static /* synthetic */ CancellationException l0(u0 u0Var, Throwable th, String str, int i2, Object obj) {
        if (obj != null) {
            throw new UnsupportedOperationException("Super calls with default arguments not supported in this target, function: toCancellationException");
        }
        if ((i2 & 1) != 0) {
            str = null;
        }
        return u0Var.k0(th, str);
    }

    private final boolean n0(InterfaceC0126i0 interfaceC0126i0, Object obj) throws Throwable {
        if (!androidx.concurrent.futures.a.a(f639b, this, interfaceC0126i0, v0.g(obj))) {
            return false;
        }
        b0(null);
        c0(obj);
        C(interfaceC0126i0, obj);
        return true;
    }

    private final void o(Throwable th, List list) {
        if (list.size() <= 1) {
            return;
        }
        Set setNewSetFromMap = Collections.newSetFromMap(new IdentityHashMap(list.size()));
        Iterator it = list.iterator();
        while (it.hasNext()) {
            Throwable th2 = (Throwable) it.next();
            if (th2 != th && th2 != th && !(th2 instanceof CancellationException) && setNewSetFromMap.add(th2)) {
                AbstractC0645a.a(th, th2);
            }
        }
    }

    private final boolean o0(InterfaceC0126i0 interfaceC0126i0, Throwable th) throws Throwable {
        z0 z0VarL = L(interfaceC0126i0);
        if (z0VarL == null) {
            return false;
        }
        if (!androidx.concurrent.futures.a.a(f639b, this, interfaceC0126i0, new c(z0VarL, false, th))) {
            return false;
        }
        Z(z0VarL, th);
        return true;
    }

    private final Object p0(Object obj, Object obj2) {
        return !(obj instanceof InterfaceC0126i0) ? v0.f652a : ((!(obj instanceof Z) && !(obj instanceof t0)) || (obj instanceof r) || (obj2 instanceof C0143y)) ? q0((InterfaceC0126i0) obj, obj2) : n0((InterfaceC0126i0) obj, obj2) ? obj2 : v0.f654c;
    }

    private final Object q0(InterfaceC0126i0 interfaceC0126i0, Object obj) throws Throwable {
        z0 z0VarL = L(interfaceC0126i0);
        if (z0VarL == null) {
            return v0.f654c;
        }
        c cVar = interfaceC0126i0 instanceof c ? (c) interfaceC0126i0 : null;
        if (cVar == null) {
            cVar = new c(z0VarL, false, null);
        }
        synchronized (cVar) {
            if (cVar.g()) {
                return v0.f652a;
            }
            cVar.j(true);
            if (cVar != interfaceC0126i0 && !androidx.concurrent.futures.a.a(f639b, this, interfaceC0126i0, cVar)) {
                return v0.f654c;
            }
            boolean zF = cVar.f();
            C0143y c0143y = obj instanceof C0143y ? (C0143y) obj : null;
            if (c0143y != null) {
                cVar.b(c0143y.f667a);
            }
            Throwable thE = zF ? null : cVar.e();
            n1.s sVar = n1.s.f5871a;
            if (thE != null) {
                Z(z0VarL, thE);
            }
            r rVarG = G(interfaceC0126i0);
            return (rVarG == null || !r0(cVar, rVarG, obj)) ? F(cVar, obj) : v0.f653b;
        }
    }

    private final boolean r0(c cVar, r rVar, Object obj) {
        while (n0.a.d(rVar.f636y, false, false, new b(this, cVar, rVar, obj), 1, null) == A0.f573b) {
            rVar = Y(rVar);
            if (rVar == null) {
                return false;
            }
        }
        return true;
    }

    private final Object u(q1.d dVar) {
        a aVar = new a(AbstractC0707b.c(dVar), this);
        aVar.v();
        AbstractC0133n.a(aVar, R(new D0(aVar)));
        Object objS = aVar.s();
        if (objS == AbstractC0707b.d()) {
            kotlin.coroutines.jvm.internal.h.c(dVar);
        }
        return objS;
    }

    private final Object y(Object obj) {
        Object objP0;
        do {
            Object objN = N();
            if (!(objN instanceof InterfaceC0126i0) || ((objN instanceof c) && ((c) objN).g())) {
                return v0.f652a;
            }
            objP0 = p0(objN, new C0143y(E(obj), false, 2, null));
        } while (objP0 == v0.f654c);
        return objP0;
    }

    private final boolean z(Throwable th) {
        if (T()) {
            return true;
        }
        boolean z2 = th instanceof CancellationException;
        InterfaceC0136q interfaceC0136qM = M();
        return (interfaceC0136qM == null || interfaceC0136qM == A0.f573b) ? z2 : interfaceC0136qM.b(th) || z2;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public String A() {
        return "Job was cancelled";
    }

    public boolean B(Throwable th) {
        if (th instanceof CancellationException) {
            return true;
        }
        return w(th) && J();
    }

    public boolean J() {
        return true;
    }

    public boolean K() {
        return false;
    }

    public final InterfaceC0136q M() {
        return (InterfaceC0136q) this._parentHandle;
    }

    public final Object N() {
        while (true) {
            Object obj = this._state;
            if (!(obj instanceof kotlinx.coroutines.internal.o)) {
                return obj;
            }
            ((kotlinx.coroutines.internal.o) obj).c(this);
        }
    }

    protected boolean O(Throwable th) {
        return false;
    }

    public void P(Throwable th) throws Throwable {
        throw th;
    }

    protected final void Q(n0 n0Var) {
        if (n0Var == null) {
            h0(A0.f573b);
            return;
        }
        n0Var.start();
        InterfaceC0136q interfaceC0136qT = n0Var.t(this);
        h0(interfaceC0136qT);
        if (S()) {
            interfaceC0136qT.dispose();
            h0(A0.f573b);
        }
    }

    public final X R(y1.l lVar) {
        return m(false, true, lVar);
    }

    public final boolean S() {
        return !(N() instanceof InterfaceC0126i0);
    }

    protected boolean T() {
        return false;
    }

    public final Object V(Object obj) {
        Object objP0;
        do {
            objP0 = p0(N(), obj);
            if (objP0 == v0.f652a) {
                throw new IllegalStateException("Job " + this + " is already complete or completing, but is being completed with " + obj, H(obj));
            }
        } while (objP0 == v0.f654c);
        return objP0;
    }

    public String X() {
        return L.a(this);
    }

    protected void b0(Throwable th) {
    }

    protected void c0(Object obj) {
    }

    protected void d0() {
    }

    @Override // F1.n0
    public final CancellationException e() {
        Object objN = N();
        if (!(objN instanceof c)) {
            if (objN instanceof InterfaceC0126i0) {
                throw new IllegalStateException(kotlin.jvm.internal.l.l("Job is still new or active: ", this).toString());
            }
            return objN instanceof C0143y ? l0(this, ((C0143y) objN).f667a, null, 1, null) : new o0(kotlin.jvm.internal.l.l(L.a(this), " has completed normally"), null, this);
        }
        Throwable thE = ((c) objN).e();
        if (thE != null) {
            return k0(thE, kotlin.jvm.internal.l.l(L.a(this), " is cancelling"));
        }
        throw new IllegalStateException(kotlin.jvm.internal.l.l("Job is still new or active: ", this).toString());
    }

    @Override // q1.g
    public Object fold(Object obj, y1.p pVar) {
        return n0.a.b(this, obj, pVar);
    }

    public final void g0(t0 t0Var) {
        Object objN;
        do {
            objN = N();
            if (!(objN instanceof t0)) {
                if (!(objN instanceof InterfaceC0126i0) || ((InterfaceC0126i0) objN).a() == null) {
                    return;
                }
                t0Var.l();
                return;
            }
            if (objN != t0Var) {
                return;
            }
        } while (!androidx.concurrent.futures.a.a(f639b, this, objN, v0.f658g));
    }

    @Override // q1.g.b, q1.g
    public g.b get(g.c cVar) {
        return n0.a.c(this, cVar);
    }

    @Override // q1.g.b
    public final g.c getKey() {
        return n0.f630a;
    }

    public final void h0(InterfaceC0136q interfaceC0136q) {
        this._parentHandle = interfaceC0136q;
    }

    @Override // F1.n0
    public boolean isActive() {
        Object objN = N();
        return (objN instanceof InterfaceC0126i0) && ((InterfaceC0126i0) objN).isActive();
    }

    @Override // F1.InterfaceC0137s
    public final void j(C0 c02) throws Throwable {
        w(c02);
    }

    protected final CancellationException k0(Throwable th, String str) {
        CancellationException o0Var = th instanceof CancellationException ? (CancellationException) th : null;
        if (o0Var == null) {
            if (str == null) {
                str = A();
            }
            o0Var = new o0(str, th, this);
        }
        return o0Var;
    }

    @Override // F1.n0
    public final X m(boolean z2, boolean z3, y1.l lVar) {
        t0 t0VarW = W(lVar, z2);
        while (true) {
            Object objN = N();
            if (objN instanceof Z) {
                Z z4 = (Z) objN;
                if (!z4.isActive()) {
                    e0(z4);
                } else if (androidx.concurrent.futures.a.a(f639b, this, objN, t0VarW)) {
                    return t0VarW;
                }
            } else {
                if (!(objN instanceof InterfaceC0126i0)) {
                    if (z3) {
                        C0143y c0143y = objN instanceof C0143y ? (C0143y) objN : null;
                        lVar.invoke(c0143y != null ? c0143y.f667a : null);
                    }
                    return A0.f573b;
                }
                z0 z0VarA = ((InterfaceC0126i0) objN).a();
                if (z0VarA != null) {
                    X x2 = A0.f573b;
                    if (z2 && (objN instanceof c)) {
                        synchronized (objN) {
                            try {
                                thE = ((c) objN).e();
                                if (thE == null || ((lVar instanceof r) && !((c) objN).g())) {
                                    if (l(objN, z0VarA, t0VarW)) {
                                        if (thE == null) {
                                            return t0VarW;
                                        }
                                        x2 = t0VarW;
                                    }
                                }
                                n1.s sVar = n1.s.f5871a;
                            } catch (Throwable th) {
                                throw th;
                            }
                        }
                    }
                    if (thE != null) {
                        if (z3) {
                            lVar.invoke(thE);
                        }
                        return x2;
                    }
                    if (l(objN, z0VarA, t0VarW)) {
                        return t0VarW;
                    }
                } else {
                    if (objN == null) {
                        throw new NullPointerException("null cannot be cast to non-null type kotlinx.coroutines.JobNode");
                    }
                    f0((t0) objN);
                }
            }
        }
    }

    public final String m0() {
        return X() + '{' + j0(N()) + '}';
    }

    @Override // q1.g
    public q1.g minusKey(g.c cVar) {
        return n0.a.e(this, cVar);
    }

    @Override // F1.C0
    public CancellationException n() {
        Throwable thE;
        Object objN = N();
        if (objN instanceof c) {
            thE = ((c) objN).e();
        } else if (objN instanceof C0143y) {
            thE = ((C0143y) objN).f667a;
        } else {
            if (objN instanceof InterfaceC0126i0) {
                throw new IllegalStateException(kotlin.jvm.internal.l.l("Cannot be cancelling child in this state: ", objN).toString());
            }
            thE = null;
        }
        CancellationException cancellationException = thE instanceof CancellationException ? (CancellationException) thE : null;
        return cancellationException == null ? new o0(kotlin.jvm.internal.l.l("Parent job is ", j0(objN)), thE, this) : cancellationException;
    }

    protected void p(Object obj) {
    }

    @Override // q1.g
    public q1.g plus(q1.g gVar) {
        return n0.a.f(this, gVar);
    }

    public final Object r(q1.d dVar) throws Throwable {
        Object objN;
        do {
            objN = N();
            if (!(objN instanceof InterfaceC0126i0)) {
                if (objN instanceof C0143y) {
                    throw ((C0143y) objN).f667a;
                }
                return v0.h(objN);
            }
        } while (i0(objN) < 0);
        return u(dVar);
    }

    @Override // F1.n0
    public void s(CancellationException cancellationException) throws Throwable {
        if (cancellationException == null) {
            cancellationException = new o0(A(), null, this);
        }
        x(cancellationException);
    }

    @Override // F1.n0
    public final boolean start() {
        int iI0;
        do {
            iI0 = i0(N());
            if (iI0 == 0) {
                return false;
            }
        } while (iI0 != 1);
        return true;
    }

    @Override // F1.n0
    public final InterfaceC0136q t(InterfaceC0137s interfaceC0137s) {
        return (InterfaceC0136q) n0.a.d(this, true, false, new r(interfaceC0137s), 2, null);
    }

    public String toString() {
        return m0() + '@' + L.b(this);
    }

    public final boolean v(Throwable th) {
        return w(th);
    }

    public final boolean w(Object obj) throws Throwable {
        Object objU = v0.f652a;
        if (K() && (objU = y(obj)) == v0.f653b) {
            return true;
        }
        if (objU == v0.f652a) {
            objU = U(obj);
        }
        if (objU == v0.f652a || objU == v0.f653b) {
            return true;
        }
        if (objU == v0.f655d) {
            return false;
        }
        p(objU);
        return true;
    }

    public void x(Throwable th) throws Throwable {
        w(th);
    }
}
