import { formulaColumnCountKey } from './formula-binding-dependency-helpers.js';
export function createPendingInitialFormulaCellTracker(args) {
    let pendingCells;
    let pendingColumnCounts;
    const hasPendingCell = (cellIndex) => (pendingCells?.[cellIndex] ?? 0) !== 0;
    return {
        withCells(cellIndices, callback) {
            const previousCells = pendingCells;
            const previousColumnCounts = pendingColumnCounts;
            let capacity = args.getCellCapacity() + 1;
            for (let index = 0; index < cellIndices.length; index += 1) {
                capacity = Math.max(capacity, (cellIndices[index] ?? 0) + 1);
            }
            if (previousCells) {
                capacity = Math.max(capacity, previousCells.length);
            }
            const nextCells = new Uint8Array(capacity);
            if (previousCells) {
                nextCells.set(previousCells);
            }
            const nextColumnCounts = new Map(previousColumnCounts);
            for (let index = 0; index < cellIndices.length; index += 1) {
                const cellIndex = cellIndices[index];
                if (nextCells[cellIndex] !== 0) {
                    continue;
                }
                nextCells[cellIndex] = 1;
                const sheetId = args.getSheetId(cellIndex);
                const col = args.getCol(cellIndex);
                if (sheetId === undefined || col === undefined) {
                    continue;
                }
                const key = formulaColumnCountKey(sheetId, col);
                nextColumnCounts.set(key, (nextColumnCounts.get(key) ?? 0) + 1);
            }
            pendingCells = nextCells;
            pendingColumnCounts = nextColumnCounts;
            try {
                return callback();
            }
            finally {
                pendingCells = previousCells;
                pendingColumnCounts = previousColumnCounts;
            }
        },
        isFormulaCell(cellIndex) {
            return args.isBoundFormulaCell(cellIndex) || hasPendingCell(cellIndex);
        },
        hasColumnMembers(sheetId, col) {
            return args.hasBoundColumnMembers(sheetId, col) || (pendingColumnCounts?.get(formulaColumnCountKey(sheetId, col)) ?? 0) !== 0;
        },
    };
}
//# sourceMappingURL=formula-binding-pending-formula-cells.js.map