package androidx.compose.runtime.snapshots;

import androidx.compose.runtime.internal.StabilityInferred;
import androidx.compose.runtime.snapshots.SnapshotApplyResult;
import androidx.compose.runtime.snapshots.tooling.SnapshotObserverKt;
import java.util.Map;
import kotlin.jvm.internal.o;
import n.O;

/* JADX INFO: loaded from: classes.dex */
@StabilityInferred(parameters = 0)
public final class NestedMutableSnapshot extends MutableSnapshot {
    public static final int $stable = 8;
    private boolean deactivated;
    private final MutableSnapshot parent;

    public NestedMutableSnapshot(long j4, SnapshotIdSet snapshotIdSet, J4.c cVar, J4.c cVar2, MutableSnapshot mutableSnapshot) {
        super(j4, snapshotIdSet, cVar, cVar2);
        this.parent = mutableSnapshot;
        mutableSnapshot.mo253nestedActivated$runtime(this);
    }

    private final void deactivate() {
        if (this.deactivated) {
            return;
        }
        this.deactivated = true;
        this.parent.mo254nestedDeactivated$runtime(this);
    }

    @Override // androidx.compose.runtime.snapshots.MutableSnapshot
    public SnapshotApplyResult apply() throws Throwable {
        NestedMutableSnapshot nestedMutableSnapshot;
        if (this.parent.getApplied$runtime() || this.parent.getDisposed$runtime()) {
            return new SnapshotApplyResult.Failure(this);
        }
        O modified$runtime = getModified$runtime();
        long snapshotId = getSnapshotId();
        Map<StateRecord, ? extends StateRecord> mapOptimisticMerges = modified$runtime != null ? SnapshotKt.optimisticMerges(this.parent.getSnapshotId(), this, this.parent.getInvalid$runtime()) : null;
        synchronized (SnapshotKt.getLock()) {
            try {
                SnapshotKt.validateOpen(this);
                try {
                    if (modified$runtime == null || modified$runtime.f21391d == 0) {
                        nestedMutableSnapshot = this;
                        closeAndReleasePinning$runtime();
                    } else {
                        nestedMutableSnapshot = this;
                        SnapshotApplyResult snapshotApplyResultInnerApplyLocked$runtime = nestedMutableSnapshot.innerApplyLocked$runtime(this.parent.getSnapshotId(), modified$runtime, mapOptimisticMerges, this.parent.getInvalid$runtime());
                        if (!o.c(snapshotApplyResultInnerApplyLocked$runtime, SnapshotApplyResult.Success.INSTANCE)) {
                            return snapshotApplyResultInnerApplyLocked$runtime;
                        }
                        O modified$runtime2 = nestedMutableSnapshot.parent.getModified$runtime();
                        if (modified$runtime2 != null) {
                            modified$runtime2.k(modified$runtime);
                        } else {
                            nestedMutableSnapshot.parent.setModified$runtime(modified$runtime);
                            setModified$runtime(null);
                        }
                    }
                    if (o.j(nestedMutableSnapshot.parent.getSnapshotId(), snapshotId) < 0) {
                        nestedMutableSnapshot.parent.advance$runtime();
                    }
                    MutableSnapshot mutableSnapshot = nestedMutableSnapshot.parent;
                    mutableSnapshot.setInvalid$runtime(mutableSnapshot.getInvalid$runtime().clear(snapshotId).andNot(getPreviousIds$runtime()));
                    nestedMutableSnapshot.parent.recordPrevious$runtime(snapshotId);
                    nestedMutableSnapshot.parent.recordPreviousPinnedSnapshot$runtime(takeoverPinnedSnapshot$runtime());
                    nestedMutableSnapshot.parent.recordPreviousList$runtime(getPreviousIds$runtime());
                    nestedMutableSnapshot.parent.recordPreviousPinnedSnapshots$runtime(getPreviousPinnedSnapshots$runtime());
                    setApplied$runtime(true);
                    deactivate();
                    SnapshotObserverKt.dispatchObserverOnApplied(this, modified$runtime);
                    return SnapshotApplyResult.Success.INSTANCE;
                } catch (Throwable th) {
                    th = th;
                    throw th;
                }
            } catch (Throwable th2) {
                th = th2;
            }
        }
    }

    @Override // androidx.compose.runtime.snapshots.MutableSnapshot, androidx.compose.runtime.snapshots.Snapshot
    public void dispose() {
        if (getDisposed$runtime()) {
            return;
        }
        super.dispose();
        deactivate();
    }

    public final MutableSnapshot getParent() {
        return this.parent;
    }

    @Override // androidx.compose.runtime.snapshots.MutableSnapshot, androidx.compose.runtime.snapshots.Snapshot
    public Snapshot getRoot() {
        return this.parent.getRoot();
    }
}
