import { translateSimpleDirectScalarFormula, translateSimpleDirectScalarFormulaWithParsedRefs, translateTrustedSimpleDirectScalarFormula, translateTrustedSimpleDirectScalarFormulaWithResultOffset, } from '../../formula/simple-direct-scalar-compile.js';
import { translateInitialPrefixSumFormula, tryBuildInitialPrefixSumTemplateKey, tryBuildInitialSimpleRowRelativeBinaryTemplate, } from './formula-initialization-template-keys.js';
export function createInitialTemplateFormulaResolver(compileTemplateFormula) {
    const simpleTemplateCache = new Map();
    return (source, row, col) => {
        const simpleTemplate = tryBuildInitialSimpleRowRelativeBinaryTemplate(source, row, col);
        const templateKey = simpleTemplate?.key;
        const cached = templateKey === undefined ? undefined : simpleTemplateCache.get(templateKey);
        if (cached && simpleTemplate !== undefined) {
            const anchorRowDelta = row - cached.anchorRow;
            const anchorColDelta = col - cached.anchorCol;
            const compiled = simpleTemplate.usesRowLiteralSuffix
                ? (translateTrustedSimpleDirectScalarFormulaWithResultOffset(cached.anchorCompiled, anchorRowDelta, anchorColDelta, source, row + 1) ?? translateSimpleDirectScalarFormula(cached.anchorCompiled, anchorRowDelta, anchorColDelta, source))
                : (translateSimpleDirectScalarFormulaWithParsedRefs(cached.anchorCompiled, source, simpleTemplate.parsedRefs) ??
                    translateTrustedSimpleDirectScalarFormula(cached.anchorCompiled, anchorRowDelta, anchorColDelta, source));
            if (compiled) {
                return {
                    ...cached.resolution,
                    compiled,
                    translated: cached.resolution.translated || anchorRowDelta !== 0 || anchorColDelta !== 0,
                    rowDelta: cached.resolution.rowDelta + anchorRowDelta,
                    colDelta: cached.resolution.colDelta + anchorColDelta,
                };
            }
        }
        const sumTemplateKey = tryBuildInitialPrefixSumTemplateKey(source, row, col);
        const cachedSum = sumTemplateKey === undefined ? undefined : simpleTemplateCache.get(sumTemplateKey.key);
        if (cachedSum && sumTemplateKey !== undefined) {
            return translateInitialPrefixSumFormula(cachedSum, source, row, col, sumTemplateKey);
        }
        const resolution = compileTemplateFormula(source, row, col);
        if (templateKey !== undefined) {
            simpleTemplateCache.set(templateKey, {
                resolution,
                anchorRow: row,
                anchorCol: col,
                anchorCompiled: resolution.compiled,
            });
        }
        if (sumTemplateKey !== undefined) {
            simpleTemplateCache.set(sumTemplateKey.key, {
                resolution,
                anchorRow: row,
                anchorCol: col,
                anchorCompiled: resolution.compiled,
            });
        }
        return resolution;
    };
}
//# sourceMappingURL=formula-initialization-template-resolver.js.map