import React from "react";
import { evaluateExpression } from "../utils/expressionParser";
import { useVariablesContext } from "@qorebase/app-component";
export default function useDisableCondition(instance) {
    const { values } = useVariablesContext();
    const variablesConfig = React.useMemo(() => Object.entries(values).map(([name, value]) => ({
        name,
        type: "any",
        evaluate: () => value,
    })), [values]);
    const isDisabled = React.useMemo(() => {
        var _a, _b, _c;
        if (!instance ||
            !instance.disableCondition ||
            (instance.disableCondition &&
                ((_a = instance.disableCondition) === null || _a === void 0 ? void 0 : _a.conditions.length) === 0))
            return false;
        const results = (_b = instance.disableCondition) === null || _b === void 0 ? void 0 : _b.conditions.map((condition) => {
            const result = evaluateExpression(condition.formula, variablesConfig);
            return Boolean(result.type === "ok" && result.result);
        });
        return ((_c = instance.disableCondition) === null || _c === void 0 ? void 0 : _c.type) === "all"
            ? results.every((result) => result)
            : results.some((result) => result);
    }, [instance, variablesConfig]);
    return { isDisabled };
}
//# sourceMappingURL=useDisableCondition.js.map