package d2;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/* JADX INFO: compiled from: CancellableContinuationImpl.kt */
/* JADX INFO: loaded from: classes.dex */
public class h<T> extends j0<T> implements g<T>, p1.d {

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private static final /* synthetic */ AtomicReferenceFieldUpdater f4491l = AtomicReferenceFieldUpdater.newUpdater(h.class, Object.class, "_state");
    private volatile /* synthetic */ int _decision;
    private volatile /* synthetic */ Object _state;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final n1.d<T> f4492h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final n1.f f4493i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private m0 f4494j;

    private final Void i(Object obj) {
        throw new IllegalStateException(w1.g.j("Already resumed, but proposed with update ", obj).toString());
    }

    private final void o() {
        if (s()) {
            return;
        }
        m();
    }

    private final void p(int i3) {
        if (x()) {
            return;
        }
        k0.a(this, i3);
    }

    private final String r() {
        Object objQ = q();
        return objQ instanceof k1 ? "Active" : objQ instanceof i ? "Cancelled" : "Completed";
    }

    private final boolean s() {
        return k0.c(this.f4498g) && ((kotlinx.coroutines.internal.f) this.f4492h).l();
    }

    private final void u(Object obj, int i3, v1.l<? super Throwable, k1.o> lVar) {
        Object obj2;
        do {
            obj2 = this._state;
            if (!(obj2 instanceof k1)) {
                if (obj2 instanceof i) {
                    i iVar = (i) obj2;
                    if (iVar.c()) {
                        if (lVar == null) {
                            return;
                        }
                        l(lVar, iVar.f4508a);
                        return;
                    }
                }
                i(obj);
                throw new k1.c();
            }
        } while (!f4491l.compareAndSet(this, obj2, w((k1) obj2, obj, i3, lVar, null)));
        o();
        p(i3);
    }

    /* JADX WARN: Multi-variable type inference failed */
    static /* synthetic */ void v(h hVar, Object obj, int i3, v1.l lVar, int i4, Object obj2) {
        if (obj2 != null) {
            throw new UnsupportedOperationException("Super calls with default arguments not supported in this target, function: resumeImpl");
        }
        if ((i4 & 4) != 0) {
            lVar = null;
        }
        hVar.u(obj, i3, lVar);
    }

    private final Object w(k1 k1Var, Object obj, int i3, v1.l<? super Throwable, k1.o> lVar, Object obj2) {
        if (obj instanceof n) {
            if (c0.a()) {
                if (!(obj2 == null)) {
                    throw new AssertionError();
                }
            }
            if (!c0.a()) {
                return obj;
            }
            if (lVar == null) {
                return obj;
            }
            throw new AssertionError();
        }
        if (!k0.b(i3) && obj2 == null) {
            return obj;
        }
        if (lVar == null && !(k1Var instanceof e) && obj2 == null) {
            return obj;
        }
        return new m(obj, k1Var instanceof e ? (e) k1Var : null, lVar, obj2, null, 16, null);
    }

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

    @Override // d2.j0
    public void a(Object obj, Throwable th) {
        while (true) {
            Object obj2 = this._state;
            if (obj2 instanceof k1) {
                throw new IllegalStateException("Not completed".toString());
            }
            if (obj2 instanceof n) {
                return;
            }
            if (obj2 instanceof m) {
                m mVar = (m) obj2;
                if (!(!mVar.c())) {
                    throw new IllegalStateException("Must be called at most once".toString());
                }
                if (f4491l.compareAndSet(this, obj2, m.b(mVar, null, null, null, null, th, 15, null))) {
                    mVar.d(this, th);
                    return;
                }
            } else if (f4491l.compareAndSet(this, obj2, new m(obj2, null, null, null, th, 14, null))) {
                return;
            }
        }
    }

    @Override // d2.j0
    public final n1.d<T> b() {
        return this.f4492h;
    }

    @Override // d2.j0
    public Throwable c(Object obj) {
        Throwable thC = super.c(obj);
        if (thC == null) {
            return null;
        }
        n1.d<T> dVarB = b();
        if (c0.c() && (dVarB instanceof p1.d)) {
            thC = kotlinx.coroutines.internal.a0.j(thC, (p1.d) dVarB);
        }
        return thC;
    }

    @Override // n1.d
    public n1.f d() {
        return this.f4493i;
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // d2.j0
    public <T> T e(Object obj) {
        return obj instanceof m ? (T) ((m) obj).f4502a : obj;
    }

    @Override // p1.d
    public p1.d g() {
        n1.d<T> dVar = this.f4492h;
        if (dVar instanceof p1.d) {
            return (p1.d) dVar;
        }
        return null;
    }

    @Override // d2.j0
    public Object h() {
        return q();
    }

    @Override // n1.d
    public void j(Object obj) {
        v(this, r.b(obj, this), this.f4498g, null, 4, null);
    }

    public final void k(e eVar, Throwable th) {
        try {
            eVar.a(th);
        } catch (Throwable th2) {
            y.a(d(), new q(w1.g.j("Exception in invokeOnCancellation handler for ", this), th2));
        }
    }

    public final void l(v1.l<? super Throwable, k1.o> lVar, Throwable th) {
        try {
            lVar.e(th);
        } catch (Throwable th2) {
            y.a(d(), new q(w1.g.j("Exception in resume onCancellation handler for ", this), th2));
        }
    }

    public final void m() {
        m0 m0Var = this.f4494j;
        if (m0Var == null) {
            return;
        }
        m0Var.b();
        this.f4494j = j1.f4499e;
    }

    @Override // p1.d
    public StackTraceElement n() {
        return null;
    }

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

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

    public String toString() {
        return t() + '(' + d0.c(this.f4492h) + "){" + r() + "}@" + d0.b(this);
    }
}
