"use strict";
var __webpack_modules__ = {
    "../../../schema-component": function(module) {
        module.exports = require("../../../schema-component/index.js");
    },
    "../../hooks": function(module) {
        module.exports = require("../../hooks/index.js");
    },
    "../components/SchemaInitializerButton": function(module) {
        module.exports = require("../components/SchemaInitializerButton.js");
    },
    "../components": function(module) {
        module.exports = require("../components/index.js");
    },
    "../hoc": function(module) {
        module.exports = require("../hoc/index.js");
    },
    "./useAriaAttributeOfMenuItem": function(module) {
        module.exports = require("./useAriaAttributeOfMenuItem.js");
    },
    react: function(module) {
        module.exports = require("react");
    }
};
var __webpack_module_cache__ = {};
function __webpack_require__(moduleId) {
    var cachedModule = __webpack_module_cache__[moduleId];
    if (void 0 !== cachedModule) return cachedModule.exports;
    var module = __webpack_module_cache__[moduleId] = {
        exports: {}
    };
    __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
    return module.exports;
}
(()=>{
    __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__, {
    useGetSchemaInitializerMenuItems: function() {
        return useGetSchemaInitializerMenuItems;
    },
    useSchemaInitializerMenuItems: function() {
        return useSchemaInitializerMenuItems;
    },
    useSchemaInitializerRender: function() {
        return useSchemaInitializerRender;
    }
});
var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("react");
var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/ __webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
var _schema_component__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("../../../schema-component");
var _hooks__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__("../../hooks");
var _components__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__("../components");
var _components_SchemaInitializerButton__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__("../components/SchemaInitializerButton");
var _hoc__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__("../hoc");
var _useAriaAttributeOfMenuItem__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__("./useAriaAttributeOfMenuItem");
var __WEBPACK_REEXPORT_OBJECT__ = {};
for(var __WEBPACK_IMPORT_KEY__ in _useAriaAttributeOfMenuItem__WEBPACK_IMPORTED_MODULE_6__)if ([
    "useGetSchemaInitializerMenuItems",
    "default",
    "useSchemaInitializerRender",
    "useSchemaInitializerMenuItems"
].indexOf(__WEBPACK_IMPORT_KEY__) < 0) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
    return _useAriaAttributeOfMenuItem__WEBPACK_IMPORTED_MODULE_6__[key];
}).bind(0, __WEBPACK_IMPORT_KEY__);
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
function useSchemaInitializerMenuItems(items, name, onClick) {
    const getMenuItems = useGetSchemaInitializerMenuItems(onClick);
    return (0, react__WEBPACK_IMPORTED_MODULE_0__.useMemo)(()=>getMenuItems(items, name), [
        getMenuItems,
        items,
        name
    ]);
}
function useGetSchemaInitializerMenuItems(onClick) {
    const compile = (0, _schema_component__WEBPACK_IMPORTED_MODULE_1__.useCompile)();
    const getMenuItems = (0, react__WEBPACK_IMPORTED_MODULE_0__.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__*/ react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_components__WEBPACK_IMPORTED_MODULE_3__.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__*/ react__WEBPACK_IMPORTED_MODULE_0___default().memo((options)=>{
    const Component = options.Component || _components_SchemaInitializerButton__WEBPACK_IMPORTED_MODULE_4__.SchemaInitializerButton;
    const ItemsComponent = options.ItemsComponent || _components__WEBPACK_IMPORTED_MODULE_3__.SchemaInitializerItems;
    const itemsComponentProps = {
        ...options.itemsComponentProps,
        options,
        items: options.items,
        style: options.itemsComponentStyle
    };
    const C = (0, react__WEBPACK_IMPORTED_MODULE_0__.useMemo)(()=>(0, _hoc__WEBPACK_IMPORTED_MODULE_5__.withInitializer)(Component), [
        Component
    ]);
    return /*#__PURE__*/ react__WEBPACK_IMPORTED_MODULE_0___default().createElement(C, options, /*#__PURE__*/ react__WEBPACK_IMPORTED_MODULE_0___default().createElement(ItemsComponent, itemsComponentProps));
});
InitializerComponent.displayName = 'InitializerComponent';
function useSchemaInitializerRender(name, options) {
    const app = (0, _hooks__WEBPACK_IMPORTED_MODULE_2__.useApp)();
    const renderCache = react__WEBPACK_IMPORTED_MODULE_0___default().useRef({});
    const initializer = (0, react__WEBPACK_IMPORTED_MODULE_0__.useMemo)(()=>app.schemaInitializerManager.get(name), [
        app.schemaInitializerManager,
        name
    ]);
    const res = (0, react__WEBPACK_IMPORTED_MODULE_0__.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__*/ react__WEBPACK_IMPORTED_MODULE_0___default().createElement(InitializerComponent, {
                    ...initializer.options,
                    ...options,
                    ...props
                });
            }
        };
    }, [
        initializer,
        name,
        options
    ]);
    return res;
}
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
});
