"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    DynamicSchemaProps: ()=>DynamicSchemaProps,
    withDynamicSchemaProps: ()=>withDynamicSchemaProps
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const index_js_namespaceObject = require("../../schema-component/index.js");
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, index_js_namespaceObject.useDesignable)();
        const scope = (0, schema_namespaceObject.useExpressionScope)();
        const useComponentPropsStr = (0, external_react_namespaceObject.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, external_react_namespaceObject.useMemo)(()=>{
            const getHook = (str, scope, allText)=>{
                let res;
                if (external_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 (external_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, external_react_namespaceObject.useMemo)(()=>({
                ...props,
                ...schemaProps
            }), [
            schemaProps,
            props
        ]);
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.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, index_js_namespaceObject.useDesignable)();
            const scope = (0, schema_namespaceObject.useExpressionScope)();
            const useComponentPropsStr = (0, external_react_namespaceObject.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, external_react_namespaceObject.useMemo)(()=>{
                const getHook = (str, scope, allText)=>{
                    let res;
                    if (external_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 (external_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, external_react_namespaceObject.useMemo)(()=>({
                    ...props,
                    ...schemaProps
                }), [
                schemaProps,
                props
            ]);
            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Component, {
                ...memoProps,
                children: props.children
            });
        };
        Component.displayName = displayName;
        ComponentWithProps.displayName = "withSchemaProps(".concat(displayName, ")");
        return ComponentWithProps;
    };
}
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
