import { ValueTag } from '@bilig/protocol';
import { makeCellEntity } from '../../entity-ids.js';
export function tryApplyOperationFormulaReplacementAsDirectScalarDeltaRoot(args, request) {
    if (request.changedTopology || request.oldNumber === undefined) {
        return false;
    }
    const formula = args.state.formulas.get(request.cellIndex);
    if (!formula ||
        formula.directScalar === undefined ||
        formula.compiled.volatile ||
        formula.compiled.producesSpill ||
        ((args.state.workbook.cellStore.flags[request.cellIndex] ?? 0) & 8 /* CellFlags.InCycle */) !== 0) {
        return false;
    }
    const result = args.evaluateDirectScalarCurrentValue(formula.directScalar);
    if (result?.kind !== 'number') {
        return false;
    }
    const dependent = args.getSingleEntityDependent(makeCellEntity(request.cellIndex));
    if (dependent !== -1 &&
        !args.tryMarkDirectScalarLinearDeltaClosure(request.cellIndex, { tag: ValueTag.Number, value: request.oldNumber }, { tag: ValueTag.Number, value: result.value }, request.postRecalcDirectFormulaIndices)) {
        return false;
    }
    if (args.canSkipFormulaColumnVersion(request.cellIndex)) {
        args.applyTerminalDirectFormulaNumericResult(request.cellIndex, result.value);
    }
    else if (!args.applyDirectFormulaCurrentResult(request.cellIndex, result)) {
        return false;
    }
    return true;
}
//# sourceMappingURL=operation-formula-replacement-direct-scalar.js.map