"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const uikitBaseComponentVue3 = require("@tencentcloud/uikit-base-component-vue3");
const chat = require("tuikit-atomicx-vue3/chat");
const room = require("tuikit-atomicx-vue3/room");
const IconButtonH5 = require("../base/IconButtonH5.vue2.js");
const PopUpArrowDown = require("../base/PopUpArrowDown.vue2.js");
const RoomChat = require("./RoomChat.vue2.js");
const _hoisted_1 = { class: "chat-content" };
const _hoisted_2 = { class: "chat-title" };
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  __name: "ChatButton",
  setup(__props) {
    const { t } = uikitBaseComponentVue3.useUIKit();
    const isPopupVisible = vue.ref(false);
    const { setActiveConversation } = chat.useConversationListState();
    const { currentRoom } = room.useRoomState();
    const { messageList } = chat.useMessageListState();
    const unreadCount = vue.ref(0);
    vue.watch(
      () => {
        var _a;
        return (_a = messageList.value) == null ? void 0 : _a.length;
      },
      (newLength, oldLength) => {
        if (!newLength || newLength === 0) {
          return;
        }
        if (!isPopupVisible.value && newLength > oldLength) {
          unreadCount.value += newLength - oldLength;
        }
      }
    );
    vue.watch(
      () => isPopupVisible.value,
      (isOpen) => {
        if (isOpen) {
          unreadCount.value = 0;
        }
      }
    );
    vue.watch(
      () => {
        var _a;
        return (_a = currentRoom.value) == null ? void 0 : _a.roomId;
      },
      (roomId) => {
        if (!roomId) {
          return;
        }
        setActiveConversation(`GROUP${roomId}`);
        unreadCount.value = 0;
      }
    );
    vue.onUnmounted(() => {
      setActiveConversation("");
    });
    const handleClick = () => {
      unreadCount.value = 0;
      isPopupVisible.value = true;
    };
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock(vue.Fragment, null, [
        vue.createVNode(IconButtonH5.default, {
          title: vue.unref(t)("Chat.Title"),
          onClick: handleClick
        }, {
          default: vue.withCtx(() => [
            vue.createVNode(vue.unref(uikitBaseComponentVue3.Badge), {
              value: unreadCount.value,
              hidden: !unreadCount.value
            }, {
              default: vue.withCtx(() => [
                vue.createVNode(vue.unref(uikitBaseComponentVue3.IconChat), { size: 24 })
              ]),
              _: 1
            }, 8, ["value", "hidden"])
          ]),
          _: 1
        }, 8, ["title"]),
        vue.createVNode(vue.unref(uikitBaseComponentVue3.TUIPopup), {
          visible: isPopupVisible.value,
          "onUpdate:visible": _cache[1] || (_cache[1] = ($event) => isPopupVisible.value = $event),
          height: "90%"
        }, {
          default: vue.withCtx(() => [
            vue.createElementVNode("div", _hoisted_1, [
              vue.createVNode(PopUpArrowDown.default, {
                onClick: _cache[0] || (_cache[0] = ($event) => isPopupVisible.value = false)
              }),
              vue.createElementVNode("div", _hoisted_2, vue.toDisplayString(vue.unref(t)("Chat.Title")), 1),
              isPopupVisible.value ? (vue.openBlock(), vue.createBlock(RoomChat.default, { key: 0 })) : vue.createCommentVNode("", true)
            ])
          ]),
          _: 1
        }, 8, ["visible"])
      ], 64);
    };
  }
});
exports.default = _sfc_main;
