import { emptyValue } from '../../engine-value-utils.js';
import { mapStructuralAxisIndex } from '../../engine-structural-utils.js';
export function collectStructuralRangeDependencies(args, formulaCellIndices) {
    const rangeIndices = new Set();
    formulaCellIndices.forEach((cellIndex) => {
        const formula = args.state.formulas.get(cellIndex);
        if (!formula) {
            return;
        }
        formula.rangeDependencies.forEach((rangeIndex) => {
            rangeIndices.add(rangeIndex);
        });
    });
    return [...rangeIndices];
}
export function clearSpillArtifactsForSheet(args, sheetName) {
    const changedCellIndices = [];
    const ownerCellIndices = [];
    args.state.workbook.listSpills().forEach((spill) => {
        if (spill.sheetName !== sheetName) {
            return;
        }
        const ownerCellIndex = args.state.workbook.getCellIndex(spill.sheetName, spill.address);
        if (ownerCellIndex === undefined) {
            args.state.workbook.deleteSpill(spill.sheetName, spill.address);
            return;
        }
        ownerCellIndices.push(ownerCellIndex);
        changedCellIndices.push(...args.clearOwnedSpill(ownerCellIndex));
    });
    return { changedCellIndices, ownerCellIndices };
}
export function clearPivotOutputsForSheet(args, sheetName) {
    args.state.workbook
        .listPivots()
        .filter((pivot) => pivot.sheetName === sheetName)
        .forEach((pivot) => {
        args.clearOwnedPivot(pivot);
    });
}
function clearDerivedCellArtifacts(args, cellIndex) {
    args.state.pivotOutputOwners.delete(cellIndex);
}
export function clearRemovedCellRuntimeState(args, cellIndex) {
    const flags = args.state.workbook.cellStore.flags[cellIndex] ?? 0;
    const hasDerivedState = (flags & (2 /* CellFlags.HasFormula */ | 4 /* CellFlags.JsOnly */ | 8 /* CellFlags.InCycle */ | 64 /* CellFlags.SpillChild */ | 128 /* CellFlags.PivotOutput */)) !== 0 ||
        args.state.workbook.getCellFormat(cellIndex) !== undefined ||
        args.state.pivotOutputOwners.has(cellIndex);
    if (!hasDerivedState) {
        args.state.workbook.cellStore.flags[cellIndex] = flags & ~16 /* CellFlags.Materialized */;
        return;
    }
    clearDerivedCellArtifacts(args, cellIndex);
    args.removeFormula(cellIndex);
    args.state.workbook.setCellFormat(cellIndex, null);
    args.state.workbook.cellStore.setValue(cellIndex, emptyValue());
    args.state.workbook.cellStore.flags[cellIndex] =
        flags &
            ~(16 /* CellFlags.Materialized */ | 2 /* CellFlags.HasFormula */ | 4 /* CellFlags.JsOnly */ | 8 /* CellFlags.InCycle */ | 64 /* CellFlags.SpillChild */ | 128 /* CellFlags.PivotOutput */);
}
export function isCellIndexMapped(args, cellIndex) {
    const sheetId = args.state.workbook.cellStore.sheetIds[cellIndex];
    const position = args.state.workbook.getCellPosition(cellIndex);
    if (sheetId === undefined || !position || !Number.isFinite(position.row) || !Number.isFinite(position.col)) {
        return false;
    }
    const sheet = args.state.workbook.getSheetById(sheetId);
    return ((sheet?.structureVersion === 1 ? sheet.grid.getPhysical(position.row, position.col) : sheet?.grid.get(position.row, position.col)) ===
        cellIndex);
}
export function structuralAxisIndexAffected(axisIndex, transform) {
    const nextIndex = mapStructuralAxisIndex(axisIndex, transform);
    return nextIndex === undefined || nextIndex !== axisIndex;
}
//# sourceMappingURL=structure-runtime-cleanup.js.map