import { getRuntimeFormulaSource } from '../runtime-formula-source.js';
import { collectTrackedDependents } from './formula-binding-dependency-helpers.js';
export function createFormulaBindingRebinds(args) {
    const rebindFormulaCellsNow = (candidates, formulaChangedCount) => {
        candidates.forEach((cellIndex) => {
            const formula = args.serviceArgs.state.formulas.get(cellIndex);
            const ownerSheetName = args.serviceArgs.state.workbook.getSheetNameById(args.serviceArgs.state.workbook.cellStore.sheetIds[cellIndex]);
            if (formula && ownerSheetName) {
                args.bindFormulaNow(cellIndex, ownerSheetName, getRuntimeFormulaSource(formula));
            }
            formulaChangedCount = args.serviceArgs.markFormulaChanged(cellIndex, formulaChangedCount);
        });
        return formulaChangedCount;
    };
    const rebindTrackedDependentsNow = (registry, keys, formulaChangedCount) => rebindFormulaCellsNow(collectTrackedDependents(registry, keys), formulaChangedCount);
    const rebindFormulasForSheetNow = (sheetName, formulaChangedCount, candidates) => {
        if (candidates) {
            for (let index = 0; index < candidates.length; index += 1) {
                const cellIndex = candidates[index];
                const formula = args.serviceArgs.state.formulas.get(cellIndex);
                if (!formula) {
                    continue;
                }
                const ownerSheetName = args.serviceArgs.state.workbook.getSheetNameById(args.serviceArgs.state.workbook.cellStore.sheetIds[cellIndex]);
                if (!ownerSheetName) {
                    continue;
                }
                const touchesSheet = formula.compiled.deps.some((dep) => {
                    if (!dep.includes('!')) {
                        return false;
                    }
                    const [qualifiedSheet] = dep.split('!');
                    return qualifiedSheet?.replace(/^'(.*)'$/, '$1') === sheetName;
                });
                if (!touchesSheet) {
                    continue;
                }
                args.bindFormulaNow(cellIndex, ownerSheetName, getRuntimeFormulaSource(formula));
                formulaChangedCount = args.serviceArgs.markFormulaChanged(cellIndex, formulaChangedCount);
            }
            return formulaChangedCount;
        }
        args.serviceArgs.state.formulas.forEach((formula, cellIndex) => {
            if (!formula) {
                return;
            }
            const ownerSheetName = args.serviceArgs.state.workbook.getSheetNameById(args.serviceArgs.state.workbook.cellStore.sheetIds[cellIndex]);
            if (!ownerSheetName) {
                return;
            }
            const touchesSheet = formula.compiled.deps.some((dep) => {
                if (!dep.includes('!')) {
                    return false;
                }
                const [qualifiedSheet] = dep.split('!');
                return qualifiedSheet?.replace(/^'(.*)'$/, '$1') === sheetName;
            });
            if (!touchesSheet) {
                return;
            }
            args.bindFormulaNow(cellIndex, ownerSheetName, getRuntimeFormulaSource(formula));
            formulaChangedCount = args.serviceArgs.markFormulaChanged(cellIndex, formulaChangedCount);
        });
        return formulaChangedCount;
    };
    return {
        rebindFormulaCellsNow,
        rebindTrackedDependentsNow,
        rebindFormulasForSheetNow,
    };
}
//# sourceMappingURL=formula-binding-rebind.js.map