import React from "react";
import { evaluateExpression } from "../utils/expressionParser";
import { template } from "./useTemplate";
import { useVariablesContext } from "@qorebase/app-component";
export const evaluate = (formula, values) => {
    if (typeof formula === "string")
        return template(formula, values);
    const variables = Object.entries(values).map(([name, value]) => ({
        name,
        type: "any",
        evaluate: () => value,
    }));
    const result = evaluateExpression(formula.formula, variables);
    return result.type === "ok" ? result.result : "#Error!";
};
export const useEvaluate = (formula) => {
    const { values } = useVariablesContext();
    return React.useMemo(() => evaluate(formula, values), [formula, values]);
};
export const useEvaluateObject = (formulas) => {
    const { values } = useVariablesContext();
    return React.useMemo(() => {
        const obj = {};
        for (const key in formulas) {
            obj[key] = evaluate(formulas[key], values);
        }
        return obj;
    }, [values, formulas]);
};
export const useEvaluateLazy = () => {
    const { values } = useVariablesContext();
    return React.useCallback((formula, additionalValues) => evaluate(formula, Object.assign(Object.assign({}, values), additionalValues)), [values]);
};
//# sourceMappingURL=useExpression.js.map