package androidx.compose.runtime;

import androidx.compose.runtime.internal.StabilityInferred;
import androidx.media3.common.C;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
@StabilityInferred(parameters = 0)
public final class SlotReader {
    public static final int $stable = 8;
    private boolean closed;
    private int currentEnd;
    private int currentGroup;
    private int currentSlot;
    private int currentSlotEnd;
    private final IntStack currentSlotStack;
    private int emptyCount;
    private final int[] groups;
    private final int groupsSize;
    private boolean hadNext;
    private int parent;
    private Object[] slots;
    private final int slotsSize;
    private HashMap<Anchor, GroupSourceInformation> sourceInformationMap;
    private final SlotTable table;

    public SlotReader(SlotTable slotTable) {
        this.table = slotTable;
        this.groups = slotTable.getGroups();
        int groupsSize = slotTable.getGroupsSize();
        this.groupsSize = groupsSize;
        this.slots = slotTable.getSlots();
        this.slotsSize = slotTable.getSlotsSize();
        this.currentEnd = groupsSize;
        this.parent = -1;
        this.currentSlotStack = new IntStack();
    }

    public static /* synthetic */ Anchor anchor$default(SlotReader slotReader, int i6, int i7, Object obj) {
        if ((i7 & 1) != 0) {
            i6 = slotReader.currentGroup;
        }
        return slotReader.anchor(i6);
    }

    private final Object aux(int[] iArr, int i6) {
        return (iArr[(i6 * 5) + 1] & 268435456) != 0 ? this.slots[SlotTableKt.auxIndex(iArr, i6)] : Composer.Companion.getEmpty();
    }

    private final Object objectKey(int[] iArr, int i6) {
        if ((iArr[(i6 * 5) + 1] & C.BUFFER_FLAG_LAST_SAMPLE) != 0) {
            return this.slots[SlotTableKt.objectKeyIndex(iArr, i6)];
        }
        return null;
    }

    public final Anchor anchor(int i6) {
        ArrayList<Anchor> anchors$runtime = this.table.getAnchors$runtime();
        int iSearch = SlotTableKt.search(anchors$runtime, i6, this.groupsSize);
        if (iSearch >= 0) {
            return anchors$runtime.get(iSearch);
        }
        Anchor anchor = new Anchor(i6);
        anchors$runtime.add(-(iSearch + 1), anchor);
        return anchor;
    }

    public final void beginEmpty() {
        this.emptyCount++;
    }

    public final void close() {
        this.closed = true;
        this.table.close$runtime(this, this.sourceInformationMap);
        this.slots = new Object[0];
    }

    public final boolean containsMark(int i6) {
        return (this.groups[(i6 * 5) + 1] & 67108864) != 0;
    }

    public final void endEmpty() {
        if (!(this.emptyCount > 0)) {
            PreconditionsKt.throwIllegalArgumentException("Unbalanced begin/end empty");
        }
        this.emptyCount--;
    }

    public final void endGroup() {
        if (this.emptyCount == 0) {
            if (!(this.currentGroup == this.currentEnd)) {
                ComposerKt.composeImmediateRuntimeError("endGroup() not called at the end of a group");
            }
            int[] iArr = this.groups;
            int i6 = iArr[(this.parent * 5) + 2];
            this.parent = i6;
            this.currentEnd = i6 < 0 ? this.groupsSize : SlotTableKt.groupSize(iArr, i6) + i6;
            int iPop = this.currentSlotStack.pop();
            if (iPop < 0) {
                this.currentSlot = 0;
                this.currentSlotEnd = 0;
            } else {
                this.currentSlot = iPop;
                this.currentSlotEnd = i6 >= this.groupsSize - 1 ? this.slotsSize : this.groups[((i6 + 1) * 5) + 4];
            }
        }
    }

    public final List<KeyInfo> extractKeys() {
        ArrayList arrayList = new ArrayList();
        if (this.emptyCount <= 0) {
            int i6 = 0;
            int iGroupSize = this.currentGroup;
            while (true) {
                int i7 = i6;
                if (iGroupSize >= this.currentEnd) {
                    break;
                }
                int[] iArr = this.groups;
                int i8 = iGroupSize * 5;
                int i9 = iArr[i8];
                Object objObjectKey = objectKey(iArr, iGroupSize);
                int i10 = 1;
                int i11 = this.groups[i8 + 1];
                if ((1073741824 & i11) == 0) {
                    i10 = i11 & 67108863;
                }
                i6 = i7 + 1;
                arrayList.add(new KeyInfo(i9, objObjectKey, iGroupSize, i10, i7));
                iGroupSize += SlotTableKt.groupSize(this.groups, iGroupSize);
            }
        }
        return arrayList;
    }

    public final Object get(int i6) {
        int i7 = this.currentSlot + i6;
        return i7 < this.currentSlotEnd ? this.slots[i7] : Composer.Companion.getEmpty();
    }

    public final boolean getClosed() {
        return this.closed;
    }

    public final int getCurrentEnd() {
        return this.currentEnd;
    }

    public final int getCurrentGroup() {
        return this.currentGroup;
    }

    public final Object getGroupAux() {
        int i6 = this.currentGroup;
        if (i6 < this.currentEnd) {
            return aux(this.groups, i6);
        }
        return 0;
    }

    public final int getGroupEnd() {
        return this.currentEnd;
    }

    public final int getGroupKey() {
        int i6 = this.currentGroup;
        if (i6 < this.currentEnd) {
            return this.groups[i6 * 5];
        }
        return 0;
    }

    public final Object getGroupNode() {
        int i6 = this.currentGroup;
        if (i6 < this.currentEnd) {
            return node(this.groups, i6);
        }
        return null;
    }

    public final Object getGroupObjectKey() {
        int i6 = this.currentGroup;
        if (i6 < this.currentEnd) {
            return objectKey(this.groups, i6);
        }
        return null;
    }

    public final int getGroupSize() {
        return SlotTableKt.groupSize(this.groups, this.currentGroup);
    }

    public final int getGroupSlotCount() {
        int i6 = this.currentGroup;
        int iSlotAnchor = SlotTableKt.slotAnchor(this.groups, i6);
        int i7 = i6 + 1;
        return (i7 < this.groupsSize ? this.groups[(i7 * 5) + 4] : this.slotsSize) - iSlotAnchor;
    }

    public final int getGroupSlotIndex() {
        return this.currentSlot - SlotTableKt.slotAnchor(this.groups, this.parent);
    }

    public final boolean getHadNext() {
        return this.hadNext;
    }

    public final boolean getHasObjectKey() {
        int i6 = this.currentGroup;
        return i6 < this.currentEnd && (this.groups[(i6 * 5) + 1] & C.BUFFER_FLAG_LAST_SAMPLE) != 0;
    }

    public final boolean getInEmpty() {
        return this.emptyCount > 0;
    }

    public final int getNodeCount() {
        return this.groups[(this.currentGroup * 5) + 1] & 67108863;
    }

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

    public final int getParentNodes() {
        int i6 = this.parent;
        if (i6 >= 0) {
            return this.groups[(i6 * 5) + 1] & 67108863;
        }
        return 0;
    }

    public final int getRemainingSlots() {
        return this.currentSlotEnd - this.currentSlot;
    }

    public final int getSize() {
        return this.groupsSize;
    }

    public final int getSlot() {
        return this.currentSlot - SlotTableKt.slotAnchor(this.groups, this.parent);
    }

    public final SlotTable getTable$runtime() {
        return this.table;
    }

    public final Object groupAux(int i6) {
        return aux(this.groups, i6);
    }

    public final int groupEnd(int i6) {
        return SlotTableKt.groupSize(this.groups, i6) + i6;
    }

    public final Object groupGet(int i6) {
        return groupGet(this.currentGroup, i6);
    }

    public final int groupKey(int i6) {
        return this.groups[i6 * 5];
    }

    public final Object groupObjectKey(int i6) {
        return objectKey(this.groups, i6);
    }

    public final int groupSize(int i6) {
        return SlotTableKt.groupSize(this.groups, i6);
    }

    public final boolean hasMark(int i6) {
        return (this.groups[(i6 * 5) + 1] & C.BUFFER_FLAG_FIRST_SAMPLE) != 0;
    }

    public final boolean hasObjectKey(int i6) {
        return (this.groups[(i6 * 5) + 1] & C.BUFFER_FLAG_LAST_SAMPLE) != 0;
    }

    public final boolean isGroupEnd() {
        return getInEmpty() || this.currentGroup == this.currentEnd;
    }

    public final boolean isNode() {
        return (this.groups[(this.currentGroup * 5) + 1] & 1073741824) != 0;
    }

    public final Object next() {
        int i6;
        if (this.emptyCount > 0 || (i6 = this.currentSlot) >= this.currentSlotEnd) {
            this.hadNext = false;
            return Composer.Companion.getEmpty();
        }
        this.hadNext = true;
        Object[] objArr = this.slots;
        this.currentSlot = i6 + 1;
        return objArr[i6];
    }

    public final Object node(int i6) {
        int[] iArr = this.groups;
        if ((iArr[(i6 * 5) + 1] & 1073741824) != 0) {
            return node(iArr, i6);
        }
        return null;
    }

    public final int nodeCount(int i6) {
        return this.groups[(i6 * 5) + 1] & 67108863;
    }

    public final int parent(int i6) {
        return this.groups[(i6 * 5) + 2];
    }

    public final int parentOf(int i6) {
        if (!(i6 >= 0 && i6 < this.groupsSize)) {
            PreconditionsKt.throwIllegalArgumentException("Invalid group index " + i6);
        }
        return this.groups[(i6 * 5) + 2];
    }

    public final void reposition(int i6) {
        if (!(this.emptyCount == 0)) {
            ComposerKt.composeImmediateRuntimeError("Cannot reposition while in an empty region");
        }
        this.currentGroup = i6;
        int i7 = this.groupsSize;
        int i8 = i6 < i7 ? this.groups[(i6 * 5) + 2] : -1;
        if (i8 != this.parent) {
            this.parent = i8;
            if (i8 < 0) {
                this.currentEnd = i7;
            } else {
                this.currentEnd = SlotTableKt.groupSize(this.groups, i8) + i8;
            }
            this.currentSlot = 0;
            this.currentSlotEnd = 0;
        }
    }

    public final void restoreParent(int i6) {
        int iGroupSize = SlotTableKt.groupSize(this.groups, i6) + i6;
        int i7 = this.currentGroup;
        if (!(i7 >= i6 && i7 <= iGroupSize)) {
            ComposerKt.composeImmediateRuntimeError("Index " + i6 + " is not a parent of " + i7);
        }
        this.parent = i6;
        this.currentEnd = iGroupSize;
        this.currentSlot = 0;
        this.currentSlotEnd = 0;
    }

    public final void setCurrentGroup(int i6) {
        this.currentGroup = i6;
    }

    public final int skipGroup() {
        if (!(this.emptyCount == 0)) {
            ComposerKt.composeImmediateRuntimeError("Cannot skip while in an empty region");
        }
        int[] iArr = this.groups;
        int i6 = this.currentGroup;
        int i7 = (iArr[(i6 * 5) + 1] & 1073741824) == 0 ? iArr[(i6 * 5) + 1] & 67108863 : 1;
        this.currentGroup = SlotTableKt.groupSize(iArr, i6) + i6;
        return i7;
    }

    public final void skipToGroupEnd() {
        if (!(this.emptyCount == 0)) {
            ComposerKt.composeImmediateRuntimeError("Cannot skip the enclosing group while in an empty region");
        }
        this.currentGroup = this.currentEnd;
        this.currentSlot = 0;
        this.currentSlotEnd = 0;
    }

    public final int slotSize(int i6) {
        int iSlotAnchor = SlotTableKt.slotAnchor(this.groups, i6);
        int i7 = i6 + 1;
        return (i7 < this.groupsSize ? this.groups[(i7 * 5) + 4] : this.slotsSize) - iSlotAnchor;
    }

    public final void startGroup() {
        GroupSourceInformation groupSourceInformation;
        if (this.emptyCount <= 0) {
            int i6 = this.parent;
            int i7 = this.currentGroup;
            if (!(this.groups[(i7 * 5) + 2] == i6)) {
                PreconditionsKt.throwIllegalArgumentException("Invalid slot table detected");
            }
            HashMap<Anchor, GroupSourceInformation> map = this.sourceInformationMap;
            if (map != null && (groupSourceInformation = map.get(anchor(i6))) != null) {
                groupSourceInformation.reportGroup(this.table, i7);
            }
            IntStack intStack = this.currentSlotStack;
            int i8 = this.currentSlot;
            int i9 = this.currentSlotEnd;
            if (i8 == 0 && i9 == 0) {
                intStack.push(-1);
            } else {
                intStack.push(i8);
            }
            this.parent = i7;
            this.currentEnd = SlotTableKt.groupSize(this.groups, i7) + i7;
            int i10 = i7 + 1;
            this.currentGroup = i10;
            this.currentSlot = SlotTableKt.slotAnchor(this.groups, i7);
            this.currentSlotEnd = i7 >= this.groupsSize - 1 ? this.slotsSize : this.groups[(i10 * 5) + 4];
        }
    }

    public final void startNode() {
        if (this.emptyCount <= 0) {
            if (!((this.groups[(this.currentGroup * 5) + 1] & 1073741824) != 0)) {
                PreconditionsKt.throwIllegalArgumentException("Expected a node group");
            }
            startGroup();
        }
    }

    public String toString() {
        StringBuilder sb = new StringBuilder("SlotReader(current=");
        sb.append(this.currentGroup);
        sb.append(", key=");
        sb.append(getGroupKey());
        sb.append(", parent=");
        sb.append(this.parent);
        sb.append(", end=");
        return com.google.android.recaptcha.internal.a.m(sb, this.currentEnd, ')');
    }

    public final Object groupGet(int i6, int i7) {
        int iSlotAnchor = SlotTableKt.slotAnchor(this.groups, i6);
        int i8 = i6 + 1;
        int i9 = iSlotAnchor + i7;
        return i9 < (i8 < this.groupsSize ? this.groups[(i8 * 5) + 4] : this.slotsSize) ? this.slots[i9] : Composer.Companion.getEmpty();
    }

    public final int groupKey(Anchor anchor) {
        if (anchor.getValid()) {
            return this.groups[this.table.anchorIndex(anchor) * 5];
        }
        return 0;
    }

    public final boolean isNode(int i6) {
        return (this.groups[(i6 * 5) + 1] & 1073741824) != 0;
    }

    private final Object node(int[] iArr, int i6) {
        int i7 = i6 * 5;
        if ((iArr[i7 + 1] & 1073741824) != 0) {
            return this.slots[iArr[i7 + 4]];
        }
        return Composer.Companion.getEmpty();
    }
}
