import { renameFormulaSheetReferences, rewriteCompiledFormulaForStructuralTransform, rewriteFormulaForStructuralTransform, } from '@bilig/formula';
function getRuntimeFormulaBaseSource(formula) {
    let source = formula.source;
    if (formula.sourceRenameTransform !== undefined) {
        source = renameFormulaSheetReferences(source, formula.sourceRenameTransform.oldSheetName, formula.sourceRenameTransform.newSheetName);
    }
    formula.sourceRenameTransforms?.forEach((transform) => {
        source = renameFormulaSheetReferences(source, transform.oldSheetName, transform.newSheetName);
    });
    return source;
}
export function getRuntimeFormulaSource(formula, inheritedStructuralSourceTransform) {
    const source = getRuntimeFormulaBaseSource(formula);
    const deferred = formula.structuralSourceTransform ?? inheritedStructuralSourceTransform;
    if (!deferred) {
        return source;
    }
    return rewriteFormulaForStructuralTransform(source, deferred.ownerSheetName, deferred.targetSheetName, deferred.transform);
}
export function getRuntimeFormulaStructuralCompiled(formula, inheritedStructuralSourceTransform) {
    const deferred = formula.structuralSourceTransform ?? inheritedStructuralSourceTransform;
    if (!deferred) {
        return undefined;
    }
    return rewriteCompiledFormulaForStructuralTransform(formula.compiled, deferred.ownerSheetName, deferred.targetSheetName, deferred.transform).compiled;
}
//# sourceMappingURL=runtime-formula-source.js.map