import { defineComponent, ref, createElementBlock, openBlock, Fragment, createElementVNode, createVNode, unref, toDisplayString, withCtx } from "vue";
import { useUIKit, IconCaretDownSmall, TUIPopup, TUIToast } from "@tencentcloud/uikit-base-component-vue3";
import { useLoginState, Avatar } from "tuikit-atomicx-vue3/room";
import PopUpArrowDown from "../base/PopUpArrowDown.vue2.mjs";
const _hoisted_1 = { class: "login-user-info" };
const _hoisted_2 = { class: "user-id" };
const _hoisted_3 = {
  key: 1,
  class: "login-user-info-trigger"
};
const _hoisted_4 = { class: "user-id" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    showLogout: { type: Boolean, default: true }
  },
  emits: ["logout"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const { loginUserInfo, logout } = useLoginState();
    const { t } = useUIKit();
    const isLogoutPopupVisible = ref(false);
    const emits = __emit;
    const showLogoutPopup = () => {
      isLogoutPopupVisible.value = true;
    };
    const handleLogout = async () => {
      try {
        await logout();
        localStorage.removeItem("tuiRoom-userInfo");
        TUIToast.success({ message: t("LoginUserInfo.LogoutSuccess") });
        emits("logout");
      } catch (_error) {
        TUIToast.error({ message: t("LoginUserInfo.LogoutFailed") });
      }
    };
    return (_ctx, _cache) => {
      var _a, _b, _c, _d, _e, _f;
      return openBlock(), createElementBlock(Fragment, null, [
        createElementVNode("div", _hoisted_1, [
          props.showLogout ? (openBlock(), createElementBlock("div", {
            key: 0,
            class: "login-user-info-trigger",
            onClick: showLogoutPopup
          }, [
            createVNode(unref(Avatar), {
              src: (_a = unref(loginUserInfo)) == null ? void 0 : _a.avatarUrl,
              size: 28
            }, null, 8, ["src"]),
            createElementVNode("span", _hoisted_2, toDisplayString(((_b = unref(loginUserInfo)) == null ? void 0 : _b.userName) || ((_c = unref(loginUserInfo)) == null ? void 0 : _c.userId)), 1),
            createVNode(unref(IconCaretDownSmall), { size: 24 })
          ])) : (openBlock(), createElementBlock("div", _hoisted_3, [
            createVNode(unref(Avatar), {
              src: (_d = unref(loginUserInfo)) == null ? void 0 : _d.avatarUrl,
              size: 28
            }, null, 8, ["src"]),
            createElementVNode("span", _hoisted_4, toDisplayString(((_e = unref(loginUserInfo)) == null ? void 0 : _e.userName) || ((_f = unref(loginUserInfo)) == null ? void 0 : _f.userId)), 1)
          ]))
        ]),
        createVNode(unref(TUIPopup), {
          modelValue: isLogoutPopupVisible.value,
          "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => isLogoutPopupVisible.value = $event)
        }, {
          default: withCtx(() => [
            createVNode(PopUpArrowDown, {
              onClick: _cache[0] || (_cache[0] = ($event) => isLogoutPopupVisible.value = false)
            }),
            createElementVNode("div", {
              class: "logout-popup",
              onClick: handleLogout
            }, toDisplayString(unref(t)("LoginUserInfo.Logout")), 1)
          ]),
          _: 1
        }, 8, ["modelValue"])
      ], 64);
    };
  }
});
export {
  _sfc_main as default
};
