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__tachybase_utils_client__ from "@tachybase/utils/client";
const CurrentUserSettingsMenuContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
CurrentUserSettingsMenuContext.displayName = 'CurrentUserSettingsMenuContext';
const useCurrentUserSettingsMenu = ()=>{
    const { menuItems } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(CurrentUserSettingsMenuContext) || {};
    const getMenuItems = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>menuItems.current, [
        menuItems
    ]);
    const addMenuItem = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((item, options)=>{
        let index;
        if (options) item._options = options;
        if (-1 !== (index = menuItems.current.findIndex((i)=>i.key === item.key))) {
            menuItems.current[index] = item;
            menuItems.current = [
                ...menuItems.current
            ];
            return;
        }
        if (options) {
            if (options.before) {
                const index = menuItems.current.findIndex((item)=>item.key === options.before);
                menuItems.current.splice(index, 0, item);
                return;
            }
            if (options.after) {
                const index = menuItems.current.findIndex((item)=>item.key === options.after);
                menuItems.current.splice(index + 1, 0, item);
                return;
            }
        }
        const oldItems = menuItems.current;
        menuItems.current = oldItems.filter((oldItem)=>{
            var _oldItem__options, _oldItem__options1;
            return item.key !== (null === (_oldItem__options = oldItem._options) || void 0 === _oldItem__options ? void 0 : _oldItem__options.before) && item.key !== (null === (_oldItem__options1 = oldItem._options) || void 0 === _oldItem__options1 ? void 0 : _oldItem__options1.after);
        });
        menuItems.current.push(...oldItems.filter((oldItem)=>{
            var _oldItem__options;
            return (null === (_oldItem__options = oldItem._options) || void 0 === _oldItem__options ? void 0 : _oldItem__options.before) === item.key;
        }));
        menuItems.current.push(item);
        menuItems.current.push(...oldItems.filter((oldItem)=>{
            var _oldItem__options;
            return (null === (_oldItem__options = oldItem._options) || void 0 === _oldItem__options ? void 0 : _oldItem__options.after) === item.key;
        }));
    }, [
        menuItems
    ]);
    if (!menuItems) {
        (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.error)('CurrentUser: You should use `CurrentUserSettingsMenuProvider` in the root of your app.');
        throw new Error('CurrentUser: You should use `CurrentUserSettingsMenuProvider` in the root of your app.');
    }
    return {
        getMenuItems,
        addMenuItem
    };
};
const CurrentUserSettingsMenuProvider = (param)=>{
    let { children } = param;
    const menuItems = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)([]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(CurrentUserSettingsMenuContext.Provider, {
        value: {
            menuItems
        },
        children: children
    });
};
export { CurrentUserSettingsMenuProvider, useCurrentUserSettingsMenu };
