"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const TUIRoomEngine = require("@tencentcloud/tuiroom-engine-js");
const useRoomEngine = require("../../hooks/useRoomEngine.js");
const chat = require("../../stores/chat.js");
const pinia = require("pinia");
const basic = require("../../stores/basic.js");
const index = require("../../locales/index.js");
const Vue = require("vue");
const room = require("../../stores/room.js");
const environment = require("../../utils/environment.js");
function useSideBar() {
  var _a;
  const roomEngine = useRoomEngine.default();
  const { t } = index.useI18n();
  const chatStore = chat.useChatStore();
  const basicStore = basic.useBasicStore();
  const { sdkAppId, isSidebarOpen, sidebarName, roomId } = pinia.storeToRefs(basicStore);
  const roomStore = room.useRoomStore();
  const { userNumber } = pinia.storeToRefs(roomStore);
  const showSideBar = Vue.computed(
    () => isSidebarOpen.value && sidebarName.value !== "transfer-leave"
  );
  const title = Vue.computed(() => {
    let sidebarTitle = "";
    switch (sidebarName.value) {
      case "chat":
        sidebarTitle = t("Chat");
        break;
      case "invite":
        sidebarTitle = t("Invite");
        break;
      case "more":
        sidebarTitle = t("More");
        break;
      case "apply":
        sidebarTitle = t("Members applying on stage");
        break;
      case "manage-member":
        sidebarTitle = `${t("Members")} (${userNumber.value})`;
        break;
    }
    return sidebarTitle;
  });
  function handleClose(done) {
    basicStore.setSidebarOpenStatus(false);
    basicStore.setSidebarName("");
    done();
  }
  const onReceiveMessage = (options) => {
    let messageTypeList = [TUIRoomEngine.TencentCloudChat.TYPES.MSG_TEXT];
    if (!environment.isMobile) {
      messageTypeList = messageTypeList.concat([
        TUIRoomEngine.TencentCloudChat.TYPES.MSG_IMAGE,
        TUIRoomEngine.TencentCloudChat.TYPES.MSG_FILE,
        TUIRoomEngine.TencentCloudChat.TYPES.MSG_FACE,
        TUIRoomEngine.TencentCloudChat.TYPES.MSG_VIDEO
      ]);
    }
    if (!options || !options.data) {
      return;
    }
    const currentConversationId = `GROUP${roomId.value}`;
    const isChatSidebarOpen = basicStore.isSidebarOpen && basicStore.sidebarName === "chat";
    options.data.forEach((message) => {
      if (message.conversationID !== currentConversationId) return;
      const shouldUpdateUnreadCount = messageTypeList.includes(message.type) && !isChatSidebarOpen;
      if (shouldUpdateUnreadCount) {
        chatStore.updateUnReadCount(++chatStore.unReadCount);
      }
    });
  };
  let tim = (_a = roomEngine.instance) == null ? void 0 : _a.getTIM();
  tim == null ? void 0 : tim.on(TUIRoomEngine.TencentCloudChat.EVENT.MESSAGE_RECEIVED, onReceiveMessage);
  Vue.watch(sdkAppId, () => {
    if (!tim && sdkAppId.value) {
      tim = TUIRoomEngine.TencentCloudChat.create({ SDKAppID: basicStore.sdkAppId });
      tim == null ? void 0 : tim.on(TUIRoomEngine.TencentCloudChat.EVENT.MESSAGE_RECEIVED, onReceiveMessage);
    }
  });
  Vue.onUnmounted(() => {
    tim == null ? void 0 : tim.off(TUIRoomEngine.TencentCloudChat.EVENT.MESSAGE_RECEIVED, onReceiveMessage);
  });
  return {
    t,
    isSidebarOpen,
    title,
    sidebarName,
    handleClose,
    showSideBar
  };
}
exports.default = useSideBar;
