/**
 * Hook for evaluating computed fields based on form data
 * Uses ref-based cache to prevent infinite loops
 */
import { useEffect, useRef } from 'react';
import { evaluate_formula } from './formula.js';
export function use_computed_fields({ form_data, computed_fields, on_computed_value_change, enabled = true, }) {
    const cache_ref = useRef({});
    const callback_ref = useRef(on_computed_value_change);
    callback_ref.current = on_computed_value_change;
    useEffect(() => {
        if (!enabled || computed_fields.length === 0)
            return;
        for (const def of computed_fields) {
            const result = evaluate_formula(def.formula, form_data);
            const cached = cache_ref.current[def.field_id];
            if (cached !== result) {
                cache_ref.current[def.field_id] = result;
                callback_ref.current?.(def.field_id, result);
            }
        }
    }, [form_data, computed_fields, enabled]);
}
//# sourceMappingURL=use_computed_fields.js.map