import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ 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 } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
        const scope = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useExpressionScope)();
        const useComponentPropsStr = (0, __WEBPACK_EXTERNAL_MODULE_react__.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 = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
            const getHook = (str, scope, allText)=>{
                let res;
                if (__WEBPACK_EXTERNAL_MODULE_lodash__["default"].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 (__WEBPACK_EXTERNAL_MODULE_lodash__["default"].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 = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
                ...props,
                ...schemaProps
            }), [
            schemaProps,
            props
        ]);
        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.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 } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
            const scope = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useExpressionScope)();
            const useComponentPropsStr = (0, __WEBPACK_EXTERNAL_MODULE_react__.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 = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
                const getHook = (str, scope, allText)=>{
                    let res;
                    if (__WEBPACK_EXTERNAL_MODULE_lodash__["default"].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 (__WEBPACK_EXTERNAL_MODULE_lodash__["default"].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 = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
                    ...props,
                    ...schemaProps
                }), [
                schemaProps,
                props
            ]);
            return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Component, {
                ...memoProps,
                children: props.children
            });
        };
        Component.displayName = displayName;
        ComponentWithProps.displayName = "withSchemaProps(".concat(displayName, ")");
        return ComponentWithProps;
    };
}
export { DynamicSchemaProps, withDynamicSchemaProps };
