"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__, {
    SettingsMenu: ()=>SettingsMenu,
    CurrentUser: ()=>CurrentUser,
    DropdownVisibleContext: ()=>DropdownVisibleContext
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const client_namespaceObject = require("@tachybase/utils/client");
const icons_namespaceObject = require("@ant-design/icons");
const external_antd_namespaceObject = require("antd");
const external_antd_style_namespaceObject = require("antd-style");
const external_react_i18next_namespaceObject = require("react-i18next");
const external_react_router_dom_namespaceObject = require("react-router-dom");
const external_index_js_namespaceObject = require("../index.js");
const external_ChangePassword_js_namespaceObject = require("./ChangePassword.js");
const external_CurrentUserSettingsMenuProvider_js_namespaceObject = require("./CurrentUserSettingsMenuProvider.js");
const external_EditProfile_js_namespaceObject = require("./EditProfile.js");
const external_LanguageSettings_js_namespaceObject = require("./LanguageSettings.js");
const external_SwitchRole_js_namespaceObject = require("./SwitchRole.js");
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n      display: inline-block;\n      vertical-align: top;\n      width: 46px;\n      height: var(--tb-header-height);\n      &:hover {\n        background: rgba(255, 255, 255, 0.1) !important;\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
function _templateObject1() {
    const data = _tagged_template_literal([
        "\n      overflow: hidden;\n      display: inline-block;\n      line-height: var(--tb-header-height);\n      white-space: nowrap;\n      text-overflow: ellipsis;\n      cursor: pointer;\n      padding: 0 16px 0 16px;\n      color: ",
        ";\n    "
    ]);
    _templateObject1 = function() {
        return data;
    };
    return data;
}
const useNickname = ()=>{
    var _data_data, _data_data1, _data_data2;
    const { data } = (0, external_index_js_namespaceObject.useCurrentUserContext)();
    const { token } = (0, external_index_js_namespaceObject.useToken)();
    return (0, external_react_namespaceObject.useMemo)(()=>{
        var _data_data, _data_data1, _data_data2;
        return {
            key: 'nickname',
            disabled: true,
            label: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                "aria-disabled": "false",
                style: {
                    cursor: 'text',
                    color: token.colorTextDescription
                },
                children: (null == data ? void 0 : null === (_data_data = data.data) || void 0 === _data_data ? void 0 : _data_data.nickname) || (null == data ? void 0 : null === (_data_data1 = data.data) || void 0 === _data_data1 ? void 0 : _data_data1.username) || (null == data ? void 0 : null === (_data_data2 = data.data) || void 0 === _data_data2 ? void 0 : _data_data2.email)
            })
        };
    }, [
        null == data ? void 0 : null === (_data_data = data.data) || void 0 === _data_data ? void 0 : _data_data.email,
        null == data ? void 0 : null === (_data_data1 = data.data) || void 0 === _data_data1 ? void 0 : _data_data1.nickname,
        null == data ? void 0 : null === (_data_data2 = data.data) || void 0 === _data_data2 ? void 0 : _data_data2.username,
        null == data ? void 0 : data.data.version,
        token.colorTextDescription
    ]);
};
const SettingsMenu = (props)=>{
    const { addMenuItem, getMenuItems } = (0, external_CurrentUserSettingsMenuProvider_js_namespaceObject.useCurrentUserSettingsMenu)();
    const { redirectUrl = '' } = props;
    const { allowAll, snippets } = (0, external_index_js_namespaceObject.useACLRoleContext)();
    const appAllowed = allowAll || (null == snippets ? void 0 : snippets.includes('app'));
    const navigate = (0, external_react_router_dom_namespaceObject.useNavigate)();
    const api = (0, external_index_js_namespaceObject.useAPIClient)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const silenceApi = (0, external_index_js_namespaceObject.useAPIClient)();
    const check = (0, external_react_namespaceObject.useCallback)(async ()=>await new Promise((resolve)=>{
            const heartbeat = setInterval(()=>{
                silenceApi.silent().resource('app').getInfo().then((res)=>{
                    if ((null == res ? void 0 : res.status) === 200) {
                        resolve('ok');
                        clearInterval(heartbeat);
                    }
                    return res;
                }).catch((err)=>{
                    (0, client_namespaceObject.error)(err);
                });
            }, 3000);
        }), [
        silenceApi
    ]);
    const nickname = useNickname();
    const editProfile = (0, external_EditProfile_js_namespaceObject.useEditProfile)();
    const changePassword = (0, external_ChangePassword_js_namespaceObject.useChangePassword)();
    const switchRole = (0, external_SwitchRole_js_namespaceObject.useSwitchRole)();
    const languageSettings = (0, external_LanguageSettings_js_namespaceObject.useLanguageSettings)();
    const { modal } = external_antd_namespaceObject.App.useApp();
    const controlApp = (0, external_react_namespaceObject.useMemo)(()=>{
        if (!appAllowed) return [];
        return [
            {
                key: 'cache',
                label: t('Clear cache'),
                onClick: async ()=>{
                    await api.resource('app').clearCache();
                    window.location.reload();
                }
            },
            {
                key: 'reboot',
                label: t('Restart application'),
                onClick: async ()=>{
                    modal.confirm({
                        title: t('Restart application'),
                        okText: t('Restart'),
                        okButtonProps: {
                            danger: true
                        },
                        onOk: async ()=>{
                            await api.resource('app').restart();
                        }
                    });
                }
            },
            {
                key: 'divider_4',
                type: 'divider'
            }
        ];
    }, [
        api,
        appAllowed,
        check,
        modal,
        t
    ]);
    (0, external_react_namespaceObject.useMemo)(()=>{
        const items = [
            nickname,
            {
                key: 'divider_1',
                type: 'divider'
            },
            editProfile,
            changePassword,
            {
                key: 'divider_2',
                type: 'divider'
            },
            switchRole,
            {
                key: 'divider_3',
                type: 'divider'
            },
            ...controlApp,
            {
                key: 'signout',
                label: t('Sign out'),
                onClick: async ()=>{
                    await api.auth.signOut();
                    navigate("/signin?redirect=".concat(encodeURIComponent(redirectUrl)));
                }
            }
        ];
        items.forEach((item)=>{
            if (item) addMenuItem(item);
        });
        if (languageSettings) addMenuItem(languageSettings, {
            before: 'divider_3'
        });
    }, [
        addMenuItem,
        api.auth,
        controlApp,
        editProfile,
        changePassword,
        languageSettings,
        navigate,
        redirectUrl,
        switchRole,
        t,
        nickname
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Menu, {
        items: getMenuItems()
    });
};
const DropdownVisibleContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
DropdownVisibleContext.displayName = 'DropdownVisibleContext';
const useStyles = (0, external_antd_style_namespaceObject.createStyles)((param)=>{
    let { css, token } = param;
    return {
        dropdown: css(_templateObject()),
        button: css(_templateObject1(), token.colorTextHeaderMenu)
    };
});
const CurrentUser = ()=>{
    const [visible, setVisible] = (0, external_react_namespaceObject.useState)(false);
    const { styles } = useStyles();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: styles.dropdown,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(DropdownVisibleContext.Provider, {
            value: {
                visible,
                setVisible
            },
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Dropdown, {
                open: visible,
                onOpenChange: (visible)=>{
                    setVisible(visible);
                },
                dropdownRender: ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(SettingsMenu, {}),
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                    "data-testid": "user-center-button",
                    className: styles.button,
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.UserOutlined, {})
                })
            })
        })
    });
};
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
});
