import { useMemo } from "react";
import { useExpressionScope } from "@tachybase/schema";
import { useCompile } from "../../../hooks/index.mjs";
import { useA } from "../hooks.mjs";
const useCompiledAction = (useAction, actionCallback)=>{
    const compile = useCompile();
    const scope = useExpressionScope();
    const compiledUseAction = useMemo(()=>{
        if ('function' == typeof useAction) return useAction;
        if ('string' == typeof useAction && useAction.startsWith('{{')) {
            const match = useAction.match(/\{\{\s*([^}]+)\s*\}\}/);
            if (match && match[1]) {
                const expression = match[1].trim();
                const func = scope[expression];
                if ('function' == typeof func) return func;
                const compiled = compile(useAction);
                if ('function' == typeof compiled) return compiled;
                console.warn("useAction not found in scope or compilation failed: ".concat(expression), scope);
                return useA;
            }
            const compiled = compile(useAction);
            if ('function' == typeof compiled) return compiled;
            console.warn("useAction compilation failed: ".concat(useAction), compiled);
            return useA;
        }
        console.warn("useAction is not a function or string reference:", useAction);
        return useA;
    }, [
        useAction,
        compile,
        scope
    ]);
    return compiledUseAction(actionCallback);
};
export { useCompiledAction };
