"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (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 = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (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__, {
    useGetSchemaInitializerMenuItems: ()=>useGetSchemaInitializerMenuItems,
    useSchemaInitializerRender: ()=>useSchemaInitializerRender,
    useSchemaInitializerMenuItems: ()=>useSchemaInitializerMenuItems,
    useAriaAttributeOfMenuItem: ()=>external_useAriaAttributeOfMenuItem_js_namespaceObject.useAriaAttributeOfMenuItem
});
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const index_js_namespaceObject = require("../../../schema-component/index.js");
const external_hooks_index_js_namespaceObject = require("../../hooks/index.js");
const external_components_index_js_namespaceObject = require("../components/index.js");
const SchemaInitializerButton_js_namespaceObject = require("../components/SchemaInitializerButton.js");
const external_hoc_index_js_namespaceObject = require("../hoc/index.js");
const external_useAriaAttributeOfMenuItem_js_namespaceObject = require("./useAriaAttributeOfMenuItem.js");
function useSchemaInitializerMenuItems(items, name, onClick) {
    const getMenuItems = useGetSchemaInitializerMenuItems(onClick);
    return (0, external_react_namespaceObject.useMemo)(()=>getMenuItems(items, name), [
        getMenuItems,
        items,
        name
    ]);
}
function useGetSchemaInitializerMenuItems(onClick) {
    const compile = (0, index_js_namespaceObject.useCompile)();
    const getMenuItems = (0, external_react_namespaceObject.useCallback)((items, parentKey)=>{
        if (!(null == items ? void 0 : items.length)) return [];
        return items.map((item, indexA)=>{
            const ItemComponent = item.component || item.Component;
            let element;
            const compiledTitle = item.title || item.label ? compile(item.title || item.label) : void 0;
            if (ItemComponent) {
                element = /*#__PURE__*/ external_react_default().createElement(external_components_index_js_namespaceObject.SchemaInitializerChild, {
                    ...item,
                    title: compiledTitle
                });
                if (!element) return;
            }
            if ('divider' === item.type) return {
                type: 'divider',
                key: item.key || "divider-".concat(indexA)
            };
            if ('item' === item.type && ItemComponent) {
                if (!item.key) item.key = "".concat(compiledTitle, "-").concat(indexA);
                return item.associationField ? {
                    key: item.key,
                    label: element,
                    associationField: item.associationField
                } : {
                    key: item.key,
                    label: element
                };
            }
            if ('itemGroup' === item.type) {
                const label = 'string' == typeof compiledTitle ? compiledTitle : item.title;
                const key = item.key || "".concat(parentKey, "-item-group-").concat(indexA);
                return {
                    type: 'group',
                    key,
                    label,
                    children: (null == item ? void 0 : item.children.length) ? getMenuItems(item.children, key) : []
                };
            }
            if ('subMenu' === item.type) {
                const label = compiledTitle;
                const key = item.key || item.name || "".concat(parentKey, "-sub-menu-").concat(indexA);
                return {
                    key,
                    label,
                    children: (null == item ? void 0 : item.children.length) ? getMenuItems(item.children, key) : []
                };
            }
            if (item.isMenuType) {
                const { isMenuType, ...menuData } = item;
                return menuData;
            }
            const label = element || compiledTitle || item.label;
            const key = item.key || "".concat(parentKey, "-").concat(compiledTitle, "-").concat(indexA);
            const handleClick = (info)=>{
                info.domEvent.stopPropagation();
                if (info.key !== key) return;
                if (item.onClick) item.onClick({
                    ...info,
                    item
                });
                else null == onClick || onClick({
                    ...info,
                    item
                });
            };
            return item.associationField ? {
                key,
                label,
                associationField: item.associationField,
                onClick: handleClick
            } : {
                key,
                label,
                onClick: handleClick
            };
        });
    }, [
        compile,
        onClick
    ]);
    return getMenuItems;
}
const InitializerComponent = /*#__PURE__*/ external_react_default().memo((options)=>{
    const Component = options.Component || SchemaInitializerButton_js_namespaceObject.SchemaInitializerButton;
    const ItemsComponent = options.ItemsComponent || external_components_index_js_namespaceObject.SchemaInitializerItems;
    const itemsComponentProps = {
        ...options.itemsComponentProps,
        options,
        items: options.items,
        style: options.itemsComponentStyle
    };
    const C = (0, external_react_namespaceObject.useMemo)(()=>(0, external_hoc_index_js_namespaceObject.withInitializer)(Component), [
        Component
    ]);
    return /*#__PURE__*/ external_react_default().createElement(C, options, /*#__PURE__*/ external_react_default().createElement(ItemsComponent, itemsComponentProps));
});
InitializerComponent.displayName = 'InitializerComponent';
function useSchemaInitializerRender(name, options) {
    const app = (0, external_hooks_index_js_namespaceObject.useApp)();
    const renderCache = external_react_default().useRef({});
    const initializer = (0, external_react_namespaceObject.useMemo)(()=>app.schemaInitializerManager.get(name), [
        app.schemaInitializerManager,
        name
    ]);
    const res = (0, external_react_namespaceObject.useMemo)(()=>{
        if (!name) return {
            exists: false,
            render: ()=>null
        };
        if (!initializer) {
            console.error('[tachybase]: SchemaInitializer "'.concat(name, '" not found'));
            return {
                exists: false,
                render: ()=>null
            };
        }
        return {
            exists: true,
            render: (props)=>{
                const key = JSON.stringify(props) || '{}';
                if (renderCache.current[key]) return renderCache.current[key];
                return renderCache.current[key] = /*#__PURE__*/ external_react_default().createElement(InitializerComponent, {
                    ...initializer.options,
                    ...options,
                    ...props
                });
            }
        };
    }, [
        initializer,
        name,
        options
    ]);
    return res;
}
exports.useAriaAttributeOfMenuItem = __webpack_exports__.useAriaAttributeOfMenuItem;
exports.useGetSchemaInitializerMenuItems = __webpack_exports__.useGetSchemaInitializerMenuItems;
exports.useSchemaInitializerMenuItems = __webpack_exports__.useSchemaInitializerMenuItems;
exports.useSchemaInitializerRender = __webpack_exports__.useSchemaInitializerRender;
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
    "useAriaAttributeOfMenuItem",
    "useGetSchemaInitializerMenuItems",
    "useSchemaInitializerMenuItems",
    "useSchemaInitializerRender"
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
Object.defineProperty(exports, '__esModule', {
    value: true
});
