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";
import * as __WEBPACK_EXTERNAL_MODULE__ant_design_icons__ from "@ant-design/icons";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_antd_style__ from "antd-style";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE_react_router_dom__ from "react-router-dom";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__ChangePassword_mjs__ from "./ChangePassword.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__CurrentUserSettingsMenuProvider_mjs__ from "./CurrentUserSettingsMenuProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__EditProfile_mjs__ from "./EditProfile.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__LanguageSettings_mjs__ from "./LanguageSettings.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__SwitchRole_mjs__ from "./SwitchRole.mjs";
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, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useCurrentUserContext)();
    const { token } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useToken)();
    return (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        var _data_data, _data_data1, _data_data2;
        return {
            key: 'nickname',
            disabled: true,
            label: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.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, __WEBPACK_EXTERNAL_MODULE__CurrentUserSettingsMenuProvider_mjs__.useCurrentUserSettingsMenu)();
    const { redirectUrl = '' } = props;
    const { allowAll, snippets } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useACLRoleContext)();
    const appAllowed = allowAll || (null == snippets ? void 0 : snippets.includes('app'));
    const navigate = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useNavigate)();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useAPIClient)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const silenceApi = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useAPIClient)();
    const check = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.error)(err);
                });
            }, 3000);
        }), [
        silenceApi
    ]);
    const nickname = useNickname();
    const editProfile = (0, __WEBPACK_EXTERNAL_MODULE__EditProfile_mjs__.useEditProfile)();
    const changePassword = (0, __WEBPACK_EXTERNAL_MODULE__ChangePassword_mjs__.useChangePassword)();
    const switchRole = (0, __WEBPACK_EXTERNAL_MODULE__SwitchRole_mjs__.useSwitchRole)();
    const languageSettings = (0, __WEBPACK_EXTERNAL_MODULE__LanguageSettings_mjs__.useLanguageSettings)();
    const { modal } = __WEBPACK_EXTERNAL_MODULE_antd__.App.useApp();
    const controlApp = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Menu, {
        items: getMenuItems()
    });
};
const DropdownVisibleContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
DropdownVisibleContext.displayName = 'DropdownVisibleContext';
const useStyles = (0, __WEBPACK_EXTERNAL_MODULE_antd_style__.createStyles)((param)=>{
    let { css, token } = param;
    return {
        dropdown: css(_templateObject()),
        button: css(_templateObject1(), token.colorTextHeaderMenu)
    };
});
const CurrentUser = ()=>{
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const { styles } = useStyles();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        className: styles.dropdown,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(DropdownVisibleContext.Provider, {
            value: {
                visible,
                setVisible
            },
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Dropdown, {
                open: visible,
                onOpenChange: (visible)=>{
                    setVisible(visible);
                },
                dropdownRender: ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SettingsMenu, {}),
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                    "data-testid": "user-center-button",
                    className: styles.button,
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.UserOutlined, {})
                })
            })
        })
    });
};
export { CurrentUser, DropdownVisibleContext, SettingsMenu };
