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