const TRACKED_CELL_PATCH_LIMIT = 64;
export function captureTrackedRecalcPatchesForCells(input) {
    return input.changed.length <= TRACKED_CELL_PATCH_LIMIT
        ? input.captureChangedPatches(input.changed, {
            invalidation: 'cells',
            invalidatedRanges: [],
            invalidatedRows: [],
            invalidatedColumns: [],
        })
        : undefined;
}
export function emitRecalcBatchEvents(input) {
    const hasGeneralEventListeners = input.state.events.hasListeners();
    const hasWatchedCellListeners = input.state.events.hasCellListeners();
    const hasTrackedEventListeners = input.state.events.hasTrackedListeners();
    if (!hasGeneralEventListeners && !hasWatchedCellListeners && !hasTrackedEventListeners) {
        return;
    }
    const changedCellIndices = input.changed instanceof Uint32Array ? input.changed : Array.from(input.changed);
    if (hasGeneralEventListeners || hasWatchedCellListeners) {
        const event = {
            kind: 'batch',
            invalidation: 'cells',
            changedCellIndices,
            changedCells: hasGeneralEventListeners ? input.captureChangedCells(input.changed) : [],
            invalidatedRanges: [],
            invalidatedRows: [],
            invalidatedColumns: [],
            metrics: input.metrics,
            explicitChangedCount: input.explicitChangedCount,
        };
        input.state.events.emit(event, input.changed, (cellIndex) => input.state.workbook.getQualifiedAddress(cellIndex));
    }
    if (!hasTrackedEventListeners) {
        return;
    }
    const patches = captureTrackedRecalcPatchesForCells({
        changed: input.changed,
        captureChangedPatches: input.captureChangedPatches,
    });
    const trackedEvent = {
        kind: 'batch',
        invalidation: 'cells',
        changedCellIndices,
        ...(patches ? { patches } : {}),
        invalidatedRanges: [],
        invalidatedRows: [],
        invalidatedColumns: [],
        metrics: input.metrics,
        explicitChangedCount: input.explicitChangedCount,
    };
    input.state.events.emitTracked(trackedEvent);
}
//# sourceMappingURL=recalc-event-emission.js.map