import { rethrowFatalFormulaBindingError } from './formula-binding-error-policy.js';
export function rebuildAllFormulaBindingsNow(args) {
    const pending = [...args.serviceArgs.state.formulas.entries()].map(([cellIndex, formula]) => ({
        cellIndex,
        source: formula.source,
        dependencyIndices: [...formula.dependencyIndices],
        planId: formula.planId,
    }));
    pending.forEach(({ planId }) => {
        args.serviceArgs.compiledPlans.release(planId);
    });
    args.serviceArgs.state.formulas.clear();
    args.serviceArgs.formulaInstances.clear();
    args.serviceArgs.state.ranges.reset();
    args.serviceArgs.edgeArena.reset();
    args.serviceArgs.programArena.reset();
    args.serviceArgs.constantArena.reset();
    args.serviceArgs.rangeListArena.reset();
    args.serviceArgs.reverseState.reverseCellEdges.length = 0;
    args.serviceArgs.reverseState.reverseRangeEdges.length = 0;
    args.serviceArgs.reverseState.reverseDefinedNameEdges.clear();
    args.serviceArgs.reverseState.reverseTableEdges.clear();
    args.serviceArgs.reverseState.reverseSpillEdges.clear();
    args.serviceArgs.reverseState.reverseAggregateColumnEdges.clear();
    args.serviceArgs.reverseState.reverseExactLookupColumnEdges.clear();
    args.serviceArgs.reverseState.reverseSortedLookupColumnEdges.clear();
    args.clearFormulaBookkeepingNow();
    args.serviceArgs.regionGraph.reset();
    const activeCellIndices = [];
    pending.forEach(({ cellIndex, source }) => {
        if (!args.isCellIndexMappedNow(cellIndex)) {
            args.serviceArgs.state.workbook.pruneCellIfEmpty(cellIndex);
            return;
        }
        const ownerSheetName = args.serviceArgs.state.workbook.getSheetNameById(args.serviceArgs.state.workbook.cellStore.sheetIds[cellIndex]);
        if (!ownerSheetName || !args.serviceArgs.state.workbook.getSheet(ownerSheetName)) {
            return;
        }
        try {
            args.bindFormulaNow(cellIndex, ownerSheetName, source);
        }
        catch (error) {
            rethrowFatalFormulaBindingError(error);
            args.invalidateFormulaNow(cellIndex);
        }
        activeCellIndices.push(cellIndex);
    });
    args.pruneOrphanedDependencyCells(pending.flatMap(({ dependencyIndices }) => dependencyIndices));
    return activeCellIndices;
}
//# sourceMappingURL=formula-binding-rebuild.js.map