package androidx.compose.runtime;

import V4.C;
import V4.C0763h0;
import V4.C0769k0;
import V4.C0787z;
import V4.E;
import V4.InterfaceC0765i0;
import androidx.compose.runtime.internal.StabilityInferred;
import androidx.compose.runtime.tooling.CompositionErrorContextImpl;

/* JADX INFO: loaded from: classes.dex */
@StabilityInferred(parameters = 0)
public final class RememberedCoroutineScope implements C, RememberObserver {
    private volatile A4.j _coroutineContext;
    private final Object lock = this;
    private final A4.j overlayContext;
    private final A4.j parentContext;
    public static final Companion Companion = new Companion(null);
    public static final int $stable = 8;
    public static final A4.j CancelledCoroutineContext = new CancelledCoroutineContext();

    public static final class Companion {
        public /* synthetic */ Companion(kotlin.jvm.internal.h hVar) {
            this();
        }

        private Companion() {
        }
    }

    public RememberedCoroutineScope(A4.j jVar, A4.j jVar2) {
        this.parentContext = jVar;
        this.overlayContext = jVar2;
    }

    public final void cancelIfCreated() {
        synchronized (this.lock) {
            try {
                A4.j jVar = this._coroutineContext;
                if (jVar == null) {
                    this._coroutineContext = CancelledCoroutineContext;
                } else {
                    E.h(jVar, new ForgottenCoroutineScopeException());
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // V4.C
    public A4.j getCoroutineContext() {
        A4.j jVarPlus;
        A4.j jVar = this._coroutineContext;
        if (jVar == null || jVar == CancelledCoroutineContext) {
            CompositionErrorContextImpl compositionErrorContextImpl = (CompositionErrorContextImpl) this.parentContext.get(CompositionErrorContextImpl.Key);
            A4.j rememberedCoroutineScope$special$$inlined$CoroutineExceptionHandler$1 = compositionErrorContextImpl != null ? new RememberedCoroutineScope$special$$inlined$CoroutineExceptionHandler$1(C0787z.f8352a, compositionErrorContextImpl, this) : A4.k.f106a;
            synchronized (this.lock) {
                try {
                    jVarPlus = this._coroutineContext;
                    if (jVarPlus == null) {
                        A4.j jVar2 = this.parentContext;
                        jVarPlus = jVar2.plus(new C0769k0((InterfaceC0765i0) jVar2.get(C0763h0.f8306a))).plus(this.overlayContext).plus(rememberedCoroutineScope$special$$inlined$CoroutineExceptionHandler$1);
                    } else if (jVarPlus == CancelledCoroutineContext) {
                        A4.j jVar3 = this.parentContext;
                        C0769k0 c0769k0 = new C0769k0((InterfaceC0765i0) jVar3.get(C0763h0.f8306a));
                        c0769k0.n(new ForgottenCoroutineScopeException());
                        jVarPlus = jVar3.plus(c0769k0).plus(this.overlayContext).plus(rememberedCoroutineScope$special$$inlined$CoroutineExceptionHandler$1);
                    }
                    this._coroutineContext = jVarPlus;
                } catch (Throwable th) {
                    throw th;
                }
            }
            jVar = jVarPlus;
        }
        kotlin.jvm.internal.o.e(jVar);
        return jVar;
    }

    @Override // androidx.compose.runtime.RememberObserver
    public void onAbandoned() {
        cancelIfCreated();
    }

    @Override // androidx.compose.runtime.RememberObserver
    public void onForgotten() {
        cancelIfCreated();
    }

    @Override // androidx.compose.runtime.RememberObserver
    public void onRemembered() {
    }
}
