import { makeRangeEntity } from '../../entity-ids.js';
import { syncFormulaBindingRangeDependencyEdges } from './formula-binding-reverse-edges.js';
export function createFormulaBindingRangeDependencyUpdater(args) {
    const makeRangeMaterializer = () => ({
        ensureCell: (sheetId, row, col) => args.ensureCellTrackedByCoords(sheetId, row, col),
        forEachSheetCell: (sheetId, fn) => args.forEachSheetCell(sheetId, fn),
        isFormulaCell: (cellIndex) => (args.state.workbook.cellStore.formulaIds[cellIndex] ?? 0) !== 0,
    });
    const syncRangeDependencyEdges = (rangeIndex, deps) => {
        syncFormulaBindingRangeDependencyEdges(args.reverseState, args.edgeArena, makeRangeEntity(rangeIndex), deps);
    };
    return {
        refreshRangeDependenciesNow(rangeIndices) {
            const refreshed = new Set();
            const materializer = makeRangeMaterializer();
            rangeIndices.forEach((rangeIndex) => {
                if (refreshed.has(rangeIndex)) {
                    return;
                }
                refreshed.add(rangeIndex);
                syncRangeDependencyEdges(rangeIndex, args.state.ranges.refresh(rangeIndex, materializer));
            });
            if (refreshed.size > 0) {
                args.scheduleWasmProgramSync();
            }
        },
        retargetRangeDependenciesNow(transaction, rangeIndices) {
            const touched = args.state.ranges.applyStructuralTransaction(transaction, rangeIndices, makeRangeMaterializer());
            touched.forEach(({ rangeIndex, oldDependencySources, newDependencySources }) => {
                syncRangeDependencyEdges(rangeIndex, { oldDependencySources, newDependencySources });
            });
            if (touched.length > 0) {
                args.scheduleWasmProgramSync();
            }
        },
    };
}
//# sourceMappingURL=formula-binding-range-dependencies.js.map