package F1;

import F1.n0;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import r1.AbstractC0707b;

/* JADX INFO: renamed from: F1.m, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public class C0132m extends U implements InterfaceC0131l, kotlin.coroutines.jvm.internal.e {

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    private static final /* synthetic */ AtomicIntegerFieldUpdater f624I = AtomicIntegerFieldUpdater.newUpdater(C0132m.class, "_decision");

    /* JADX INFO: renamed from: L, reason: collision with root package name */
    private static final /* synthetic */ AtomicReferenceFieldUpdater f625L = AtomicReferenceFieldUpdater.newUpdater(C0132m.class, Object.class, "_state");

    /* JADX INFO: renamed from: H, reason: collision with root package name */
    private X f626H;
    private volatile /* synthetic */ int _decision;
    private volatile /* synthetic */ Object _state;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    private final q1.d f627x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    private final q1.g f628y;

    public C0132m(q1.d dVar, int i2) {
        super(i2);
        this.f627x = dVar;
        this.f628y = dVar.getContext();
        this._decision = 0;
        this._state = C0115d.f610b;
    }

    private final void A(y1.l lVar, Object obj) {
        throw new IllegalStateException(("It's prohibited to register multiple handlers, tried to register " + lVar + ", already has " + obj).toString());
    }

    private final void D() {
        q1.d dVar = this.f627x;
        kotlinx.coroutines.internal.e eVar = dVar instanceof kotlinx.coroutines.internal.e ? (kotlinx.coroutines.internal.e) dVar : null;
        Throwable thN = eVar != null ? eVar.n(this) : null;
        if (thN == null) {
            return;
        }
        o();
        m(thN);
    }

    private final void E(Object obj, int i2, y1.l lVar) {
        Object obj2;
        do {
            obj2 = this._state;
            if (!(obj2 instanceof B0)) {
                if (obj2 instanceof C0134o) {
                    C0134o c0134o = (C0134o) obj2;
                    if (c0134o.c()) {
                        if (lVar == null) {
                            return;
                        }
                        l(lVar, c0134o.f667a);
                        return;
                    }
                }
                i(obj);
                throw new n1.d();
            }
        } while (!androidx.concurrent.futures.a.a(f625L, this, obj2, G((B0) obj2, obj, i2, lVar, null)));
        p();
        q(i2);
    }

    static /* synthetic */ void F(C0132m c0132m, Object obj, int i2, y1.l lVar, int i3, Object obj2) {
        if (obj2 != null) {
            throw new UnsupportedOperationException("Super calls with default arguments not supported in this target, function: resumeImpl");
        }
        if ((i3 & 4) != 0) {
            lVar = null;
        }
        c0132m.E(obj, i2, lVar);
    }

    private final Object G(B0 b02, Object obj, int i2, y1.l lVar, Object obj2) {
        if (obj instanceof C0143y) {
            return obj;
        }
        if (!V.b(i2) && obj2 == null) {
            return obj;
        }
        if (lVar == null && !(b02 instanceof AbstractC0127j) && obj2 == null) {
            return obj;
        }
        return new C0142x(obj, b02 instanceof AbstractC0127j ? (AbstractC0127j) b02 : null, lVar, obj2, null, 16, null);
    }

    private final boolean H() {
        do {
            int i2 = this._decision;
            if (i2 != 0) {
                if (i2 == 1) {
                    return false;
                }
                throw new IllegalStateException("Already resumed");
            }
        } while (!f624I.compareAndSet(this, 0, 2));
        return true;
    }

    private final boolean I() {
        do {
            int i2 = this._decision;
            if (i2 != 0) {
                if (i2 == 2) {
                    return false;
                }
                throw new IllegalStateException("Already suspended");
            }
        } while (!f624I.compareAndSet(this, 0, 1));
        return true;
    }

    private final Void i(Object obj) {
        throw new IllegalStateException(kotlin.jvm.internal.l.l("Already resumed, but proposed with update ", obj).toString());
    }

    private final void k(y1.l lVar, Throwable th) {
        try {
            lVar.invoke(th);
        } catch (Throwable th2) {
            G.a(getContext(), new B(kotlin.jvm.internal.l.l("Exception in invokeOnCancellation handler for ", this), th2));
        }
    }

    private final boolean n(Throwable th) {
        if (y()) {
            return ((kotlinx.coroutines.internal.e) this.f627x).l(th);
        }
        return false;
    }

    private final void p() {
        if (y()) {
            return;
        }
        o();
    }

    private final void q(int i2) {
        if (H()) {
            return;
        }
        V.a(this, i2);
    }

    private final String u() {
        Object objT = t();
        return objT instanceof B0 ? "Active" : objT instanceof C0134o ? "Cancelled" : "Completed";
    }

    private final X w() {
        n0 n0Var = (n0) getContext().get(n0.f630a);
        if (n0Var == null) {
            return null;
        }
        X xD = n0.a.d(n0Var, true, false, new C0135p(this), 2, null);
        this.f626H = xD;
        return xD;
    }

    private final boolean y() {
        return V.c(this.f591r) && ((kotlinx.coroutines.internal.e) this.f627x).k();
    }

    private final AbstractC0127j z(y1.l lVar) {
        return lVar instanceof AbstractC0127j ? (AbstractC0127j) lVar : new C0130k0(lVar);
    }

    protected String B() {
        return "CancellableContinuation";
    }

    public final void C(Throwable th) {
        if (n(th)) {
            return;
        }
        m(th);
        p();
    }

    @Override // F1.U
    public void a(Object obj, Throwable th) {
        while (true) {
            Object obj2 = this._state;
            if (obj2 instanceof B0) {
                throw new IllegalStateException("Not completed");
            }
            if (obj2 instanceof C0143y) {
                return;
            }
            if (obj2 instanceof C0142x) {
                C0142x c0142x = (C0142x) obj2;
                if (c0142x.c()) {
                    throw new IllegalStateException("Must be called at most once");
                }
                if (androidx.concurrent.futures.a.a(f625L, this, obj2, C0142x.b(c0142x, null, null, null, null, th, 15, null))) {
                    c0142x.d(this, th);
                    return;
                }
            } else if (androidx.concurrent.futures.a.a(f625L, this, obj2, new C0142x(obj2, null, null, null, th, 14, null))) {
                return;
            }
        }
    }

    @Override // F1.InterfaceC0131l
    public void b(E e2, Object obj) {
        q1.d dVar = this.f627x;
        kotlinx.coroutines.internal.e eVar = dVar instanceof kotlinx.coroutines.internal.e ? (kotlinx.coroutines.internal.e) dVar : null;
        F(this, obj, (eVar != null ? eVar.f5149x : null) == e2 ? 4 : this.f591r, null, 4, null);
    }

    @Override // F1.U
    public final q1.d c() {
        return this.f627x;
    }

    @Override // F1.U
    public Throwable d(Object obj) {
        Throwable thD = super.d(obj);
        if (thD == null) {
            return null;
        }
        c();
        return thD;
    }

    @Override // F1.U
    public Object e(Object obj) {
        return obj instanceof C0142x ? ((C0142x) obj).f660a : obj;
    }

    @Override // F1.InterfaceC0131l
    public void g(y1.l lVar) {
        AbstractC0127j abstractC0127jZ = z(lVar);
        while (true) {
            Object obj = this._state;
            if (obj instanceof C0115d) {
                if (androidx.concurrent.futures.a.a(f625L, this, obj, abstractC0127jZ)) {
                    return;
                }
            } else if (obj instanceof AbstractC0127j) {
                A(lVar, obj);
            } else {
                if (obj instanceof C0143y) {
                    C0143y c0143y = (C0143y) obj;
                    if (!c0143y.b()) {
                        A(lVar, obj);
                    }
                    if (obj instanceof C0134o) {
                        if (!(obj instanceof C0143y)) {
                            c0143y = null;
                        }
                        k(lVar, c0143y != null ? c0143y.f667a : null);
                        return;
                    }
                    return;
                }
                if (obj instanceof C0142x) {
                    C0142x c0142x = (C0142x) obj;
                    if (c0142x.f661b != null) {
                        A(lVar, obj);
                    }
                    if (c0142x.c()) {
                        k(lVar, c0142x.f664e);
                        return;
                    } else {
                        if (androidx.concurrent.futures.a.a(f625L, this, obj, C0142x.b(c0142x, null, abstractC0127jZ, null, null, null, 29, null))) {
                            return;
                        }
                    }
                } else {
                    if (androidx.concurrent.futures.a.a(f625L, this, obj, new C0142x(obj, abstractC0127jZ, null, null, null, 28, null))) {
                        return;
                    }
                }
            }
        }
    }

    @Override // kotlin.coroutines.jvm.internal.e
    public kotlin.coroutines.jvm.internal.e getCallerFrame() {
        q1.d dVar = this.f627x;
        if (dVar instanceof kotlin.coroutines.jvm.internal.e) {
            return (kotlin.coroutines.jvm.internal.e) dVar;
        }
        return null;
    }

    @Override // q1.d
    public q1.g getContext() {
        return this.f628y;
    }

    @Override // F1.U
    public Object h() {
        return t();
    }

    public final void j(AbstractC0127j abstractC0127j, Throwable th) {
        try {
            abstractC0127j.a(th);
        } catch (Throwable th2) {
            G.a(getContext(), new B(kotlin.jvm.internal.l.l("Exception in invokeOnCancellation handler for ", this), th2));
        }
    }

    public final void l(y1.l lVar, Throwable th) {
        try {
            lVar.invoke(th);
        } catch (Throwable th2) {
            G.a(getContext(), new B(kotlin.jvm.internal.l.l("Exception in resume onCancellation handler for ", this), th2));
        }
    }

    public boolean m(Throwable th) {
        Object obj;
        boolean z2;
        do {
            obj = this._state;
            if (!(obj instanceof B0)) {
                return false;
            }
            z2 = obj instanceof AbstractC0127j;
        } while (!androidx.concurrent.futures.a.a(f625L, this, obj, new C0134o(this, th, z2)));
        AbstractC0127j abstractC0127j = z2 ? (AbstractC0127j) obj : null;
        if (abstractC0127j != null) {
            j(abstractC0127j, th);
        }
        p();
        q(this.f591r);
        return true;
    }

    public final void o() {
        X x2 = this.f626H;
        if (x2 == null) {
            return;
        }
        x2.dispose();
        this.f626H = A0.f573b;
    }

    public Throwable r(n0 n0Var) {
        return n0Var.e();
    }

    @Override // q1.d
    public void resumeWith(Object obj) {
        F(this, C.b(obj, this), this.f591r, null, 4, null);
    }

    public final Object s() {
        n0 n0Var;
        boolean zY = y();
        if (I()) {
            if (this.f626H == null) {
                w();
            }
            if (zY) {
                D();
            }
            return AbstractC0707b.d();
        }
        if (zY) {
            D();
        }
        Object objT = t();
        if (objT instanceof C0143y) {
            throw ((C0143y) objT).f667a;
        }
        if (!V.b(this.f591r) || (n0Var = (n0) getContext().get(n0.f630a)) == null || n0Var.isActive()) {
            return e(objT);
        }
        CancellationException cancellationExceptionE = n0Var.e();
        a(objT, cancellationExceptionE);
        throw cancellationExceptionE;
    }

    public final Object t() {
        return this._state;
    }

    public String toString() {
        return B() + '(' + L.c(this.f627x) + "){" + u() + "}@" + L.b(this);
    }

    public void v() {
        X xW = w();
        if (xW != null && x()) {
            xW.dispose();
            this.f626H = A0.f573b;
        }
    }

    public boolean x() {
        return !(t() instanceof B0);
    }
}
