const GENERAL_CHANGED_CELL_PAYLOAD_LIMIT = 512;
export function shouldMaterializeOperationChangedCells(args) {
    return (args.hasGeneralEventListeners &&
        args.invalidation !== 'full' &&
        (args.changedLength <= GENERAL_CHANGED_CELL_PAYLOAD_LIMIT ||
            (args.invalidations.invalidatedRanges.length === 0 &&
                args.invalidations.invalidatedRows.length === 0 &&
                args.invalidations.invalidatedColumns.length === 0)));
}
export function emitOperationChangedEvents(access, args) {
    if (args.hasGeneralEventListeners || args.hasWatchedCellListeners) {
        const event = {
            kind: 'batch',
            invalidation: args.invalidation,
            changedCellIndices: args.changed,
            changedCells: args.materializeChangedCells ? access.captureChangedCells(args.changed) : [],
            invalidatedRanges: args.invalidatedRanges,
            invalidatedRows: args.invalidatedRows,
            invalidatedColumns: args.invalidatedColumns,
            metrics: args.metrics,
            explicitChangedCount: args.explicitChangedCount,
        };
        if (event.invalidation === 'full') {
            access.state.events.emitAllWatched(event);
        }
        else {
            access.state.events.emit(event, args.changed, (cellIndex) => access.state.workbook.getQualifiedAddress(cellIndex));
        }
    }
    if (!args.hasTrackedEventListeners) {
        return;
    }
    const patchRequest = {
        invalidation: args.invalidation,
        invalidatedRanges: args.invalidatedRanges,
        invalidatedRows: args.invalidatedRows,
        invalidatedColumns: args.invalidatedColumns,
    };
    const shouldCapturePatches = args.changed.length > 0 &&
        (patchRequest.invalidation !== 'cells' ||
            patchRequest.invalidatedRanges.length > 0 ||
            patchRequest.invalidatedRows.length > 0 ||
            patchRequest.invalidatedColumns.length > 0);
    const patches = shouldCapturePatches ? access.captureChangedPatches(args.changed, patchRequest) : undefined;
    access.state.events.emitTracked({
        kind: 'batch',
        invalidation: args.invalidation,
        changedCellIndices: args.changed,
        ...(patches ? { patches } : {}),
        invalidatedRanges: args.invalidatedRanges,
        invalidatedRows: args.invalidatedRows,
        invalidatedColumns: args.invalidatedColumns,
        metrics: args.metrics,
        explicitChangedCount: args.explicitChangedCount,
    });
}
//# sourceMappingURL=operation-event-emission.js.map