import { ErrorCode, ValueTag } from '@bilig/protocol';
import { writeLiteralToCellStore } from '../../engine-value-utils.js';
export const FAST_OPERATION_LITERAL_OVERWRITE_FLAGS = 2 /* CellFlags.HasFormula */ | 4 /* CellFlags.JsOnly */ | 8 /* CellFlags.InCycle */ | 64 /* CellFlags.SpillChild */ | 128 /* CellFlags.PivotOutput */;
export function canFastPathOperationLiteralOverwrite(input) {
    const flags = input.cellStore.flags[input.cellIndex] ?? 0;
    return (flags & FAST_OPERATION_LITERAL_OVERWRITE_FLAGS) === 0 && input.formulas.get(input.cellIndex) === undefined;
}
export function isOperationNullLiteralWriteNoOp(input) {
    if (input.state.formulas.get(input.cellIndex) !== undefined) {
        return false;
    }
    if (input.state.workbook.getCellFormat(input.cellIndex) !== undefined) {
        return false;
    }
    const flags = input.state.workbook.cellStore.flags[input.cellIndex] ?? 0;
    if ((flags & FAST_OPERATION_LITERAL_OVERWRITE_FLAGS) !== 0) {
        return false;
    }
    const value = input.state.workbook.cellStore.getValue(input.cellIndex, (id) => input.state.strings.get(id));
    return value.tag === ValueTag.Empty;
}
export function isOperationClearCellNoOp(input) {
    if (((input.state.workbook.cellStore.flags[input.cellIndex] ?? 0) & 256 /* CellFlags.AuthoredBlank */) !== 0) {
        return false;
    }
    return isOperationNullLiteralWriteNoOp(input);
}
export function writeOperationLiteralToExistingCell(input) {
    const cellStore = input.workbook.cellStore;
    const hasSetValueHook = cellStore.onSetValue !== null;
    writeLiteralToCellStore(cellStore, input.cellIndex, input.value, input.strings);
    if (!hasSetValueHook) {
        input.workbook.notifyCellValueWritten(input.cellIndex);
    }
}
export function writeOperationNumericLiteralToExistingCell(input) {
    const cellStore = input.workbook.cellStore;
    const flags = cellStore.flags[input.cellIndex] ?? 0;
    const hasSetValueHook = cellStore.onSetValue !== null;
    cellStore.tags[input.cellIndex] = ValueTag.Number;
    cellStore.errors[input.cellIndex] = ErrorCode.None;
    cellStore.stringIds[input.cellIndex] = 0;
    cellStore.numbers[input.cellIndex] = input.value;
    if ((flags & 256 /* CellFlags.AuthoredBlank */) !== 0) {
        cellStore.flags[input.cellIndex] = flags & ~256 /* CellFlags.AuthoredBlank */;
    }
    cellStore.versions[input.cellIndex] = (cellStore.versions[input.cellIndex] ?? 0) + 1;
    cellStore.onSetValue?.(input.cellIndex);
    if (!hasSetValueHook) {
        input.workbook.notifyCellValueWritten(input.cellIndex);
    }
}
export function writeOperationNumericLiteralToExistingCellWithoutColumnNotify(input) {
    const flags = input.cellStore.flags[input.cellIndex] ?? 0;
    input.cellStore.tags[input.cellIndex] = ValueTag.Number;
    input.cellStore.errors[input.cellIndex] = ErrorCode.None;
    input.cellStore.stringIds[input.cellIndex] = 0;
    input.cellStore.numbers[input.cellIndex] = input.value;
    if ((flags & 256 /* CellFlags.AuthoredBlank */) !== 0) {
        input.cellStore.flags[input.cellIndex] = flags & ~256 /* CellFlags.AuthoredBlank */;
    }
    input.cellStore.versions[input.cellIndex] = (input.cellStore.versions[input.cellIndex] ?? 0) + 1;
}
export function writeTrustedOperationExistingNumericLiteralToCell(input) {
    const flags = input.cellStore.flags[input.cellIndex] ?? 0;
    input.cellStore.numbers[input.cellIndex] = input.value;
    if ((flags & 256 /* CellFlags.AuthoredBlank */) !== 0) {
        input.cellStore.flags[input.cellIndex] = flags & ~256 /* CellFlags.AuthoredBlank */;
    }
    input.cellStore.versions[input.cellIndex] = (input.cellStore.versions[input.cellIndex] ?? 0) + 1;
    const onSetValue = input.cellStore.onSetValue;
    if (onSetValue) {
        onSetValue(input.cellIndex);
        return;
    }
    input.sheet.columnVersions[input.col] = (input.sheet.columnVersions[input.col] ?? 0) + 1;
}
export function writeFastPathOperationLiteralToExistingCell(input) {
    if (typeof input.value === 'number') {
        writeOperationNumericLiteralToExistingCell({
            workbook: input.workbook,
            cellIndex: input.cellIndex,
            value: input.value,
        });
        return;
    }
    writeOperationLiteralToExistingCell(input);
}
function getVisibleOperationCellColumn(workbook, sheetId, cellIndex) {
    const sheet = workbook.getSheetById(sheetId);
    if (!sheet || sheet.structureVersion === 1) {
        return workbook.cellStore.cols[cellIndex];
    }
    return sheet.logical.getCellVisiblePosition(cellIndex)?.col ?? workbook.cellStore.cols[cellIndex];
}
export function canSkipOperationFormulaColumnVersion(input) {
    const sheetId = input.workbook.cellStore.sheetIds[input.cellIndex];
    if (sheetId === undefined) {
        return false;
    }
    const col = getVisibleOperationCellColumn(input.workbook, sheetId, input.cellIndex);
    return col !== undefined && !input.hasTrackedColumnDependents(sheetId, col);
}
export function cellsShareOperationVersionColumn(input) {
    const cellStore = input.workbook.cellStore;
    const leftSheetId = cellStore.sheetIds[input.leftCellIndex];
    if (leftSheetId === undefined || leftSheetId !== cellStore.sheetIds[input.rightCellIndex]) {
        return false;
    }
    const leftCol = getVisibleOperationCellColumn(input.workbook, leftSheetId, input.leftCellIndex);
    const rightCol = getVisibleOperationCellColumn(input.workbook, leftSheetId, input.rightCellIndex);
    return leftCol !== undefined && leftCol === rightCol;
}
//# sourceMappingURL=operation-literal-write-helpers.js.map