package androidx.compose.runtime;

import androidx.compose.runtime.DerivedState;
import androidx.compose.runtime.collection.MutableVector;
import androidx.compose.runtime.internal.IntRef;
import androidx.compose.runtime.internal.StabilityInferred;
import androidx.compose.runtime.internal.System_jvmKt;
import androidx.compose.runtime.snapshots.Snapshot;
import androidx.compose.runtime.snapshots.SnapshotKt;
import androidx.compose.runtime.snapshots.StateObject;
import androidx.compose.runtime.snapshots.StateObjectImpl;
import androidx.compose.runtime.snapshots.StateRecord;
import n.C2571F;
import n.S;
import n.T;
import w4.C3137A;

/* JADX INFO: loaded from: classes.dex */
final class DerivedSnapshotState<T> extends StateObjectImpl implements DerivedState<T> {
    private final J4.a calculation;
    private ResultRecord<T> first = new ResultRecord<>(SnapshotKt.currentSnapshot().getSnapshotId());
    private final SnapshotMutationPolicy<T> policy;

    @StabilityInferred(parameters = 0)
    public static final class ResultRecord<T> extends StateRecord implements DerivedState.Record<T> {
        private S dependencies;
        private Object result;
        private int resultHash;
        private long validSnapshotId;
        private int validSnapshotWriteCount;
        public static final Companion Companion = new Companion(null);
        public static final int $stable = 8;
        private static final Object Unset = new Object();

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

            public final Object getUnset() {
                return ResultRecord.Unset;
            }

            private Companion() {
            }
        }

        public ResultRecord(long j4) {
            super(j4);
            C2571F c2571f = T.f21375a;
            kotlin.jvm.internal.o.f(c2571f, "null cannot be cast to non-null type androidx.collection.ObjectIntMap<K of androidx.collection.ObjectIntMapKt.emptyObjectIntMap>");
            this.dependencies = c2571f;
            this.result = Unset;
        }

        @Override // androidx.compose.runtime.snapshots.StateRecord
        public void assign(StateRecord stateRecord) {
            kotlin.jvm.internal.o.f(stateRecord, "null cannot be cast to non-null type androidx.compose.runtime.DerivedSnapshotState.ResultRecord<T of androidx.compose.runtime.DerivedSnapshotState.ResultRecord>");
            ResultRecord resultRecord = (ResultRecord) stateRecord;
            setDependencies(resultRecord.getDependencies());
            this.result = resultRecord.result;
            this.resultHash = resultRecord.resultHash;
        }

        @Override // androidx.compose.runtime.snapshots.StateRecord
        public StateRecord create() {
            return create(SnapshotKt.currentSnapshot().getSnapshotId());
        }

        @Override // androidx.compose.runtime.DerivedState.Record
        public T getCurrentValue() {
            return (T) this.result;
        }

        @Override // androidx.compose.runtime.DerivedState.Record
        public S getDependencies() {
            return this.dependencies;
        }

        public final Object getResult() {
            return this.result;
        }

        public final int getResultHash() {
            return this.resultHash;
        }

        public final long getValidSnapshotId() {
            return this.validSnapshotId;
        }

        public final int getValidSnapshotWriteCount() {
            return this.validSnapshotWriteCount;
        }

        /* JADX WARN: Removed duplicated region for block: B:12:0x001e  */
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final boolean isValid(DerivedState<?> derivedState, Snapshot snapshot) {
            boolean z6;
            boolean z7;
            synchronized (SnapshotKt.getLock()) {
                z6 = true;
                if (this.validSnapshotId == snapshot.getSnapshotId()) {
                    z7 = this.validSnapshotWriteCount != snapshot.getWriteCount$runtime();
                }
            }
            if (this.result == Unset || (z7 && this.resultHash != readableHash(derivedState, snapshot))) {
                z6 = false;
            }
            if (!z6 || !z7) {
                return z6;
            }
            synchronized (SnapshotKt.getLock()) {
                this.validSnapshotId = snapshot.getSnapshotId();
                this.validSnapshotWriteCount = snapshot.getWriteCount$runtime();
            }
            return z6;
        }

        public final int readableHash(DerivedState<?> derivedState, Snapshot snapshot) {
            S dependencies;
            int iIdentityHashCode;
            int i6;
            int i7;
            int i8;
            synchronized (SnapshotKt.getLock()) {
                dependencies = getDependencies();
            }
            int i9 = 7;
            if (!(dependencies.f21374e != 0)) {
                return 7;
            }
            MutableVector<DerivedStateObserver> mutableVectorDerivedStateObservers = SnapshotStateKt.derivedStateObservers();
            DerivedStateObserver[] derivedStateObserverArr = mutableVectorDerivedStateObservers.content;
            int size = mutableVectorDerivedStateObservers.getSize();
            for (int i10 = 0; i10 < size; i10++) {
                derivedStateObserverArr[i10].start(derivedState);
            }
            try {
                Object[] objArr = dependencies.f21371b;
                int[] iArr = dependencies.f21372c;
                long[] jArr = dependencies.f21370a;
                int length = jArr.length - 2;
                if (length >= 0) {
                    iIdentityHashCode = 7;
                    int i11 = 0;
                    while (true) {
                        long j4 = jArr[i11];
                        if ((((~j4) << i9) & j4 & (-9187201950435737472L)) != -9187201950435737472L) {
                            int i12 = 8;
                            int i13 = 8 - ((~(i11 - length)) >>> 31);
                            int i14 = 0;
                            while (i14 < i13) {
                                if ((j4 & 255) < 128) {
                                    int i15 = (i11 << 3) + i14;
                                    Object obj = objArr[i15];
                                    i7 = i9;
                                    int i16 = iArr[i15];
                                    i8 = i12;
                                    StateObject stateObject = (StateObject) obj;
                                    if (i16 == 1) {
                                        StateRecord stateRecordCurrent = stateObject instanceof DerivedSnapshotState ? ((DerivedSnapshotState) stateObject).current(snapshot) : SnapshotKt.current(stateObject.getFirstStateRecord(), snapshot);
                                        iIdentityHashCode = (((iIdentityHashCode * 31) + System_jvmKt.identityHashCode(stateRecordCurrent)) * 31) + Long.hashCode(stateRecordCurrent.getSnapshotId$runtime());
                                    }
                                } else {
                                    i7 = i9;
                                    i8 = i12;
                                }
                                j4 >>= i8;
                                i14++;
                                i12 = i8;
                                i9 = i7;
                            }
                            i6 = i9;
                            if (i13 != i12) {
                                break;
                            }
                        } else {
                            i6 = i9;
                        }
                        if (i11 == length) {
                            i9 = iIdentityHashCode;
                            break;
                        }
                        i11++;
                        i9 = i6;
                    }
                }
                iIdentityHashCode = i9;
                DerivedStateObserver[] derivedStateObserverArr2 = mutableVectorDerivedStateObservers.content;
                int size2 = mutableVectorDerivedStateObservers.getSize();
                for (int i17 = 0; i17 < size2; i17++) {
                    derivedStateObserverArr2[i17].done(derivedState);
                }
                return iIdentityHashCode;
            } catch (Throwable th) {
                DerivedStateObserver[] derivedStateObserverArr3 = mutableVectorDerivedStateObservers.content;
                int size3 = mutableVectorDerivedStateObservers.getSize();
                for (int i18 = 0; i18 < size3; i18++) {
                    derivedStateObserverArr3[i18].done(derivedState);
                }
                throw th;
            }
        }

        public void setDependencies(S s6) {
            this.dependencies = s6;
        }

        public final void setResult(Object obj) {
            this.result = obj;
        }

        public final void setResultHash(int i6) {
            this.resultHash = i6;
        }

        public final void setValidSnapshotId(long j4) {
            this.validSnapshotId = j4;
        }

        public final void setValidSnapshotWriteCount(int i6) {
            this.validSnapshotWriteCount = i6;
        }

        @Override // androidx.compose.runtime.snapshots.StateRecord
        public StateRecord create(long j4) {
            return new ResultRecord(j4);
        }
    }

    public DerivedSnapshotState(J4.a aVar, SnapshotMutationPolicy<T> snapshotMutationPolicy) {
        this.calculation = aVar;
        this.policy = snapshotMutationPolicy;
    }

    /* JADX WARN: Finally extract failed */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:29:0x00a9  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private final ResultRecord<T> currentRecord(ResultRecord<T> resultRecord, Snapshot snapshot, boolean z6, J4.a aVar) {
        int i6;
        Snapshot.Companion companion;
        SnapshotMutationPolicy<T> policy;
        int i7;
        ResultRecord<T> resultRecord2 = resultRecord;
        int i8 = 0;
        if (!resultRecord2.isValid(this, snapshot)) {
            final C2571F c2571f = new C2571F();
            final IntRef intRef = (IntRef) SnapshotStateKt__DerivedStateKt.calculationBlockNestedLevel.get();
            if (intRef == null) {
                i6 = 0;
                intRef = new IntRef(0);
                SnapshotStateKt__DerivedStateKt.calculationBlockNestedLevel.set(intRef);
            } else {
                i6 = 0;
            }
            final int element = intRef.getElement();
            MutableVector<DerivedStateObserver> mutableVectorDerivedStateObservers = SnapshotStateKt.derivedStateObservers();
            DerivedStateObserver[] derivedStateObserverArr = mutableVectorDerivedStateObservers.content;
            int size = mutableVectorDerivedStateObservers.getSize();
            for (int i9 = i6; i9 < size; i9++) {
                derivedStateObserverArr[i9].start(this);
            }
            try {
                intRef.setElement(element + 1);
                Object objObserve = Snapshot.Companion.observe(new J4.c() { // from class: androidx.compose.runtime.h
                    @Override // J4.c
                    public final Object invoke(Object obj) {
                        return DerivedSnapshotState.currentRecord$lambda$1$0$0(this.f10916a, intRef, c2571f, element, obj);
                    }
                }, null, aVar);
                intRef.setElement(element);
                DerivedStateObserver[] derivedStateObserverArr2 = mutableVectorDerivedStateObservers.content;
                int size2 = mutableVectorDerivedStateObservers.getSize();
                while (i6 < size2) {
                    derivedStateObserverArr2[i6].done(this);
                    i6++;
                }
                synchronized (SnapshotKt.getLock()) {
                    try {
                        companion = Snapshot.Companion;
                        Snapshot current = companion.getCurrent();
                        if (resultRecord2.getResult() == ResultRecord.Companion.getUnset() || (policy = getPolicy()) == 0 || !policy.equivalent(objObserve, resultRecord2.getResult())) {
                            resultRecord2 = (ResultRecord) SnapshotKt.newWritableRecord(this.first, this, current);
                            resultRecord2.setDependencies(c2571f);
                            resultRecord2.setResultHash(resultRecord2.readableHash(this, current));
                            resultRecord2.setResult(objObserve);
                        } else {
                            resultRecord2.setDependencies(c2571f);
                            resultRecord2.setResultHash(resultRecord2.readableHash(this, current));
                        }
                    } catch (Throwable th) {
                        throw th;
                    }
                }
                IntRef intRef2 = (IntRef) SnapshotStateKt__DerivedStateKt.calculationBlockNestedLevel.get();
                if (intRef2 == null || intRef2.getElement() != 0) {
                    return resultRecord2;
                }
                companion.notifyObjectsInitialized();
                synchronized (SnapshotKt.getLock()) {
                    Snapshot current2 = companion.getCurrent();
                    resultRecord2.setValidSnapshotId(current2.getSnapshotId());
                    resultRecord2.setValidSnapshotWriteCount(current2.getWriteCount$runtime());
                }
                return resultRecord2;
            } catch (Throwable th2) {
                DerivedStateObserver[] derivedStateObserverArr3 = mutableVectorDerivedStateObservers.content;
                int size3 = mutableVectorDerivedStateObservers.getSize();
                while (i6 < size3) {
                    derivedStateObserverArr3[i6].done(this);
                    i6++;
                }
                throw th2;
            }
        }
        if (z6) {
            MutableVector<DerivedStateObserver> mutableVectorDerivedStateObservers2 = SnapshotStateKt.derivedStateObservers();
            DerivedStateObserver[] derivedStateObserverArr4 = mutableVectorDerivedStateObservers2.content;
            int size4 = mutableVectorDerivedStateObservers2.getSize();
            for (int i10 = 0; i10 < size4; i10++) {
                derivedStateObserverArr4[i10].start(this);
            }
            try {
                S dependencies = resultRecord2.getDependencies();
                IntRef intRef3 = (IntRef) SnapshotStateKt__DerivedStateKt.calculationBlockNestedLevel.get();
                if (intRef3 == null) {
                    intRef3 = new IntRef(0);
                    SnapshotStateKt__DerivedStateKt.calculationBlockNestedLevel.set(intRef3);
                }
                int element2 = intRef3.getElement();
                Object[] objArr = dependencies.f21371b;
                int[] iArr = dependencies.f21372c;
                long[] jArr = dependencies.f21370a;
                int length = jArr.length - 2;
                if (length >= 0) {
                    int i11 = 0;
                    while (true) {
                        long j4 = jArr[i11];
                        if ((((~j4) << 7) & j4 & (-9187201950435737472L)) != -9187201950435737472L) {
                            int i12 = 8;
                            int i13 = 8 - ((~(i11 - length)) >>> 31);
                            while (i8 < i13) {
                                if ((j4 & 255) < 128) {
                                    int i14 = (i11 << 3) + i8;
                                    i7 = i12;
                                    StateObject stateObject = (StateObject) objArr[i14];
                                    intRef3.setElement(element2 + iArr[i14]);
                                    J4.c readObserver = snapshot.getReadObserver();
                                    if (readObserver != null) {
                                        readObserver.invoke(stateObject);
                                    }
                                } else {
                                    i7 = i12;
                                }
                                j4 >>= i7;
                                i8++;
                                i12 = i7;
                            }
                            if (i13 != i12) {
                                break;
                            }
                            if (i11 == length) {
                                break;
                            }
                            i11++;
                            i8 = 0;
                        }
                    }
                }
                intRef3.setElement(element2);
                DerivedStateObserver[] derivedStateObserverArr5 = mutableVectorDerivedStateObservers2.content;
                int size5 = mutableVectorDerivedStateObservers2.getSize();
                for (int i15 = 0; i15 < size5; i15++) {
                    derivedStateObserverArr5[i15].done(this);
                }
            } catch (Throwable th3) {
                DerivedStateObserver[] derivedStateObserverArr6 = mutableVectorDerivedStateObservers2.content;
                int size6 = mutableVectorDerivedStateObservers2.getSize();
                for (int i16 = 0; i16 < size6; i16++) {
                    derivedStateObserverArr6[i16].done(this);
                }
                throw th3;
            }
        }
        return resultRecord2;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final C3137A currentRecord$lambda$1$0$0(DerivedSnapshotState derivedSnapshotState, IntRef intRef, C2571F c2571f, int i6, Object obj) {
        if (obj == derivedSnapshotState) {
            throw new IllegalStateException("A derived state calculation cannot read itself");
        }
        if (obj instanceof StateObject) {
            int element = intRef.getElement() - i6;
            int iA = c2571f.a(obj);
            c2571f.g(Math.min(element, iA >= 0 ? c2571f.f21372c[iA] : Integer.MAX_VALUE), obj);
        }
        return C3137A.f25453a;
    }

    private final String displayValue() {
        ResultRecord resultRecord = (ResultRecord) SnapshotKt.current(this.first);
        return resultRecord.isValid(this, Snapshot.Companion.getCurrent()) ? String.valueOf(resultRecord.getResult()) : "<Not calculated>";
    }

    public static /* synthetic */ void getDebuggerDisplayValue$annotations() {
    }

    public final StateRecord current(Snapshot snapshot) {
        return currentRecord((ResultRecord) SnapshotKt.current(this.first, snapshot), snapshot, false, this.calculation);
    }

    @Override // androidx.compose.runtime.DerivedState
    public DerivedState.Record<T> getCurrentRecord() {
        Snapshot current = Snapshot.Companion.getCurrent();
        return currentRecord((ResultRecord) SnapshotKt.current(this.first, current), current, false, this.calculation);
    }

    public final T getDebuggerDisplayValue() {
        ResultRecord resultRecord = (ResultRecord) SnapshotKt.current(this.first);
        if (resultRecord.isValid(this, Snapshot.Companion.getCurrent())) {
            return (T) resultRecord.getResult();
        }
        return null;
    }

    @Override // androidx.compose.runtime.snapshots.StateObject
    public StateRecord getFirstStateRecord() {
        return this.first;
    }

    @Override // androidx.compose.runtime.DerivedState
    public SnapshotMutationPolicy<T> getPolicy() {
        return this.policy;
    }

    @Override // androidx.compose.runtime.State
    public T getValue() {
        Snapshot.Companion companion = Snapshot.Companion;
        J4.c readObserver = companion.getCurrent().getReadObserver();
        if (readObserver != null) {
            readObserver.invoke(this);
        }
        Snapshot current = companion.getCurrent();
        return (T) currentRecord((ResultRecord) SnapshotKt.current(this.first, current), current, true, this.calculation).getResult();
    }

    @Override // androidx.compose.runtime.snapshots.StateObject
    public void prependStateRecord(StateRecord stateRecord) {
        kotlin.jvm.internal.o.f(stateRecord, "null cannot be cast to non-null type androidx.compose.runtime.DerivedSnapshotState.ResultRecord<T of androidx.compose.runtime.DerivedSnapshotState>");
        this.first = (ResultRecord) stateRecord;
    }

    public String toString() {
        return "DerivedState(value=" + displayValue() + ")@" + hashCode();
    }
}
