import { addEngineCounter } from '../../perf/engine-counters.js';
import { readFreshFormulaFamilyRunsFromRefs } from './formula-initialization-family-runs.js';
export function readAlignedFreshFormulaFamilyRunsFromRefs(args) {
    const freshFormulaFamilyRuns = readFreshFormulaFamilyRunsFromRefs(args.refs);
    if (args.hadExistingFormulas && freshFormulaFamilyRuns === undefined) {
        return undefined;
    }
    const alignedFreshFormulaFamilyRuns = freshFormulaFamilyRuns?.runs;
    if (freshFormulaFamilyRuns?.fallbackCount) {
        addEngineCounter(args.counters, 'formulaFamilyRuntimeRunFallbacks', freshFormulaFamilyRuns.fallbackCount);
    }
    if (alignedFreshFormulaFamilyRuns !== undefined) {
        addEngineCounter(args.counters, 'formulaFamilyRuntimeRunsRestored', alignedFreshFormulaFamilyRuns.length);
        addEngineCounter(args.counters, 'formulaFamilyRuntimeRunMembersRestored', alignedFreshFormulaFamilyRuns.reduce((sum, run) => sum + run.cellIndices.length, 0));
    }
    return alignedFreshFormulaFamilyRuns;
}
export function flushAlignedFreshFormulaFamilyRuns(args) {
    if (args.runs === undefined) {
        return false;
    }
    if (args.shouldDeferFormulaFamilyIndex) {
        if (args.deferFormulaFamilyIndexRuns) {
            args.deferFormulaFamilyIndexRuns(args.runs);
        }
        else {
            args.deferFormulaFamilyIndexRebuild?.();
        }
        return true;
    }
    args.runs.forEach((run) => {
        args.checkEvaluationBudget?.();
        args.registerFormulaFamilyRun(run);
    });
    return true;
}
//# sourceMappingURL=formula-initialization-restored-family-runs.js.map