"use strict";
var __webpack_require__ = {};
(()=>{
    __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__, {
    useCurrentUserSettingsMenu: ()=>useCurrentUserSettingsMenu,
    CurrentUserSettingsMenuProvider: ()=>CurrentUserSettingsMenuProvider
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const client_namespaceObject = require("@tachybase/utils/client");
const CurrentUserSettingsMenuContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
CurrentUserSettingsMenuContext.displayName = 'CurrentUserSettingsMenuContext';
const useCurrentUserSettingsMenu = ()=>{
    const { menuItems } = (0, external_react_namespaceObject.useContext)(CurrentUserSettingsMenuContext) || {};
    const getMenuItems = (0, external_react_namespaceObject.useCallback)(()=>menuItems.current, [
        menuItems
    ]);
    const addMenuItem = (0, external_react_namespaceObject.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, client_namespaceObject.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, external_react_namespaceObject.useRef)([]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CurrentUserSettingsMenuContext.Provider, {
        value: {
            menuItems
        },
        children: children
    });
};
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
});
