import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
const GetMenuItemContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
GetMenuItemContext.displayName = 'GetMenuItemContext';
const GetMenuItemsContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
GetMenuItemsContext.displayName = 'GetMenuItemsContext';
const useCollectMenuItem = ()=>(0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(GetMenuItemContext) || {};
const useCollectMenuItems = ()=>(0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(GetMenuItemsContext) || {};
const useMenuItem = ()=>{
    const list = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)([]);
    const renderItems = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    const shouldRerender = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(false);
    const Component = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((param)=>{
        let { limitCount } = param;
        if (!shouldRerender.current) return null;
        shouldRerender.current = false;
        if (renderItems.current) return renderItems.current();
        if (limitCount && list.current.length > limitCount) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
            children: list.current.slice(0, limitCount).map((Com, index)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Com, {}, index))
        });
        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
            children: list.current.map((Com, index)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Com, {}, index))
        });
    }, []);
    const getMenuItems = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((Com)=>{
        const items = [];
        const pushMenuItem = (item)=>{
            items.push(item);
            items.sort((a, b)=>(a.order || 0) - (b.order || 0));
        };
        shouldRerender.current = true;
        renderItems.current = ()=>{
            const notDeleteItems = items.filter((item)=>item.notdelete).map((item)=>__WEBPACK_EXTERNAL_MODULE_lodash__["default"].omit(item, 'notdelete'));
            items.length = 0;
            items.push(...notDeleteItems);
            return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(GetMenuItemsContext.Provider, {
                value: {
                    pushMenuItem
                },
                children: Com()
            });
        };
        return items;
    }, []);
    const getMenuItem = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((Com)=>{
        const item = {};
        const collectMenuItem = (menuItem)=>{
            Object.assign(item, menuItem);
        };
        shouldRerender.current = true;
        list.current.push(()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(GetMenuItemContext.Provider, {
                value: {
                    collectMenuItem
                },
                children: Com()
            }));
        return item;
    }, []);
    const clean = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        list.current = [];
    }, []);
    return {
        Component,
        getMenuItems,
        getMenuItem,
        clean
    };
};
export { GetMenuItemContext, GetMenuItemsContext, useCollectMenuItem, useCollectMenuItems, useMenuItem };
