import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_index_mjs__ from "../components/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_SchemaInitializerButton_mjs__ from "../components/SchemaInitializerButton.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hoc_index_mjs__ from "../hoc/index.mjs";
export * from "./useAriaAttributeOfMenuItem.mjs";
function useSchemaInitializerMenuItems(items, name, onClick) {
    const getMenuItems = useGetSchemaInitializerMenuItems(onClick);
    return (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>getMenuItems(items, name), [
        getMenuItems,
        items,
        name
    ]);
}
function useGetSchemaInitializerMenuItems(onClick) {
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const getMenuItems = (0, __WEBPACK_EXTERNAL_MODULE_react__.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__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE__components_index_mjs__.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__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].memo((options)=>{
    const Component = options.Component || __WEBPACK_EXTERNAL_MODULE__components_SchemaInitializerButton_mjs__.SchemaInitializerButton;
    const ItemsComponent = options.ItemsComponent || __WEBPACK_EXTERNAL_MODULE__components_index_mjs__.SchemaInitializerItems;
    const itemsComponentProps = {
        ...options.itemsComponentProps,
        options,
        items: options.items,
        style: options.itemsComponentStyle
    };
    const C = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__hoc_index_mjs__.withInitializer)(Component), [
        Component
    ]);
    return /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(C, options, /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(ItemsComponent, itemsComponentProps));
});
InitializerComponent.displayName = 'InitializerComponent';
function useSchemaInitializerRender(name, options) {
    const app = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useApp)();
    const renderCache = __WEBPACK_EXTERNAL_MODULE_react__["default"].useRef({});
    const initializer = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>app.schemaInitializerManager.get(name), [
        app.schemaInitializerManager,
        name
    ]);
    const res = (0, __WEBPACK_EXTERNAL_MODULE_react__.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__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(InitializerComponent, {
                    ...initializer.options,
                    ...options,
                    ...props
                });
            }
        };
    }, [
        initializer,
        name,
        options
    ]);
    return res;
}
export { useGetSchemaInitializerMenuItems, useSchemaInitializerMenuItems, useSchemaInitializerRender };
