import { addEngineCounter } from '../../perf/engine-counters.js';
import { assertInitialFreshDirectScalarFormulaBindingMember, } from './formula-initialization-fresh-direct-scalar-binding.js';
export function createInitialFreshDirectScalarFormulaRunQueue(args) {
    let pending;
    const flush = () => {
        const run = pending;
        if (run === undefined) {
            return;
        }
        pending = undefined;
        const bindFreshDirectScalarFormulaRun = args.bindFreshDirectScalarFormulaRun;
        if (bindFreshDirectScalarFormulaRun === undefined) {
            return;
        }
        if (run.members.length === 1) {
            const prepared = run.members[0];
            bindFreshDirectScalarFormulaRun({
                sheetId: prepared.sheetId,
                ownerSheetName: prepared.ownerSheetName,
                cellIndex: prepared.cellIndex,
                member: run.members[0],
            });
        }
        else {
            bindFreshDirectScalarFormulaRun({
                sheetId: run.sheetId,
                ownerSheetName: run.ownerSheetName,
                cellIndices: run.cellIndexCount === run.cellIndices.length ? run.cellIndices : run.cellIndices.subarray(0, run.cellIndexCount),
                members: run.members,
            });
        }
        addEngineCounter(args.counters, 'initialFreshDirectScalarFastBindings', run.members.length);
        for (const prepared of run.members) {
            args.noteInitializedFormula(prepared, args.readRuntimeFormula(prepared.cellIndex));
            args.clearPendingFormulaCell(prepared.cellIndex);
        }
    };
    return {
        flush,
        queue(prepared) {
            if (pending !== undefined && (pending.sheetId !== prepared.sheetId || pending.ownerSheetName !== prepared.ownerSheetName)) {
                flush();
            }
            pending ??= {
                sheetId: prepared.sheetId,
                ownerSheetName: prepared.ownerSheetName,
                cellIndices: new Uint32Array(Math.max(1, args.capacity ?? 16)),
                cellIndexCount: 0,
                members: preallocatedRunArray(args.capacity),
            };
            assertInitialFreshDirectScalarFormulaBindingMember(prepared);
            const member = prepared;
            if (pending.cellIndexCount === pending.cellIndices.length) {
                const next = new Uint32Array(pending.cellIndices.length * 2);
                next.set(pending.cellIndices);
                pending.cellIndices = next;
            }
            pending.cellIndices[pending.cellIndexCount] = member.cellIndex;
            pending.cellIndexCount += 1;
            pending.members.push(member);
        },
    };
}
function preallocatedRunArray(capacity) {
    const values = [];
    if (capacity !== undefined && capacity > 0) {
        values.length = capacity;
        values.length = 0;
    }
    return values;
}
//# sourceMappingURL=formula-initialization-fresh-direct-scalar-run-queue.js.map