package tp;

import ca.canadapost.core.data.auth.model.ProofingProfileDTOKt;
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.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import tp.u1;
import vm.g;
import zp.s;

/* loaded from: classes3.dex */
public class c2 implements u1, w, l2 {

    /* renamed from: p, reason: collision with root package name */
    private static final AtomicReferenceFieldUpdater f36496p = AtomicReferenceFieldUpdater.newUpdater(c2.class, Object.class, "_state");

    /* renamed from: q, reason: collision with root package name */
    private static final AtomicReferenceFieldUpdater f36497q = AtomicReferenceFieldUpdater.newUpdater(c2.class, Object.class, "_parentHandle");
    private volatile Object _parentHandle;
    private volatile Object _state;

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes3.dex */
    public static final class a extends p {

        /* renamed from: x, reason: collision with root package name */
        private final c2 f36498x;

        public a(vm.d dVar, c2 c2Var) {
            super(dVar, 1);
            this.f36498x = c2Var;
        }

        @Override // tp.p
        protected String E() {
            return "AwaitContinuation";
        }

        @Override // tp.p
        public Throwable p(u1 u1Var) {
            Throwable f10;
            Object n02 = this.f36498x.n0();
            return (!(n02 instanceof c) || (f10 = ((c) n02).f()) == null) ? n02 instanceof c0 ? ((c0) n02).f36494a : u1Var.t() : f10;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes3.dex */
    public static final class b extends b2 {

        /* renamed from: t, reason: collision with root package name */
        private final c2 f36499t;

        /* renamed from: u, reason: collision with root package name */
        private final c f36500u;

        /* renamed from: v, reason: collision with root package name */
        private final v f36501v;

        /* renamed from: w, reason: collision with root package name */
        private final Object f36502w;

        public b(c2 c2Var, c cVar, v vVar, Object obj) {
            this.f36499t = c2Var;
            this.f36500u = cVar;
            this.f36501v = vVar;
            this.f36502w = obj;
        }

        @Override // dn.l
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            x((Throwable) obj);
            return rm.n0.f34904a;
        }

        @Override // tp.e0
        public void x(Throwable th2) {
            this.f36499t.c0(this.f36500u, this.f36501v, this.f36502w);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes3.dex */
    public static final class c implements p1 {

        /* renamed from: q, reason: collision with root package name */
        private static final AtomicIntegerFieldUpdater f36503q = AtomicIntegerFieldUpdater.newUpdater(c.class, "_isCompleting");

        /* renamed from: r, reason: collision with root package name */
        private static final AtomicReferenceFieldUpdater f36504r = AtomicReferenceFieldUpdater.newUpdater(c.class, Object.class, "_rootCause");

        /* renamed from: s, reason: collision with root package name */
        private static final AtomicReferenceFieldUpdater f36505s = AtomicReferenceFieldUpdater.newUpdater(c.class, Object.class, "_exceptionsHolder");
        private volatile Object _exceptionsHolder;
        private volatile int _isCompleting;
        private volatile Object _rootCause;

        /* renamed from: p, reason: collision with root package name */
        private final h2 f36506p;

        public c(h2 h2Var, boolean z10, Throwable th2) {
            this.f36506p = h2Var;
            this._isCompleting = z10 ? 1 : 0;
            this._rootCause = th2;
        }

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

        private final Object e() {
            return f36505s.get(this);
        }

        private final void l(Object obj) {
            f36505s.set(this, obj);
        }

        public final void a(Throwable th2) {
            Throwable f10 = f();
            if (f10 == null) {
                m(th2);
                return;
            }
            if (th2 == f10) {
                return;
            }
            Object e10 = e();
            if (e10 == null) {
                l(th2);
                return;
            }
            if (e10 instanceof Throwable) {
                if (th2 == e10) {
                    return;
                }
                ArrayList d10 = d();
                d10.add(e10);
                d10.add(th2);
                l(d10);
                return;
            }
            if (e10 instanceof ArrayList) {
                ((ArrayList) e10).add(th2);
                return;
            }
            throw new IllegalStateException(("State is " + e10).toString());
        }

        @Override // tp.p1
        public boolean b() {
            return f() == null;
        }

        @Override // tp.p1
        public h2 c() {
            return this.f36506p;
        }

        public final Throwable f() {
            return (Throwable) f36504r.get(this);
        }

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

        public final boolean h() {
            return f36503q.get(this) != 0;
        }

        public final boolean i() {
            zp.h0 h0Var;
            Object e10 = e();
            h0Var = d2.f36522e;
            return e10 == h0Var;
        }

        public final List j(Throwable th2) {
            ArrayList arrayList;
            zp.h0 h0Var;
            Object e10 = e();
            if (e10 == null) {
                arrayList = d();
            } else if (e10 instanceof Throwable) {
                ArrayList d10 = d();
                d10.add(e10);
                arrayList = d10;
            } else {
                if (!(e10 instanceof ArrayList)) {
                    throw new IllegalStateException(("State is " + e10).toString());
                }
                arrayList = (ArrayList) e10;
            }
            Throwable f10 = f();
            if (f10 != null) {
                arrayList.add(0, f10);
            }
            if (th2 != null && !kotlin.jvm.internal.t.b(th2, f10)) {
                arrayList.add(th2);
            }
            h0Var = d2.f36522e;
            l(h0Var);
            return arrayList;
        }

        public final void k(boolean z10) {
            f36503q.set(this, z10 ? 1 : 0);
        }

        public final void m(Throwable th2) {
            f36504r.set(this, th2);
        }

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

    /* loaded from: classes3.dex */
    public static final class d extends s.a {

        /* renamed from: d, reason: collision with root package name */
        final /* synthetic */ c2 f36507d;

        /* renamed from: e, reason: collision with root package name */
        final /* synthetic */ Object f36508e;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public d(zp.s sVar, c2 c2Var, Object obj) {
            super(sVar);
            this.f36507d = c2Var;
            this.f36508e = obj;
        }

        @Override // zp.b
        /* renamed from: f, reason: merged with bridge method [inline-methods] */
        public Object d(zp.s sVar) {
            if (this.f36507d.n0() == this.f36508e) {
                return null;
            }
            return zp.r.a();
        }
    }

    /* loaded from: classes3.dex */
    static final class e extends kotlin.coroutines.jvm.internal.k implements dn.p {

        /* renamed from: q, reason: collision with root package name */
        Object f36509q;

        /* renamed from: r, reason: collision with root package name */
        Object f36510r;

        /* renamed from: s, reason: collision with root package name */
        int f36511s;

        /* renamed from: t, reason: collision with root package name */
        private /* synthetic */ Object f36512t;

        e(vm.d dVar) {
            super(2, dVar);
        }

        @Override // dn.p
        /* renamed from: b, reason: merged with bridge method [inline-methods] */
        public final Object invoke(rp.j jVar, vm.d dVar) {
            return ((e) create(jVar, dVar)).invokeSuspend(rm.n0.f34904a);
        }

        @Override // kotlin.coroutines.jvm.internal.a
        public final vm.d create(Object obj, vm.d dVar) {
            e eVar = new e(dVar);
            eVar.f36512t = obj;
            return eVar;
        }

        /* JADX WARN: Removed duplicated region for block: B:9:0x0069  */
        /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:10:0x006b -> B:6:0x0081). Please report as a decompilation issue!!! */
        /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:12:0x007e -> B:6:0x0081). Please report as a decompilation issue!!! */
        @Override // kotlin.coroutines.jvm.internal.a
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final Object invokeSuspend(Object obj) {
            Object f10;
            h2 c10;
            rp.j jVar;
            zp.q qVar;
            zp.s sVar;
            f10 = wm.d.f();
            int i10 = this.f36511s;
            if (i10 == 0) {
                rm.y.b(obj);
                rp.j jVar2 = (rp.j) this.f36512t;
                Object n02 = c2.this.n0();
                if (n02 instanceof v) {
                    w wVar = ((v) n02).f36594t;
                    this.f36511s = 1;
                    if (jVar2.a(wVar, this) == f10) {
                        return f10;
                    }
                } else if ((n02 instanceof p1) && (c10 = ((p1) n02).c()) != null) {
                    Object p10 = c10.p();
                    kotlin.jvm.internal.t.e(p10, "null cannot be cast to non-null type kotlinx.coroutines.internal.LockFreeLinkedListNode{ kotlinx.coroutines.internal.LockFreeLinkedListKt.Node }");
                    zp.s sVar2 = (zp.s) p10;
                    jVar = jVar2;
                    qVar = c10;
                    sVar = sVar2;
                    if (!kotlin.jvm.internal.t.b(sVar, qVar)) {
                    }
                }
            } else if (i10 == 1) {
                rm.y.b(obj);
            } else {
                if (i10 != 2) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                sVar = (zp.s) this.f36510r;
                qVar = (zp.q) this.f36509q;
                jVar = (rp.j) this.f36512t;
                rm.y.b(obj);
                sVar = sVar.q();
                if (!kotlin.jvm.internal.t.b(sVar, qVar)) {
                    if (sVar instanceof v) {
                        w wVar2 = ((v) sVar).f36594t;
                        this.f36512t = jVar;
                        this.f36509q = qVar;
                        this.f36510r = sVar;
                        this.f36511s = 2;
                        if (jVar.a(wVar2, this) == f10) {
                            return f10;
                        }
                    }
                    sVar = sVar.q();
                    if (!kotlin.jvm.internal.t.b(sVar, qVar)) {
                    }
                }
            }
            return rm.n0.f34904a;
        }
    }

    public c2(boolean z10) {
        this._state = z10 ? d2.f36524g : d2.f36523f;
    }

    private final void A0(h2 h2Var, Throwable th2) {
        C0(th2);
        Object p10 = h2Var.p();
        kotlin.jvm.internal.t.e(p10, "null cannot be cast to non-null type kotlinx.coroutines.internal.LockFreeLinkedListNode{ kotlinx.coroutines.internal.LockFreeLinkedListKt.Node }");
        f0 f0Var = null;
        for (zp.s sVar = (zp.s) p10; !kotlin.jvm.internal.t.b(sVar, h2Var); sVar = sVar.q()) {
            if (sVar instanceof w1) {
                b2 b2Var = (b2) sVar;
                try {
                    b2Var.x(th2);
                } catch (Throwable th3) {
                    if (f0Var != null) {
                        rm.h.a(f0Var, th3);
                    } else {
                        f0Var = new f0("Exception in completion handler " + b2Var + " for " + this, th3);
                        rm.n0 n0Var = rm.n0.f34904a;
                    }
                }
            }
        }
        if (f0Var != null) {
            p0(f0Var);
        }
        Y(th2);
    }

    private final void B0(h2 h2Var, Throwable th2) {
        Object p10 = h2Var.p();
        kotlin.jvm.internal.t.e(p10, "null cannot be cast to non-null type kotlinx.coroutines.internal.LockFreeLinkedListNode{ kotlinx.coroutines.internal.LockFreeLinkedListKt.Node }");
        f0 f0Var = null;
        for (zp.s sVar = (zp.s) p10; !kotlin.jvm.internal.t.b(sVar, h2Var); sVar = sVar.q()) {
            if (sVar instanceof b2) {
                b2 b2Var = (b2) sVar;
                try {
                    b2Var.x(th2);
                } catch (Throwable th3) {
                    if (f0Var != null) {
                        rm.h.a(f0Var, th3);
                    } else {
                        f0Var = new f0("Exception in completion handler " + b2Var + " for " + this, th3);
                        rm.n0 n0Var = rm.n0.f34904a;
                    }
                }
            }
        }
        if (f0Var != null) {
            p0(f0Var);
        }
    }

    private final boolean D(Object obj, h2 h2Var, b2 b2Var) {
        int w10;
        d dVar = new d(b2Var, this, obj);
        do {
            w10 = h2Var.r().w(b2Var, h2Var, dVar);
            if (w10 == 1) {
                return true;
            }
        } while (w10 != 2);
        return false;
    }

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

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r1v2, types: [tp.o1] */
    private final void F0(d1 d1Var) {
        h2 h2Var = new h2();
        if (!d1Var.b()) {
            h2Var = new o1(h2Var);
        }
        androidx.concurrent.futures.b.a(f36496p, this, d1Var, h2Var);
    }

    private final void G0(b2 b2Var) {
        b2Var.i(new h2());
        androidx.concurrent.futures.b.a(f36496p, this, b2Var, b2Var.q());
    }

    private final int J0(Object obj) {
        d1 d1Var;
        if (!(obj instanceof d1)) {
            if (!(obj instanceof o1)) {
                return 0;
            }
            if (!androidx.concurrent.futures.b.a(f36496p, this, obj, ((o1) obj).c())) {
                return -1;
            }
            E0();
            return 1;
        }
        if (((d1) obj).b()) {
            return 0;
        }
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f36496p;
        d1Var = d2.f36524g;
        if (!androidx.concurrent.futures.b.a(atomicReferenceFieldUpdater, this, obj, d1Var)) {
            return -1;
        }
        E0();
        return 1;
    }

    private final String K0(Object obj) {
        if (!(obj instanceof c)) {
            return obj instanceof p1 ? ((p1) obj).b() ? ProofingProfileDTOKt.PROOFING_PROFILE_ACTIVE : "New" : obj instanceof c0 ? "Cancelled" : "Completed";
        }
        c cVar = (c) obj;
        return cVar.g() ? "Cancelling" : cVar.h() ? "Completing" : ProofingProfileDTOKt.PROOFING_PROFILE_ACTIVE;
    }

    public static /* synthetic */ CancellationException M0(c2 c2Var, Throwable th2, String str, int i10, Object obj) {
        if (obj != null) {
            throw new UnsupportedOperationException("Super calls with default arguments not supported in this target, function: toCancellationException");
        }
        if ((i10 & 1) != 0) {
            str = null;
        }
        return c2Var.L0(th2, str);
    }

    private final Object O(vm.d dVar) {
        vm.d d10;
        Object f10;
        d10 = wm.c.d(dVar);
        a aVar = new a(d10, this);
        aVar.u();
        r.a(aVar, T(new m2(aVar)));
        Object r10 = aVar.r();
        f10 = wm.d.f();
        if (r10 == f10) {
            kotlin.coroutines.jvm.internal.h.c(dVar);
        }
        return r10;
    }

    private final boolean O0(p1 p1Var, Object obj) {
        if (!androidx.concurrent.futures.b.a(f36496p, this, p1Var, d2.g(obj))) {
            return false;
        }
        C0(null);
        D0(obj);
        b0(p1Var, obj);
        return true;
    }

    private final boolean P0(p1 p1Var, Throwable th2) {
        h2 l02 = l0(p1Var);
        if (l02 == null) {
            return false;
        }
        if (!androidx.concurrent.futures.b.a(f36496p, this, p1Var, new c(l02, false, th2))) {
            return false;
        }
        A0(l02, th2);
        return true;
    }

    private final Object Q0(Object obj, Object obj2) {
        zp.h0 h0Var;
        zp.h0 h0Var2;
        if (!(obj instanceof p1)) {
            h0Var2 = d2.f36518a;
            return h0Var2;
        }
        if ((!(obj instanceof d1) && !(obj instanceof b2)) || (obj instanceof v) || (obj2 instanceof c0)) {
            return R0((p1) obj, obj2);
        }
        if (O0((p1) obj, obj2)) {
            return obj2;
        }
        h0Var = d2.f36520c;
        return h0Var;
    }

    private final Object R0(p1 p1Var, Object obj) {
        zp.h0 h0Var;
        zp.h0 h0Var2;
        zp.h0 h0Var3;
        h2 l02 = l0(p1Var);
        if (l02 == null) {
            h0Var3 = d2.f36520c;
            return h0Var3;
        }
        c cVar = p1Var instanceof c ? (c) p1Var : null;
        if (cVar == null) {
            cVar = new c(l02, false, null);
        }
        kotlin.jvm.internal.o0 o0Var = new kotlin.jvm.internal.o0();
        synchronized (cVar) {
            if (cVar.h()) {
                h0Var2 = d2.f36518a;
                return h0Var2;
            }
            cVar.k(true);
            if (cVar != p1Var && !androidx.concurrent.futures.b.a(f36496p, this, p1Var, cVar)) {
                h0Var = d2.f36520c;
                return h0Var;
            }
            boolean g10 = cVar.g();
            c0 c0Var = obj instanceof c0 ? (c0) obj : null;
            if (c0Var != null) {
                cVar.a(c0Var.f36494a);
            }
            Throwable f10 = Boolean.valueOf(g10 ? false : true).booleanValue() ? cVar.f() : null;
            o0Var.f26199p = f10;
            rm.n0 n0Var = rm.n0.f34904a;
            if (f10 != null) {
                A0(l02, f10);
            }
            v f02 = f0(p1Var);
            return (f02 == null || !S0(cVar, f02, obj)) ? e0(cVar, obj) : d2.f36519b;
        }
    }

    private final boolean S0(c cVar, v vVar, Object obj) {
        while (u1.a.d(vVar.f36594t, false, false, new b(this, cVar, vVar, obj), 1, null) == j2.f36560p) {
            vVar = z0(vVar);
            if (vVar == null) {
                return false;
            }
        }
        return true;
    }

    private final Object X(Object obj) {
        zp.h0 h0Var;
        Object Q0;
        zp.h0 h0Var2;
        do {
            Object n02 = n0();
            if (!(n02 instanceof p1) || ((n02 instanceof c) && ((c) n02).h())) {
                h0Var = d2.f36518a;
                return h0Var;
            }
            Q0 = Q0(n02, new c0(d0(obj), false, 2, null));
            h0Var2 = d2.f36520c;
        } while (Q0 == h0Var2);
        return Q0;
    }

    private final boolean Y(Throwable th2) {
        if (r0()) {
            return true;
        }
        boolean z10 = th2 instanceof CancellationException;
        u m02 = m0();
        return (m02 == null || m02 == j2.f36560p) ? z10 : m02.g(th2) || z10;
    }

    private final void b0(p1 p1Var, Object obj) {
        u m02 = m0();
        if (m02 != null) {
            m02.a();
            I0(j2.f36560p);
        }
        c0 c0Var = obj instanceof c0 ? (c0) obj : null;
        Throwable th2 = c0Var != null ? c0Var.f36494a : null;
        if (!(p1Var instanceof b2)) {
            h2 c10 = p1Var.c();
            if (c10 != null) {
                B0(c10, th2);
                return;
            }
            return;
        }
        try {
            ((b2) p1Var).x(th2);
        } catch (Throwable th3) {
            p0(new f0("Exception in completion handler " + p1Var + " for " + this, th3));
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void c0(c cVar, v vVar, Object obj) {
        v z02 = z0(vVar);
        if (z02 == null || !S0(cVar, z02, obj)) {
            F(e0(cVar, obj));
        }
    }

    private final Throwable d0(Object obj) {
        if (obj == null ? true : obj instanceof Throwable) {
            Throwable th2 = (Throwable) obj;
            return th2 == null ? new v1(Z(), null, this) : th2;
        }
        kotlin.jvm.internal.t.e(obj, "null cannot be cast to non-null type kotlinx.coroutines.ParentJob");
        return ((l2) obj).K();
    }

    private final Object e0(c cVar, Object obj) {
        boolean g10;
        Throwable i02;
        c0 c0Var = obj instanceof c0 ? (c0) obj : null;
        Throwable th2 = c0Var != null ? c0Var.f36494a : null;
        synchronized (cVar) {
            g10 = cVar.g();
            List j10 = cVar.j(th2);
            i02 = i0(cVar, j10);
            if (i02 != null) {
                E(i02, j10);
            }
        }
        if (i02 != null && i02 != th2) {
            obj = new c0(i02, false, 2, null);
        }
        if (i02 != null) {
            if (Y(i02) || o0(i02)) {
                kotlin.jvm.internal.t.e(obj, "null cannot be cast to non-null type kotlinx.coroutines.CompletedExceptionally");
                ((c0) obj).b();
            }
        }
        if (!g10) {
            C0(i02);
        }
        D0(obj);
        androidx.concurrent.futures.b.a(f36496p, this, cVar, d2.g(obj));
        b0(cVar, obj);
        return obj;
    }

    private final v f0(p1 p1Var) {
        v vVar = p1Var instanceof v ? (v) p1Var : null;
        if (vVar != null) {
            return vVar;
        }
        h2 c10 = p1Var.c();
        if (c10 != null) {
            return z0(c10);
        }
        return null;
    }

    private final Throwable h0(Object obj) {
        c0 c0Var = obj instanceof c0 ? (c0) obj : null;
        if (c0Var != null) {
            return c0Var.f36494a;
        }
        return null;
    }

    private final Throwable i0(c cVar, List list) {
        Object obj;
        Object obj2 = null;
        if (list.isEmpty()) {
            if (cVar.g()) {
                return new v1(Z(), null, this);
            }
            return null;
        }
        List list2 = list;
        Iterator it = list2.iterator();
        while (true) {
            if (!it.hasNext()) {
                obj = null;
                break;
            }
            obj = it.next();
            if (!(((Throwable) obj) instanceof CancellationException)) {
                break;
            }
        }
        Throwable th2 = (Throwable) obj;
        if (th2 != null) {
            return th2;
        }
        Throwable th3 = (Throwable) list.get(0);
        if (th3 instanceof u2) {
            Iterator it2 = list2.iterator();
            while (true) {
                if (!it2.hasNext()) {
                    break;
                }
                Object next = it2.next();
                Throwable th4 = (Throwable) next;
                if (th4 != th3 && (th4 instanceof u2)) {
                    obj2 = next;
                    break;
                }
            }
            Throwable th5 = (Throwable) obj2;
            if (th5 != null) {
                return th5;
            }
        }
        return th3;
    }

    private final h2 l0(p1 p1Var) {
        h2 c10 = p1Var.c();
        if (c10 != null) {
            return c10;
        }
        if (p1Var instanceof d1) {
            return new h2();
        }
        if (p1Var instanceof b2) {
            G0((b2) p1Var);
            return null;
        }
        throw new IllegalStateException(("State should have list: " + p1Var).toString());
    }

    private final boolean s0() {
        Object n02;
        do {
            n02 = n0();
            if (!(n02 instanceof p1)) {
                return false;
            }
        } while (J0(n02) < 0);
        return true;
    }

    private final Object t0(vm.d dVar) {
        vm.d d10;
        Object f10;
        Object f11;
        d10 = wm.c.d(dVar);
        p pVar = new p(d10, 1);
        pVar.u();
        r.a(pVar, T(new n2(pVar)));
        Object r10 = pVar.r();
        f10 = wm.d.f();
        if (r10 == f10) {
            kotlin.coroutines.jvm.internal.h.c(dVar);
        }
        f11 = wm.d.f();
        return r10 == f11 ? r10 : rm.n0.f34904a;
    }

    private final Object u0(Object obj) {
        zp.h0 h0Var;
        zp.h0 h0Var2;
        zp.h0 h0Var3;
        zp.h0 h0Var4;
        zp.h0 h0Var5;
        zp.h0 h0Var6;
        Throwable th2 = null;
        while (true) {
            Object n02 = n0();
            if (n02 instanceof c) {
                synchronized (n02) {
                    if (((c) n02).i()) {
                        h0Var2 = d2.f36521d;
                        return h0Var2;
                    }
                    boolean g10 = ((c) n02).g();
                    if (obj != null || !g10) {
                        if (th2 == null) {
                            th2 = d0(obj);
                        }
                        ((c) n02).a(th2);
                    }
                    Throwable f10 = g10 ^ true ? ((c) n02).f() : null;
                    if (f10 != null) {
                        A0(((c) n02).c(), f10);
                    }
                    h0Var = d2.f36518a;
                    return h0Var;
                }
            }
            if (!(n02 instanceof p1)) {
                h0Var3 = d2.f36521d;
                return h0Var3;
            }
            if (th2 == null) {
                th2 = d0(obj);
            }
            p1 p1Var = (p1) n02;
            if (!p1Var.b()) {
                Object Q0 = Q0(n02, new c0(th2, false, 2, null));
                h0Var5 = d2.f36518a;
                if (Q0 == h0Var5) {
                    throw new IllegalStateException(("Cannot happen in " + n02).toString());
                }
                h0Var6 = d2.f36520c;
                if (Q0 != h0Var6) {
                    return Q0;
                }
            } else if (P0(p1Var, th2)) {
                h0Var4 = d2.f36518a;
                return h0Var4;
            }
        }
    }

    private final b2 x0(dn.l lVar, boolean z10) {
        b2 b2Var;
        if (z10) {
            b2Var = lVar instanceof w1 ? (w1) lVar : null;
            if (b2Var == null) {
                b2Var = new s1(lVar);
            }
        } else {
            b2Var = lVar instanceof b2 ? (b2) lVar : null;
            if (b2Var == null) {
                b2Var = new t1(lVar);
            }
        }
        b2Var.z(this);
        return b2Var;
    }

    private final v z0(zp.s sVar) {
        while (sVar.s()) {
            sVar = sVar.r();
        }
        while (true) {
            sVar = sVar.q();
            if (!sVar.s()) {
                if (sVar instanceof v) {
                    return (v) sVar;
                }
                if (sVar instanceof h2) {
                    return null;
                }
            }
        }
    }

    protected void C0(Throwable th2) {
    }

    protected void D0(Object obj) {
    }

    protected void E0() {
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public void F(Object obj) {
    }

    @Override // tp.u1
    public final u G(w wVar) {
        b1 d10 = u1.a.d(this, true, false, new v(wVar), 2, null);
        kotlin.jvm.internal.t.e(d10, "null cannot be cast to non-null type kotlinx.coroutines.ChildHandle");
        return (u) d10;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public final Object H(vm.d dVar) {
        Object n02;
        do {
            n02 = n0();
            if (!(n02 instanceof p1)) {
                if (n02 instanceof c0) {
                    throw ((c0) n02).f36494a;
                }
                return d2.h(n02);
            }
        } while (J0(n02) < 0);
        return O(dVar);
    }

    public final void H0(b2 b2Var) {
        Object n02;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        d1 d1Var;
        do {
            n02 = n0();
            if (!(n02 instanceof b2)) {
                if (!(n02 instanceof p1) || ((p1) n02).c() == null) {
                    return;
                }
                b2Var.t();
                return;
            }
            if (n02 != b2Var) {
                return;
            }
            atomicReferenceFieldUpdater = f36496p;
            d1Var = d2.f36524g;
        } while (!androidx.concurrent.futures.b.a(atomicReferenceFieldUpdater, this, n02, d1Var));
    }

    public final void I0(u uVar) {
        f36497q.set(this, uVar);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r1v11, types: [java.lang.Throwable] */
    /* JADX WARN: Type inference failed for: r1v7, types: [java.lang.Throwable] */
    @Override // tp.l2
    public CancellationException K() {
        CancellationException cancellationException;
        Object n02 = n0();
        if (n02 instanceof c) {
            cancellationException = ((c) n02).f();
        } else if (n02 instanceof c0) {
            cancellationException = ((c0) n02).f36494a;
        } else {
            if (n02 instanceof p1) {
                throw new IllegalStateException(("Cannot be cancelling child in this state: " + n02).toString());
            }
            cancellationException = null;
        }
        CancellationException cancellationException2 = cancellationException instanceof CancellationException ? cancellationException : null;
        if (cancellationException2 != null) {
            return cancellationException2;
        }
        return new v1("Parent job is " + K0(n02), cancellationException, this);
    }

    protected final CancellationException L0(Throwable th2, String str) {
        CancellationException cancellationException = th2 instanceof CancellationException ? (CancellationException) th2 : null;
        if (cancellationException == null) {
            if (str == null) {
                str = Z();
            }
            cancellationException = new v1(str, th2, this);
        }
        return cancellationException;
    }

    @Override // tp.u1
    public final Object N(vm.d dVar) {
        Object f10;
        if (!s0()) {
            y1.j(dVar.getContext());
            return rm.n0.f34904a;
        }
        Object t02 = t0(dVar);
        f10 = wm.d.f();
        return t02 == f10 ? t02 : rm.n0.f34904a;
    }

    public final String N0() {
        return y0() + '{' + K0(n0()) + '}';
    }

    public final boolean P(Throwable th2) {
        return U(th2);
    }

    public final boolean R() {
        return !(n0() instanceof p1);
    }

    @Override // tp.u1
    public final b1 T(dn.l lVar) {
        return m(false, true, lVar);
    }

    public final boolean U(Object obj) {
        Object obj2;
        zp.h0 h0Var;
        zp.h0 h0Var2;
        zp.h0 h0Var3;
        obj2 = d2.f36518a;
        if (k0() && (obj2 = X(obj)) == d2.f36519b) {
            return true;
        }
        h0Var = d2.f36518a;
        if (obj2 == h0Var) {
            obj2 = u0(obj);
        }
        h0Var2 = d2.f36518a;
        if (obj2 == h0Var2 || obj2 == d2.f36519b) {
            return true;
        }
        h0Var3 = d2.f36521d;
        if (obj2 == h0Var3) {
            return false;
        }
        F(obj2);
        return true;
    }

    public void V(Throwable th2) {
        U(th2);
    }

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

    public boolean a0(Throwable th2) {
        if (th2 instanceof CancellationException) {
            return true;
        }
        return U(th2) && j0();
    }

    @Override // tp.u1
    public boolean b() {
        Object n02 = n0();
        return (n02 instanceof p1) && ((p1) n02).b();
    }

    @Override // tp.u1
    public void c(CancellationException cancellationException) {
        if (cancellationException == null) {
            cancellationException = new v1(Z(), null, this);
        }
        V(cancellationException);
    }

    @Override // vm.g.b, vm.g
    public g.b d(g.c cVar) {
        return u1.a.c(this, cVar);
    }

    public final Object g0() {
        Object n02 = n0();
        if (!(!(n02 instanceof p1))) {
            throw new IllegalStateException("This job has not completed yet".toString());
        }
        if (n02 instanceof c0) {
            throw ((c0) n02).f36494a;
        }
        return d2.h(n02);
    }

    @Override // vm.g.b
    public final g.c getKey() {
        return u1.f36592j;
    }

    @Override // tp.u1
    public u1 getParent() {
        u m02 = m0();
        if (m02 != null) {
            return m02.getParent();
        }
        return null;
    }

    @Override // vm.g
    public vm.g h(vm.g gVar) {
        return u1.a.f(this, gVar);
    }

    @Override // tp.u1
    public final boolean isCancelled() {
        Object n02 = n0();
        return (n02 instanceof c0) || ((n02 instanceof c) && ((c) n02).g());
    }

    public boolean j0() {
        return true;
    }

    public boolean k0() {
        return false;
    }

    @Override // tp.u1
    public final b1 m(boolean z10, boolean z11, dn.l lVar) {
        b2 x02 = x0(lVar, z10);
        while (true) {
            Object n02 = n0();
            if (n02 instanceof d1) {
                d1 d1Var = (d1) n02;
                if (!d1Var.b()) {
                    F0(d1Var);
                } else if (androidx.concurrent.futures.b.a(f36496p, this, n02, x02)) {
                    return x02;
                }
            } else {
                if (!(n02 instanceof p1)) {
                    if (z11) {
                        c0 c0Var = n02 instanceof c0 ? (c0) n02 : null;
                        lVar.invoke(c0Var != null ? c0Var.f36494a : null);
                    }
                    return j2.f36560p;
                }
                h2 c10 = ((p1) n02).c();
                if (c10 == null) {
                    kotlin.jvm.internal.t.e(n02, "null cannot be cast to non-null type kotlinx.coroutines.JobNode");
                    G0((b2) n02);
                } else {
                    b1 b1Var = j2.f36560p;
                    if (z10 && (n02 instanceof c)) {
                        synchronized (n02) {
                            r3 = ((c) n02).f();
                            if (r3 == null || ((lVar instanceof v) && !((c) n02).h())) {
                                if (D(n02, c10, x02)) {
                                    if (r3 == null) {
                                        return x02;
                                    }
                                    b1Var = x02;
                                }
                            }
                            rm.n0 n0Var = rm.n0.f34904a;
                        }
                    }
                    if (r3 != null) {
                        if (z11) {
                            lVar.invoke(r3);
                        }
                        return b1Var;
                    }
                    if (D(n02, c10, x02)) {
                        return x02;
                    }
                }
            }
        }
    }

    public final u m0() {
        return (u) f36497q.get(this);
    }

    public final Object n0() {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f36496p;
        while (true) {
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (!(obj instanceof zp.a0)) {
                return obj;
            }
            ((zp.a0) obj).a(this);
        }
    }

    protected boolean o0(Throwable th2) {
        return false;
    }

    @Override // tp.u1
    public final rp.h p() {
        rp.h b10;
        b10 = rp.l.b(new e(null));
        return b10;
    }

    public void p0(Throwable th2) {
        throw th2;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public final void q0(u1 u1Var) {
        if (u1Var == null) {
            I0(j2.f36560p);
            return;
        }
        u1Var.start();
        u G = u1Var.G(this);
        I0(G);
        if (R()) {
            G.a();
            I0(j2.f36560p);
        }
    }

    @Override // vm.g
    public vm.g r(g.c cVar) {
        return u1.a.e(this, cVar);
    }

    protected boolean r0() {
        return false;
    }

    @Override // tp.u1
    public final boolean start() {
        int J0;
        do {
            J0 = J0(n0());
            if (J0 == 0) {
                return false;
            }
        } while (J0 != 1);
        return true;
    }

    @Override // tp.u1
    public final CancellationException t() {
        Object n02 = n0();
        if (!(n02 instanceof c)) {
            if (n02 instanceof p1) {
                throw new IllegalStateException(("Job is still new or active: " + this).toString());
            }
            if (n02 instanceof c0) {
                return M0(this, ((c0) n02).f36494a, null, 1, null);
            }
            return new v1(q0.a(this) + " has completed normally", null, this);
        }
        Throwable f10 = ((c) n02).f();
        if (f10 != null) {
            CancellationException L0 = L0(f10, q0.a(this) + " is cancelling");
            if (L0 != null) {
                return L0;
            }
        }
        throw new IllegalStateException(("Job is still new or active: " + this).toString());
    }

    public String toString() {
        return N0() + '@' + q0.b(this);
    }

    @Override // tp.w
    public final void v(l2 l2Var) {
        U(l2Var);
    }

    public final boolean v0(Object obj) {
        Object Q0;
        zp.h0 h0Var;
        zp.h0 h0Var2;
        do {
            Q0 = Q0(n0(), obj);
            h0Var = d2.f36518a;
            if (Q0 == h0Var) {
                return false;
            }
            if (Q0 == d2.f36519b) {
                return true;
            }
            h0Var2 = d2.f36520c;
        } while (Q0 == h0Var2);
        F(Q0);
        return true;
    }

    public final Object w0(Object obj) {
        Object Q0;
        zp.h0 h0Var;
        zp.h0 h0Var2;
        do {
            Q0 = Q0(n0(), obj);
            h0Var = d2.f36518a;
            if (Q0 == h0Var) {
                throw new IllegalStateException("Job " + this + " is already complete or completing, but is being completed with " + obj, h0(obj));
            }
            h0Var2 = d2.f36520c;
        } while (Q0 == h0Var2);
        return Q0;
    }

    @Override // vm.g
    public Object y(Object obj, dn.p pVar) {
        return u1.a.b(this, obj, pVar);
    }

    public String y0() {
        return q0.a(this);
    }
}
