import { ValueTag } from '@bilig/protocol';
import { writeLiteralToCellStore } from '../../engine-value-utils.js';
import { addEngineCounter } from '../../perf/engine-counters.js';
export function tryApplySingleDirectFormulaLiteralMutationWithoutEvents(args, request) {
    const formulaCellIndex = request.formulaCellIndex;
    if (formulaCellIndex < 0) {
        return false;
    }
    const formula = args.state.formulas.get(formulaCellIndex);
    if (!formula || ((args.state.workbook.cellStore.flags[formulaCellIndex] ?? 0) & 8 /* CellFlags.InCycle */) !== 0) {
        return false;
    }
    if (formula.directLookup !== undefined) {
        return tryApplyDirectLookupFormulaLiteralMutation(args, request, formula.directLookup);
    }
    return tryApplyDirectScalarFormulaLiteralMutation(args, request, formula.directScalar);
}
function tryApplyDirectLookupFormulaLiteralMutation(args, request, directLookup) {
    const formulaCellIndex = request.formulaCellIndex;
    if (!args.hasNoCellDependents(formulaCellIndex)) {
        return false;
    }
    const numericResult = args.tryDirectUniformLookupNumericResultFromDescriptor(directLookup, request.exactLookupValue, request.approximateLookupValue);
    if (numericResult !== undefined) {
        const resultChanged = !args.directScalarNumericResultMatchesCell(formulaCellIndex, numericResult);
        args.withOptionalColumnVersionBatch(resultChanged && args.cellsShareVersionColumn(request.existingIndex, formulaCellIndex), () => {
            args.writeFastPathLiteralToExistingCell(request.existingIndex, request.value);
            if (resultChanged) {
                args.applyTerminalDirectFormulaNumericResult(formulaCellIndex, numericResult);
            }
        });
        addEngineCounter(args.state.counters, resultChanged ? 'directFormulaKernelSyncOnlyRecalcSkips' : 'kernelSyncOnlyRecalcSkips');
        args.deferSingleCellKernelSync(request.existingIndex);
        args.state.setLastMetrics(args.makeSingleLiteralSkipMetrics());
        return true;
    }
    const result = args.tryDirectUniformLookupCurrentResultFromNumeric(formulaCellIndex, request.exactLookupValue, request.approximateLookupValue);
    if (result === undefined) {
        return false;
    }
    const resultChanged = !args.directScalarCurrentResultMatchesCell(formulaCellIndex, result);
    args.withOptionalColumnVersionBatch(resultChanged && args.cellsShareVersionColumn(request.existingIndex, formulaCellIndex), () => {
        args.writeFastPathLiteralToExistingCell(request.existingIndex, request.value);
        if (resultChanged && !args.applyDirectFormulaCurrentResult(formulaCellIndex, result)) {
            throw new Error('Failed to apply direct lookup result');
        }
    });
    addEngineCounter(args.state.counters, resultChanged ? 'directFormulaKernelSyncOnlyRecalcSkips' : 'kernelSyncOnlyRecalcSkips');
    args.deferSingleCellKernelSync(request.existingIndex);
    args.state.setLastMetrics(args.makeSingleLiteralSkipMetrics());
    return true;
}
function tryApplyDirectScalarFormulaLiteralMutation(args, request, directScalar) {
    const formulaCellIndex = request.formulaCellIndex;
    if (!args.canUseDirectFormulaPostRecalc(formulaCellIndex)) {
        return false;
    }
    if (directScalar === undefined || args.state.workbook.cellStore.tags[formulaCellIndex] !== ValueTag.Number) {
        return false;
    }
    const delta = args.tryDirectScalarNumericDeltaFromNumbers(directScalar, request.existingIndex, request.oldNumber, request.newNumber);
    if (delta === undefined) {
        return false;
    }
    args.withOptionalColumnVersionBatch(args.cellsShareVersionColumn(request.existingIndex, formulaCellIndex), () => {
        writeLiteralToCellStore(args.state.workbook.cellStore, request.existingIndex, request.value, args.state.strings);
        args.state.workbook.notifyCellValueWritten(request.existingIndex);
        if (!args.applyDirectFormulaNumericDelta(formulaCellIndex, delta)) {
            throw new Error('Failed to apply direct scalar delta');
        }
    });
    addEngineCounter(args.state.counters, 'directScalarDeltaApplications');
    addEngineCounter(args.state.counters, 'directScalarDeltaOnlyRecalcSkips');
    args.deferSingleCellKernelSync(request.existingIndex);
    args.state.setLastMetrics(args.makeSingleLiteralSkipMetrics());
    return true;
}
//# sourceMappingURL=operation-direct-formula-literal-fast-path.js.map