import { canTrustPhysicalTrackedChangeSplit, tagTrustedPhysicalTrackedChanges } from './operation-change-helpers.js';
import { createOperationBatchMetrics } from './operation-batch-metrics.js';
import { emitOperationChangedEvents } from './operation-event-emission.js';
export function finalizeOperationMutationEvents(request) {
    const changed = request.suppressChangedSet
        ? new Uint32Array()
        : request.canComposeDisjointEventChanges
            ? request.serviceArgs.composeDisjointEventChanges(request.recalculated, request.explicitChangedCount)
            : request.serviceArgs.composeEventChanges(request.recalculated, request.explicitChangedCount);
    if (request.hasTrackedEventListeners &&
        request.canComposeDisjointEventChanges &&
        changed.length > 4 &&
        request.explicitChangedCount > 0 &&
        request.explicitChangedCount < changed.length) {
        const sheetId = request.serviceArgs.state.workbook.cellStore.sheetIds[changed[0]];
        if (sheetId !== undefined &&
            canTrustPhysicalTrackedChangeSplit(changed, sheetId, request.explicitChangedCount, request.serviceArgs.state.workbook)) {
            tagTrustedPhysicalTrackedChanges(changed, sheetId, request.explicitChangedCount);
        }
    }
    const lastMetrics = createOperationBatchMetrics({
        previousMetrics: request.serviceArgs.state.getLastMetrics(),
        didRunRecalc: request.didRunRecalc,
        directFormulaMetrics: request.directFormulaMetrics,
        changedInputCount: request.changedInputCount,
        formulaChangedCount: request.formulaChangedCount,
        compileMs: request.compileMs,
    });
    request.serviceArgs.state.setLastMetrics(lastMetrics);
    emitOperationChangedEvents(request.serviceArgs, {
        changed,
        invalidation: request.invalidation,
        invalidatedRanges: request.invalidatedRanges,
        invalidatedRows: request.invalidatedRows,
        invalidatedColumns: request.invalidatedColumns,
        metrics: lastMetrics,
        explicitChangedCount: request.explicitChangedCount,
        hasGeneralEventListeners: request.hasGeneralEventListeners,
        hasTrackedEventListeners: request.hasTrackedEventListeners,
        hasWatchedCellListeners: request.hasWatchedCellListeners,
        materializeChangedCells: request.shouldMaterializeChangedCells(changed.length),
    });
}
//# sourceMappingURL=operation-mutation-event-finalizer.js.map