package androidx.compose.runtime;

import K.a1;
import androidx.compose.runtime.internal.StabilityInferred;
import androidx.compose.runtime.tooling.ComposeToolingApi;
import androidx.compose.runtime.tooling.IdentifiableRecomposeScope;
import java.util.List;
import n.C2571F;
import n.N;
import n.Z;
import w4.C3137A;

/* JADX INFO: loaded from: classes.dex */
@StabilityInferred(parameters = 0)
public final class RecomposeScopeImpl implements ScopeUpdateScope, RecomposeScope, IdentifiableRecomposeScope {
    private Anchor anchor;
    private J4.e block;
    private int currentToken;
    private int flags;
    private RecomposeScopeOwner owner;
    private N trackedDependencies;
    private C2571F trackedInstances;
    public static final Companion Companion = new Companion(null);
    public static final int $stable = 8;

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

        public final void adoptAnchoredScopes$runtime(SlotWriter slotWriter, List<Anchor> list, RecomposeScopeOwner recomposeScopeOwner) {
            if (list.isEmpty()) {
                return;
            }
            int size = list.size();
            for (int i6 = 0; i6 < size; i6++) {
                Object objSlot = slotWriter.slot(list.get(i6), 0);
                RecomposeScopeImpl recomposeScopeImpl = objSlot instanceof RecomposeScopeImpl ? (RecomposeScopeImpl) objSlot : null;
                if (recomposeScopeImpl != null) {
                    recomposeScopeImpl.adoptedBy(recomposeScopeOwner);
                }
            }
        }

        public final boolean hasAnchoredRecomposeScopes$runtime(SlotTable slotTable, List<Anchor> list) {
            if (!list.isEmpty()) {
                int size = list.size();
                for (int i6 = 0; i6 < size; i6++) {
                    Anchor anchor = list.get(i6);
                    if (slotTable.ownsAnchor(anchor) && (slotTable.slot$runtime(slotTable.anchorIndex(anchor), 0) instanceof RecomposeScopeImpl)) {
                        return true;
                    }
                }
            }
            return false;
        }

        private Companion() {
        }
    }

    public RecomposeScopeImpl(RecomposeScopeOwner recomposeScopeOwner) {
        this.owner = recomposeScopeOwner;
    }

    private final boolean checkDerivedStateChanged(DerivedState<?> derivedState, N n6) {
        kotlin.jvm.internal.o.f(derivedState, "null cannot be cast to non-null type androidx.compose.runtime.DerivedState<kotlin.Any?>");
        SnapshotMutationPolicy<?> policy = derivedState.getPolicy();
        if (policy == null) {
            policy = SnapshotStateKt.structuralEqualityPolicy();
        }
        return !policy.equivalent(derivedState.getCurrentRecord().getCurrentValue(), n6.d(derivedState));
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Removed duplicated region for block: B:34:0x0085  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final C3137A end$lambda$0$1(RecomposeScopeImpl recomposeScopeImpl, int i6, C2571F c2571f, Composition composition) {
        int i7;
        if (recomposeScopeImpl.currentToken == i6 && kotlin.jvm.internal.o.c(c2571f, recomposeScopeImpl.trackedInstances) && (composition instanceof CompositionImpl)) {
            long[] jArr = c2571f.f21370a;
            int length = jArr.length - 2;
            if (length >= 0) {
                int i8 = 0;
                while (true) {
                    long j4 = jArr[i8];
                    if ((((~j4) << 7) & j4 & (-9187201950435737472L)) != -9187201950435737472L) {
                        int i9 = 8;
                        int i10 = 8 - ((~(i8 - length)) >>> 31);
                        int i11 = 0;
                        while (i11 < i10) {
                            if ((255 & j4) < 128) {
                                int i12 = (i8 << 3) + i11;
                                Object obj = c2571f.f21371b[i12];
                                boolean z6 = c2571f.f21372c[i12] != i6;
                                if (z6) {
                                    CompositionImpl compositionImpl = (CompositionImpl) composition;
                                    compositionImpl.removeObservation$runtime(obj, recomposeScopeImpl);
                                    i7 = i9;
                                    if (obj instanceof DerivedState) {
                                        compositionImpl.removeDerivedStateObservation$runtime((DerivedState) obj);
                                        N n6 = recomposeScopeImpl.trackedDependencies;
                                        if (n6 != null) {
                                            n6.j(obj);
                                        }
                                    }
                                } else {
                                    i7 = i9;
                                }
                                if (z6) {
                                    c2571f.f(i12);
                                }
                            } else {
                                i7 = i9;
                            }
                            j4 >>= i7;
                            i11++;
                            i9 = i7;
                        }
                        if (i10 != i9) {
                            break;
                        }
                        if (i8 == length) {
                            break;
                        }
                        i8++;
                    }
                }
            }
        }
        return C3137A.f25453a;
    }

    private final boolean getFlag(int i6) {
        return (i6 & this.flags) != 0;
    }

    @ComposeToolingApi
    public static /* synthetic */ void getIdentity$annotations() {
    }

    private final boolean getRereading() {
        return (this.flags & 32) != 0;
    }

    private final void setFlag(int i6, boolean z6) {
        int i7 = this.flags;
        this.flags = z6 ? i6 | i7 : (~i6) & i7;
    }

    private final void setRereading(boolean z6) {
        int i6 = this.flags;
        this.flags = z6 ? i6 | 32 : i6 & (-33);
    }

    private final void setSkipped(boolean z6) {
        int i6 = this.flags;
        this.flags = z6 ? i6 | 16 : i6 & (-17);
    }

    public final void adoptedBy(RecomposeScopeOwner recomposeScopeOwner) {
        this.owner = recomposeScopeOwner;
    }

    public final void compose(Composer composer) {
        J4.e eVar = this.block;
        if (eVar == null) {
            throw new IllegalStateException("Invalid restart scope");
        }
        eVar.invoke(composer, 1);
    }

    public final J4.c end(int i6) {
        C2571F c2571f = this.trackedInstances;
        if (c2571f == null || getSkipped$runtime()) {
            return null;
        }
        Object[] objArr = c2571f.f21371b;
        int[] iArr = c2571f.f21372c;
        long[] jArr = c2571f.f21370a;
        int length = jArr.length - 2;
        if (length < 0) {
            return null;
        }
        int i7 = 0;
        while (true) {
            long j4 = jArr[i7];
            if ((((~j4) << 7) & j4 & (-9187201950435737472L)) != -9187201950435737472L) {
                int i8 = 8 - ((~(i7 - length)) >>> 31);
                for (int i9 = 0; i9 < i8; i9++) {
                    if ((255 & j4) < 128) {
                        int i10 = (i7 << 3) + i9;
                        Object obj = objArr[i10];
                        if (iArr[i10] != i6) {
                            return new a1(this, i6, c2571f, 2);
                        }
                    }
                    j4 >>= 8;
                }
                if (i8 != 8) {
                    return null;
                }
            }
            if (i7 == length) {
                return null;
            }
            i7++;
        }
    }

    public final Anchor getAnchor() {
        return this.anchor;
    }

    public final boolean getCanRecompose() {
        return this.block != null;
    }

    public final boolean getDefaultsInScope() {
        return (this.flags & 2) != 0;
    }

    public final boolean getDefaultsInvalid() {
        return (this.flags & 4) != 0;
    }

    public final boolean getForcedRecompose() {
        return (this.flags & 64) != 0;
    }

    @Override // androidx.compose.runtime.tooling.IdentifiableRecomposeScope
    public Object getIdentity() {
        return this.anchor;
    }

    public final RecomposeScopeOwner getOwner$runtime() {
        return this.owner;
    }

    public final boolean getPaused() {
        return (this.flags & 256) != 0;
    }

    public final boolean getRequiresRecompose() {
        return (this.flags & 8) != 0;
    }

    public final boolean getResetReusing() {
        return (this.flags & 1024) != 0;
    }

    public final boolean getResuming() {
        return (this.flags & 512) != 0;
    }

    public final boolean getReusing() {
        return (this.flags & 128) != 0;
    }

    public final boolean getSkipped$runtime() {
        return (this.flags & 16) != 0;
    }

    public final boolean getUsed() {
        return (this.flags & 1) != 0;
    }

    public final boolean getValid() {
        if (this.owner != null) {
            Anchor anchor = this.anchor;
            if (anchor != null ? anchor.getValid() : false) {
                return true;
            }
        }
        return false;
    }

    @Override // androidx.compose.runtime.RecomposeScope
    public void invalidate() {
        RecomposeScopeOwner recomposeScopeOwner = this.owner;
        if (recomposeScopeOwner != null) {
            recomposeScopeOwner.invalidate(this, null);
        }
    }

    public final InvalidationResult invalidateForResult(Object obj) {
        InvalidationResult invalidationResultInvalidate;
        RecomposeScopeOwner recomposeScopeOwner = this.owner;
        return (recomposeScopeOwner == null || (invalidationResultInvalidate = recomposeScopeOwner.invalidate(this, obj)) == null) ? InvalidationResult.IGNORED : invalidationResultInvalidate;
    }

    public final boolean isConditional() {
        return this.trackedDependencies != null;
    }

    /* JADX WARN: Code restructure failed: missing block: B:29:0x0065, code lost:
    
        return true;
     */
    /* JADX WARN: Removed duplicated region for block: B:32:0x006c  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean isInvalidFor(Object obj) {
        N n6;
        if (obj == null || (n6 = this.trackedDependencies) == null) {
            return true;
        }
        if (obj instanceof DerivedState) {
            return checkDerivedStateChanged((DerivedState) obj, n6);
        }
        if (!(obj instanceof Z)) {
            return true;
        }
        Z z6 = (Z) obj;
        if (z6.c()) {
            Object[] objArr = z6.f21389b;
            long[] jArr = z6.f21388a;
            int length = jArr.length - 2;
            if (length >= 0) {
                int i6 = 0;
                loop0: while (true) {
                    long j4 = jArr[i6];
                    if ((((~j4) << 7) & j4 & (-9187201950435737472L)) != -9187201950435737472L) {
                        int i7 = 8 - ((~(i6 - length)) >>> 31);
                        for (int i8 = 0; i8 < i7; i8++) {
                            if ((255 & j4) < 128) {
                                Object obj2 = objArr[(i6 << 3) + i8];
                                if (!(obj2 instanceof DerivedState) || checkDerivedStateChanged((DerivedState) obj2, n6)) {
                                    break loop0;
                                }
                            }
                            j4 >>= 8;
                        }
                        if (i7 != 8) {
                            break;
                        }
                        if (i6 == length) {
                            break;
                        }
                        i6++;
                    }
                }
            }
        }
        return false;
    }

    public final void recordDerivedStateValue(DerivedState<?> derivedState, Object obj) {
        N n6 = this.trackedDependencies;
        if (n6 == null) {
            n6 = new N();
            this.trackedDependencies = n6;
        }
        n6.l(derivedState, obj);
    }

    public final boolean recordRead(Object obj) {
        int i6;
        if (getRereading()) {
            return false;
        }
        C2571F c2571f = this.trackedInstances;
        if (c2571f == null) {
            c2571f = new C2571F();
            this.trackedInstances = c2571f;
        }
        int i7 = this.currentToken;
        int iD = c2571f.d(obj);
        if (iD < 0) {
            iD = ~iD;
            i6 = -1;
        } else {
            i6 = c2571f.f21372c[iD];
        }
        c2571f.f21371b[iD] = obj;
        c2571f.f21372c[iD] = i7;
        return i6 == this.currentToken;
    }

    public final void release() {
        RecomposeScopeOwner recomposeScopeOwner = this.owner;
        if (recomposeScopeOwner != null) {
            recomposeScopeOwner.recomposeScopeReleased(this);
        }
        this.owner = null;
        this.trackedInstances = null;
        this.trackedDependencies = null;
        this.block = null;
    }

    /* JADX WARN: Removed duplicated region for block: B:22:0x0053  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void rereadTrackedInstances() {
        C2571F c2571f;
        RecomposeScopeOwner recomposeScopeOwner = this.owner;
        if (recomposeScopeOwner == null || (c2571f = this.trackedInstances) == null) {
            return;
        }
        setRereading(true);
        try {
            Object[] objArr = c2571f.f21371b;
            int[] iArr = c2571f.f21372c;
            long[] jArr = c2571f.f21370a;
            int length = jArr.length - 2;
            if (length >= 0) {
                int i6 = 0;
                while (true) {
                    long j4 = jArr[i6];
                    if ((((~j4) << 7) & j4 & (-9187201950435737472L)) != -9187201950435737472L) {
                        int i7 = 8 - ((~(i6 - length)) >>> 31);
                        for (int i8 = 0; i8 < i7; i8++) {
                            if ((255 & j4) < 128) {
                                int i9 = (i6 << 3) + i8;
                                Object obj = objArr[i9];
                                int i10 = iArr[i9];
                                recomposeScopeOwner.recordReadOf(obj);
                            }
                            j4 >>= 8;
                        }
                        if (i7 != 8) {
                            break;
                        } else if (i6 == length) {
                            break;
                        } else {
                            i6++;
                        }
                    }
                }
            }
        } finally {
            setRereading(false);
        }
    }

    public final void scopeSkipped() {
        if (getReusing()) {
            return;
        }
        setSkipped(true);
    }

    public final void setAnchor(Anchor anchor) {
        this.anchor = anchor;
    }

    public final void setDefaultsInScope(boolean z6) {
        int i6 = this.flags;
        this.flags = z6 ? i6 | 2 : i6 & (-3);
    }

    public final void setDefaultsInvalid(boolean z6) {
        int i6 = this.flags;
        this.flags = z6 ? i6 | 4 : i6 & (-5);
    }

    public final void setForcedRecompose(boolean z6) {
        int i6 = this.flags;
        this.flags = z6 ? i6 | 64 : i6 & (-65);
    }

    public final void setOwner$runtime(RecomposeScopeOwner recomposeScopeOwner) {
        this.owner = recomposeScopeOwner;
    }

    public final void setPaused(boolean z6) {
        int i6 = this.flags;
        this.flags = z6 ? i6 | 256 : i6 & (-257);
    }

    public final void setRequiresRecompose(boolean z6) {
        int i6 = this.flags;
        this.flags = z6 ? i6 | 8 : i6 & (-9);
    }

    public final void setResetReusing(boolean z6) {
        int i6 = this.flags;
        this.flags = z6 ? i6 | 1024 : i6 & (-1025);
    }

    public final void setResuming(boolean z6) {
        int i6 = this.flags;
        this.flags = z6 ? i6 | 512 : i6 & (-513);
    }

    public final void setReusing(boolean z6) {
        int i6 = this.flags;
        this.flags = z6 ? i6 | 128 : i6 & (-129);
    }

    public final void setUsed(boolean z6) {
        int i6 = this.flags;
        this.flags = z6 ? i6 | 1 : i6 & (-2);
    }

    public final void start(int i6) {
        this.currentToken = i6;
        setSkipped(false);
    }

    @Override // androidx.compose.runtime.ScopeUpdateScope
    public void updateScope(J4.e eVar) {
        this.block = eVar;
    }
}
