package androidx.compose.runtime.changelist;

import B.B0;
import J4.c;
import R4.p;
import androidx.compose.runtime.Applier;
import androidx.compose.runtime.InternalComposeApi;
import androidx.compose.runtime.RememberManager;
import androidx.compose.runtime.SlotWriter;
import androidx.compose.runtime.changelist.Operation;
import androidx.compose.runtime.internal.StabilityInferred;
import kotlin.jvm.internal.o;
import w4.InterfaceC3141c;
import x4.AbstractC3250p;
import x4.AbstractC3251q;
import x4.y;

/* JADX INFO: loaded from: classes.dex */
@StabilityInferred(parameters = 0)
public final class Operations extends OperationsDebugStringFormattable {
    public static final int $stable = 8;
    public int intArgsSize;
    public int objectArgsSize;
    public int opCodesSize;
    private int pushedIntMask;
    private int pushedObjectMask;
    public Operation[] opCodes = new Operation[16];
    public int[] intArgs = new int[16];
    public Object[] objectArgs = new Object[16];

    public final class OpIterator implements OperationArgContainer {
        private int intIdx;
        private int objIdx;
        private int opIdx;

        public OpIterator() {
        }

        public final String currentOperationDebugString() {
            String str = "operation[" + this.opIdx + "] = " + Operations.this.currentOpToDebugString(this, "");
            o.g(str, "toString(...)");
            return str;
        }

        @Override // androidx.compose.runtime.changelist.OperationArgContainer
        public int getInt(int i6) {
            return Operations.this.intArgs[this.intIdx + i6];
        }

        @Override // androidx.compose.runtime.changelist.OperationArgContainer
        /* JADX INFO: renamed from: getObject-31yXWZQ */
        public <T> T mo140getObject31yXWZQ(int i6) {
            return (T) Operations.this.objectArgs[this.objIdx + i6];
        }

        public final Operation getOperation() {
            return Operations.this.opCodes[this.opIdx];
        }

        public final boolean next() {
            if (this.opIdx >= Operations.this.opCodesSize) {
                return false;
            }
            Operation operation = getOperation();
            this.intIdx = operation.getInts() + this.intIdx;
            this.objIdx = operation.getObjects() + this.objIdx;
            int i6 = this.opIdx + 1;
            this.opIdx = i6;
            return i6 < Operations.this.opCodesSize;
        }
    }

    private final int createExpectedArgMask(int i6) {
        return (i6 == 0 ? 0 : -1) >>> (32 - i6);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final String currentOpToDebugString(OpIterator opIterator, String str) {
        Operation operation = opIterator.getOperation();
        if (operation.getInts() == 0 && operation.getObjects() == 0) {
            return operation.getName();
        }
        StringBuilder sb = new StringBuilder();
        sb.append(operation.getName());
        sb.append('(');
        String strIndent = indent(str);
        int ints = operation.getInts();
        boolean z6 = true;
        for (int i6 = 0; i6 < ints; i6++) {
            String strIntParamName = operation.intParamName(i6);
            if (z6) {
                z6 = false;
            } else {
                sb.append(", ");
            }
            sb.append('\n');
            sb.append(strIndent);
            sb.append(strIntParamName);
            sb.append(" = ");
            sb.append(opIterator.getInt(i6));
        }
        int objects = operation.getObjects();
        for (int i7 = 0; i7 < objects; i7++) {
            int iM120constructorimpl = Operation.ObjectParameter.m120constructorimpl(i7);
            String strMo94objectParamName31yXWZQ = operation.mo94objectParamName31yXWZQ(iM120constructorimpl);
            if (z6) {
                z6 = false;
            } else {
                sb.append(", ");
            }
            sb.append('\n');
            sb.append(strIndent);
            sb.append(strMo94objectParamName31yXWZQ);
            sb.append(" = ");
            sb.append(formatOpArgumentToString(opIterator.mo140getObject31yXWZQ(iM120constructorimpl), strIndent));
        }
        sb.append('\n');
        sb.append(str);
        sb.append(")");
        String string = sb.toString();
        o.g(string, "toString(...)");
        return string;
    }

    private final int determineNewSize(int i6, int i7) {
        int i8 = i6 + (i6 <= 1024 ? i6 : 1024);
        return i8 < i7 ? i7 : i8;
    }

    private final void ensureIntArgsSizeAtLeast(int i6) {
        int length = this.intArgs.length;
        if (i6 > length) {
            resizeIntArgs(length, i6);
        }
    }

    private final void ensureObjectArgsSizeAtLeast(int i6) {
        int length = this.objectArgs.length;
        if (i6 > length) {
            resizeObjectArgs(length, i6);
        }
    }

    private final String exceptionMessageForOperationPushNoScope(Operation operation) {
        return "Cannot push " + operation + " without arguments because it expects " + operation.getInts() + " ints and " + operation.getObjects() + " objects.";
    }

    private final String exceptionMessageForOperationPushWithScope(Operation operation) {
        StringBuilder sb = new StringBuilder();
        int ints = operation.getInts();
        int i6 = 0;
        for (int i7 = 0; i7 < ints; i7++) {
            if (((1 << i7) & this.pushedIntMask) == 0) {
                if (i6 > 0) {
                    sb.append(", ");
                }
                sb.append(operation.intParamName(i7));
                i6++;
            }
        }
        String string = sb.toString();
        o.g(string, "toString(...)");
        StringBuilder sb2 = new StringBuilder();
        int objects = operation.getObjects();
        int i8 = 0;
        for (int i9 = 0; i9 < objects; i9++) {
            if (((1 << i9) & this.pushedObjectMask) == 0) {
                if (i6 > 0) {
                    sb2.append(", ");
                }
                sb2.append(operation.mo94objectParamName31yXWZQ(Operation.ObjectParameter.m120constructorimpl(i9)));
                i8++;
            }
        }
        String string2 = sb2.toString();
        o.g(string2, "toString(...)");
        return "Error while pushing " + operation + ". Not all arguments were provided. Missing " + i6 + " int arguments (" + string + ") and " + i8 + " object arguments (" + string2 + ").";
    }

    private final String formatOpArgumentToString(Object obj, String str) {
        if (obj == null) {
            return "null";
        }
        boolean z6 = obj instanceof Object[];
        Object pVar = y.f26065a;
        if (z6) {
            Object[] objArr = (Object[]) obj;
            if (objArr.length != 0) {
                pVar = new p(objArr, 2);
            }
            return toCollectionString(pVar, str);
        }
        if (obj instanceof int[]) {
            int[] iArr = (int[]) obj;
            if (iArr.length != 0) {
                pVar = new p(iArr, 3);
            }
            return toCollectionString(pVar, str);
        }
        if (obj instanceof long[]) {
            long[] jArr = (long[]) obj;
            if (jArr.length != 0) {
                pVar = new p(jArr, 4);
            }
            return toCollectionString(pVar, str);
        }
        if (obj instanceof float[]) {
            float[] fArr = (float[]) obj;
            if (fArr.length != 0) {
                pVar = new p(fArr, 5);
            }
            return toCollectionString(pVar, str);
        }
        if (!(obj instanceof double[])) {
            return obj instanceof Iterable ? toCollectionString((Iterable) obj, str) : obj instanceof OperationsDebugStringFormattable ? ((OperationsDebugStringFormattable) obj).toDebugString(str) : obj.toString();
        }
        double[] dArr = (double[]) obj;
        if (dArr.length != 0) {
            pVar = new p(dArr, 6);
        }
        return toCollectionString(pVar, str);
    }

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

    private final String indent(String str) {
        return Z0.o.n(str, "    ");
    }

    private final Operation peekOperation() {
        return this.opCodes[this.opCodesSize - 1];
    }

    private final void resizeIntArgs(int i6, int i7) {
        int[] iArr = new int[determineNewSize(i6, i7)];
        AbstractC3250p.x(0, 0, i6, this.intArgs, iArr);
        this.intArgs = iArr;
    }

    private final void resizeObjectArgs(int i6, int i7) {
        Object[] objArr = new Object[determineNewSize(i6, i7)];
        System.arraycopy(this.objectArgs, 0, objArr, 0, i6);
        this.objectArgs = objArr;
    }

    private final void resizeOpCodes() {
        int i6 = this.opCodesSize;
        Operation[] operationArr = new Operation[(i6 <= 1024 ? i6 : 1024) + i6];
        System.arraycopy(this.opCodes, 0, operationArr, 0, i6);
        this.opCodes = operationArr;
    }

    private final <T> String toCollectionString(Iterable<? extends T> iterable, String str) {
        return AbstractC3251q.m0(iterable, ", ", "[", "]", new B0(26, this, str), 24);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final CharSequence toCollectionString$lambda$0(Operations operations, String str, Object obj) {
        return operations.formatOpArgumentToString(obj, str);
    }

    private final int topIntIndexOf(int i6) {
        return (this.intArgsSize - this.opCodes[this.opCodesSize - 1].getInts()) + i6;
    }

    /* JADX INFO: renamed from: topObjectIndexOf-31yXWZQ, reason: not valid java name */
    private final int m141topObjectIndexOf31yXWZQ(int i6) {
        return (this.objectArgsSize - this.opCodes[this.opCodesSize - 1].getObjects()) + i6;
    }

    public final void clear() {
        this.opCodesSize = 0;
        this.intArgsSize = 0;
        AbstractC3250p.H(this.objectArgs, null, 0, this.objectArgsSize);
        this.objectArgsSize = 0;
    }

    public final void drain(c cVar) {
        if (isNotEmpty()) {
            OpIterator opIterator = new OpIterator();
            do {
                cVar.invoke(opIterator);
            } while (opIterator.next());
        }
        clear();
    }

    public final void ensureAllArgumentsPushedFor(Operation operation) {
        int i6 = this.pushedIntMask;
        int ints = operation.getInts();
        if (i6 == ((ints == 0 ? 0 : -1) >>> (32 - ints))) {
            operation.getObjects();
        }
    }

    public final void executeAndFlushAllPendingOperations(Applier<?> applier, SlotWriter slotWriter, RememberManager rememberManager, OperationErrorContext operationErrorContext) {
        if (isNotEmpty()) {
            OpIterator opIterator = new OpIterator();
            while (true) {
                Applier<?> applier2 = applier;
                SlotWriter slotWriter2 = slotWriter;
                RememberManager rememberManager2 = rememberManager;
                OperationErrorContext operationErrorContext2 = operationErrorContext;
                opIterator.getOperation().executeWithComposeStackTrace(opIterator, applier2, slotWriter2, rememberManager2, operationErrorContext2);
                if (!opIterator.next()) {
                    break;
                }
                applier = applier2;
                slotWriter = slotWriter2;
                rememberManager = rememberManager2;
                operationErrorContext = operationErrorContext2;
            }
        }
        clear();
    }

    public final void forEach(c cVar) {
        if (isNotEmpty()) {
            OpIterator opIterator = new OpIterator();
            do {
                cVar.invoke(opIterator);
            } while (opIterator.next());
        }
    }

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

    public final boolean isEmpty() {
        return getSize() == 0;
    }

    public final boolean isNotEmpty() {
        return getSize() != 0;
    }

    public final void pop() {
        Operation[] operationArr = this.opCodes;
        int i6 = this.opCodesSize - 1;
        this.opCodesSize = i6;
        Operation operation = operationArr[i6];
        operationArr[i6] = null;
        int objects = operation.getObjects();
        for (int i7 = 0; i7 < objects; i7++) {
            Object[] objArr = this.objectArgs;
            int i8 = this.objectArgsSize - 1;
            this.objectArgsSize = i8;
            objArr[i8] = null;
        }
        this.intArgsSize -= operation.getInts();
    }

    public final void popInto(Operations operations) {
        Operation[] operationArr = this.opCodes;
        int i6 = this.opCodesSize - 1;
        this.opCodesSize = i6;
        Operation operation = operationArr[i6];
        operationArr[i6] = null;
        operations.pushOp(operation);
        Object[] objArr = this.objectArgs;
        Object[] objArr2 = operations.objectArgs;
        int objects = operations.objectArgsSize - operation.getObjects();
        int objects2 = this.objectArgsSize - operation.getObjects();
        System.arraycopy(objArr, objects2, objArr2, objects, this.objectArgsSize - objects2);
        AbstractC3250p.H(this.objectArgs, null, this.objectArgsSize - operation.getObjects(), this.objectArgsSize);
        AbstractC3250p.x(operations.intArgsSize - operation.getInts(), this.intArgsSize - operation.getInts(), this.intArgsSize, this.intArgs, operations.intArgs);
        this.objectArgsSize -= operation.getObjects();
        this.intArgsSize -= operation.getInts();
    }

    public final void push(Operation operation) {
        pushOp(operation);
    }

    @InternalComposeApi
    public final void pushOp(Operation operation) {
        if (this.opCodesSize == this.opCodes.length) {
            resizeOpCodes();
        }
        int ints = operation.getInts() + this.intArgsSize;
        int length = this.intArgs.length;
        if (ints > length) {
            resizeIntArgs(length, ints);
        }
        int objects = operation.getObjects() + this.objectArgsSize;
        int length2 = this.objectArgs.length;
        if (objects > length2) {
            resizeObjectArgs(length2, objects);
        }
        Operation[] operationArr = this.opCodes;
        int i6 = this.opCodesSize;
        this.opCodesSize = i6 + 1;
        operationArr[i6] = operation;
        this.intArgsSize = operation.getInts() + this.intArgsSize;
        this.objectArgsSize = operation.getObjects() + this.objectArgsSize;
    }

    @Override // androidx.compose.runtime.changelist.OperationsDebugStringFormattable
    public String toDebugString(String str) {
        StringBuilder sb = new StringBuilder();
        if (isNotEmpty()) {
            OpIterator opIterator = new OpIterator();
            int i6 = 0;
            while (true) {
                sb.append(str);
                int i7 = i6 + 1;
                sb.append(i6);
                sb.append(". ");
                sb.append(currentOpToDebugString(opIterator, str));
                sb.append('\n');
                if (!opIterator.next()) {
                    break;
                }
                i6 = i7;
            }
        }
        String string = sb.toString();
        o.g(string, "toString(...)");
        return string;
    }

    @InterfaceC3141c
    public String toString() {
        return super.toString();
    }

    public final void push(Operation operation, c cVar) {
        pushOp(operation);
        cVar.invoke(WriteScope.m142boximpl(WriteScope.m143constructorimpl(this)));
        ensureAllArgumentsPushedFor(operation);
    }

    public static final class WriteScope {
        private final Operations stack;

        private /* synthetic */ WriteScope(Operations operations) {
            this.stack = operations;
        }

        /* JADX INFO: renamed from: box-impl, reason: not valid java name */
        public static final /* synthetic */ WriteScope m142boximpl(Operations operations) {
            return new WriteScope(operations);
        }

        /* JADX INFO: renamed from: constructor-impl, reason: not valid java name */
        public static Operations m143constructorimpl(Operations operations) {
            return operations;
        }

        /* JADX INFO: renamed from: equals-impl, reason: not valid java name */
        public static boolean m144equalsimpl(Operations operations, Object obj) {
            return (obj instanceof WriteScope) && o.c(operations, ((WriteScope) obj).m156unboximpl());
        }

        /* JADX INFO: renamed from: equals-impl0, reason: not valid java name */
        public static final boolean m145equalsimpl0(Operations operations, Operations operations2) {
            return o.c(operations, operations2);
        }

        /* JADX INFO: renamed from: getOperation-impl, reason: not valid java name */
        public static final Operation m146getOperationimpl(Operations operations) {
            return operations.opCodes[operations.opCodesSize - 1];
        }

        /* JADX INFO: renamed from: hashCode-impl, reason: not valid java name */
        public static int m147hashCodeimpl(Operations operations) {
            return operations.hashCode();
        }

        /* JADX INFO: renamed from: setInt-impl, reason: not valid java name */
        public static final void m148setIntimpl(Operations operations, int i6, int i7) {
            operations.intArgs[(operations.intArgsSize - operations.opCodes[operations.opCodesSize - 1].getInts()) + i6] = i7;
        }

        /* JADX INFO: renamed from: setInts-impl, reason: not valid java name */
        public static final void m149setIntsimpl(Operations operations, int i6, int i7, int i8, int i9) {
            int ints = operations.intArgsSize - operations.opCodes[operations.opCodesSize - 1].getInts();
            int[] iArr = operations.intArgs;
            iArr[i6 + ints] = i7;
            iArr[ints + i8] = i9;
        }

        /* JADX INFO: renamed from: setObject-DKhxnng, reason: not valid java name */
        public static final <T> void m151setObjectDKhxnng(Operations operations, int i6, T t6) {
            operations.objectArgs[(operations.objectArgsSize - operations.opCodes[operations.opCodesSize - 1].getObjects()) + i6] = t6;
        }

        /* JADX INFO: renamed from: setObjects-4uCC6AY, reason: not valid java name */
        public static final <T, U> void m152setObjects4uCC6AY(Operations operations, int i6, T t6, int i7, U u6) {
            int objects = operations.objectArgsSize - operations.opCodes[operations.opCodesSize - 1].getObjects();
            Object[] objArr = operations.objectArgs;
            objArr[i6 + objects] = t6;
            objArr[objects + i7] = u6;
        }

        /* JADX INFO: renamed from: setObjects-OGa0p1M, reason: not valid java name */
        public static final <T, U, V, W> void m153setObjectsOGa0p1M(Operations operations, int i6, T t6, int i7, U u6, int i8, V v3, int i9, W w6) {
            int objects = operations.objectArgsSize - operations.opCodes[operations.opCodesSize - 1].getObjects();
            Object[] objArr = operations.objectArgs;
            objArr[i6 + objects] = t6;
            objArr[i7 + objects] = u6;
            objArr[i8 + objects] = v3;
            objArr[objects + i9] = w6;
        }

        /* JADX INFO: renamed from: setObjects-t7hvbck, reason: not valid java name */
        public static final <T, U, V> void m154setObjectst7hvbck(Operations operations, int i6, T t6, int i7, U u6, int i8, V v3) {
            int objects = operations.objectArgsSize - operations.opCodes[operations.opCodesSize - 1].getObjects();
            Object[] objArr = operations.objectArgs;
            objArr[i6 + objects] = t6;
            objArr[i7 + objects] = u6;
            objArr[objects + i8] = v3;
        }

        /* JADX INFO: renamed from: toString-impl, reason: not valid java name */
        public static String m155toStringimpl(Operations operations) {
            return "WriteScope(stack=" + operations + ')';
        }

        public boolean equals(Object obj) {
            return m144equalsimpl(this.stack, obj);
        }

        public int hashCode() {
            return m147hashCodeimpl(this.stack);
        }

        public String toString() {
            return m155toStringimpl(this.stack);
        }

        /* JADX INFO: renamed from: unbox-impl, reason: not valid java name */
        public final /* synthetic */ Operations m156unboximpl() {
            return this.stack;
        }

        /* JADX INFO: renamed from: setInts-impl, reason: not valid java name */
        public static final void m150setIntsimpl(Operations operations, int i6, int i7, int i8, int i9, int i10, int i11) {
            int ints = operations.intArgsSize - operations.opCodes[operations.opCodesSize - 1].getInts();
            int[] iArr = operations.intArgs;
            iArr[i6 + ints] = i7;
            iArr[i8 + ints] = i9;
            iArr[ints + i10] = i11;
        }
    }
}
