package kotlinx.coroutines;

import java.util.concurrent.CancellationException;
import kotlin.Result;
import kotlin.coroutines.Continuation;

/* JADX INFO: loaded from: classes.dex */
public abstract class f0 extends be.h {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public int f29356c;

    public f0(int i10) {
        super(0L, be.j.f4844g);
        this.f29356c = i10;
    }

    public abstract void b(Object obj, CancellationException cancellationException);

    public abstract Continuation c();

    public Throwable e(Object obj) {
        q qVar = obj instanceof q ? (q) obj : null;
        if (qVar != null) {
            return qVar.f29523a;
        }
        return null;
    }

    public Object g(Object obj) {
        return obj;
    }

    public final void i(Throwable th, Throwable th2) {
        if (th == null && th2 == null) {
            return;
        }
        if (th != null && th2 != null) {
            kotlin.a.a(th, th2);
        }
        if (th == null) {
            th = th2;
        }
        nb.g.f(th);
        hb.a.g0(c().getContext(), new CoroutinesInternalError("Fatal exception in coroutines machinery for " + this + ". Please read KDoc to 'handleFatalException' method and report this incident to maintainers", th));
    }

    public abstract Object j();

    @Override // java.lang.Runnable
    public final void run() {
        Object objB = kd.e.f29109a;
        androidx.datastore.preferences.protobuf.h hVar = this.f4836b;
        try {
            Continuation continuationC = c();
            nb.g.g(continuationC, "null cannot be cast to non-null type kotlinx.coroutines.internal.DispatchedContinuation<T of kotlinx.coroutines.DispatchedTask>");
            kotlinx.coroutines.internal.g gVar = (kotlinx.coroutines.internal.g) continuationC;
            Continuation continuation = gVar.f29462e;
            Object obj = gVar.f29464g;
            kotlin.coroutines.h context = continuation.getContext();
            Object objC = kotlinx.coroutines.internal.v.c(context, obj);
            t1 t1VarH0 = objC != kotlinx.coroutines.internal.v.f29490a ? y.h0(continuation, context, objC) : null;
            try {
                kotlin.coroutines.h context2 = continuation.getContext();
                Object objJ = j();
                Throwable thE = e(objJ);
                y0 y0Var = (thE == null && y.H(this.f29356c)) ? (y0) context2.m(u.f29565b) : null;
                if (y0Var != null && !y0Var.isActive()) {
                    CancellationException cancellationExceptionZ = ((g1) y0Var).z();
                    b(objJ, cancellationExceptionZ);
                    continuation.resumeWith(kotlin.a.b(cancellationExceptionZ));
                } else if (thE != null) {
                    continuation.resumeWith(kotlin.a.b(thE));
                } else {
                    continuation.resumeWith(g(objJ));
                }
                if (t1VarH0 == null || t1VarH0.b0()) {
                    kotlinx.coroutines.internal.v.a(context, objC);
                }
                try {
                    hVar.getClass();
                } catch (Throwable th) {
                    objB = kotlin.a.b(th);
                }
                i(null, Result.a(objB));
            } catch (Throwable th2) {
                if (t1VarH0 == null || t1VarH0.b0()) {
                    kotlinx.coroutines.internal.v.a(context, objC);
                }
                throw th2;
            }
        } catch (Throwable th3) {
            try {
                hVar.getClass();
            } catch (Throwable th4) {
                objB = kotlin.a.b(th4);
            }
            i(th3, Result.a(objB));
        }
    }
}
