import { jsx } from "react/jsx-runtime";
import { useMemo } from "react";
import { useExpressionScope } from "@tachybase/schema";
import lodash from "lodash";
import { useDesignable } from "../../schema-component/index.mjs";
const useDefaultSchemaProps = ()=>void 0;
function withDynamicSchemaProps(Component) {
    let options = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
    const displayName = options.displayName || Component.displayName || Component.name;
    const ComponentWithProps = (props)=>{
        const { dn, findComponent } = useDesignable();
        const scope = useExpressionScope();
        const useComponentPropsStr = useMemo(()=>{
            const xComponent = dn.getSchemaAttribute('x-component');
            const xDecorator = dn.getSchemaAttribute('x-decorator');
            const xUseComponentProps = dn.getSchemaAttribute('x-use-component-props');
            const xUseDecoratorProps = dn.getSchemaAttribute('x-use-decorator-props');
            if (xComponent && xUseComponentProps && findComponent(xComponent) === ComponentWithProps) return xUseComponentProps;
            if (xDecorator && xUseDecoratorProps && findComponent(xDecorator) === ComponentWithProps) return xUseDecoratorProps;
        }, [
            dn
        ]);
        const useSchemaProps = useMemo(()=>{
            const getHook = (str, scope, allText)=>{
                let res;
                if (lodash.isFunction(str)) res = str;
                else {
                    res = scope[str];
                    if (!res) console.error("".concat(allText, " is not registered"));
                }
                return res || useDefaultSchemaProps;
            };
            if (!useComponentPropsStr) return useDefaultSchemaProps;
            if (lodash.isFunction(useComponentPropsStr)) return useComponentPropsStr;
            const pathList = useComponentPropsStr.split('.');
            let result;
            for (const item of pathList)result = getHook(item, result || scope, useComponentPropsStr);
            return result;
        }, [
            scope,
            useComponentPropsStr
        ]);
        const schemaProps = useSchemaProps(props);
        const memoProps = useMemo(()=>({
                ...props,
                ...schemaProps
            }), [
            schemaProps,
            props
        ]);
        return /*#__PURE__*/ jsx(Component, {
            ...memoProps,
            children: props.children
        });
    };
    Component.displayName = displayName;
    ComponentWithProps.displayName = "withSchemaProps(".concat(displayName, ")");
    return ComponentWithProps;
}
function DynamicSchemaProps() {
    let options = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
    return (Component, _context)=>{
        const displayName = options.displayName || Component.displayName || Component.name;
        const ComponentWithProps = (props)=>{
            const { dn, findComponent } = useDesignable();
            const scope = useExpressionScope();
            const useComponentPropsStr = useMemo(()=>{
                const xComponent = dn.getSchemaAttribute('x-component');
                const xDecorator = dn.getSchemaAttribute('x-decorator');
                const xUseComponentProps = dn.getSchemaAttribute('x-use-component-props');
                const xUseDecoratorProps = dn.getSchemaAttribute('x-use-decorator-props');
                if (xComponent && xUseComponentProps && findComponent(xComponent) === ComponentWithProps) return xUseComponentProps;
                if (xDecorator && xUseDecoratorProps && findComponent(xDecorator) === ComponentWithProps) return xUseDecoratorProps;
            }, [
                dn
            ]);
            const useSchemaProps = useMemo(()=>{
                const getHook = (str, scope, allText)=>{
                    let res;
                    if (lodash.isFunction(str)) res = str;
                    else {
                        res = scope[str];
                        if (!res) console.error("".concat(allText, " is not registered"));
                    }
                    return res || useDefaultSchemaProps;
                };
                if (!useComponentPropsStr) return useDefaultSchemaProps;
                if (lodash.isFunction(useComponentPropsStr)) return useComponentPropsStr;
                const pathList = useComponentPropsStr.split('.');
                let result;
                for (const item of pathList)result = getHook(item, result || scope, useComponentPropsStr);
                return result;
            }, [
                scope,
                useComponentPropsStr
            ]);
            const schemaProps = useSchemaProps(props);
            const memoProps = useMemo(()=>({
                    ...props,
                    ...schemaProps
                }), [
                schemaProps,
                props
            ]);
            return /*#__PURE__*/ jsx(Component, {
                ...memoProps,
                children: props.children
            });
        };
        Component.displayName = displayName;
        ComponentWithProps.displayName = "withSchemaProps(".concat(displayName, ")");
        return ComponentWithProps;
    };
}
export { DynamicSchemaProps, withDynamicSchemaProps };
