import { TencentCloudChat } from "@tencentcloud/tuiroom-engine-js";
import useGetRoomEngine from "../../hooks/useRoomEngine.mjs";
import { useChatStore } from "../../stores/chat.mjs";
import { storeToRefs } from "pinia";
import { useBasicStore } from "../../stores/basic.mjs";
import { useI18n } from "../../locales/index.mjs";
import { computed, watch, onUnmounted } from "vue";
import { useRoomStore } from "../../stores/room.mjs";
import { isMobile } from "../../utils/environment.mjs";
function useSideBar() {
  var _a;
  const roomEngine = useGetRoomEngine();
  const { t } = useI18n();
  const chatStore = useChatStore();
  const basicStore = useBasicStore();
  const { sdkAppId, isSidebarOpen, sidebarName, roomId } = storeToRefs(basicStore);
  const roomStore = useRoomStore();
  const { userNumber } = storeToRefs(roomStore);
  const showSideBar = computed(
    () => isSidebarOpen.value && sidebarName.value !== "transfer-leave"
  );
  const title = 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 = [TencentCloudChat.TYPES.MSG_TEXT];
    if (!isMobile) {
      messageTypeList = messageTypeList.concat([
        TencentCloudChat.TYPES.MSG_IMAGE,
        TencentCloudChat.TYPES.MSG_FILE,
        TencentCloudChat.TYPES.MSG_FACE,
        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(TencentCloudChat.EVENT.MESSAGE_RECEIVED, onReceiveMessage);
  watch(sdkAppId, () => {
    if (!tim && sdkAppId.value) {
      tim = TencentCloudChat.create({ SDKAppID: basicStore.sdkAppId });
      tim == null ? void 0 : tim.on(TencentCloudChat.EVENT.MESSAGE_RECEIVED, onReceiveMessage);
    }
  });
  onUnmounted(() => {
    tim == null ? void 0 : tim.off(TencentCloudChat.EVENT.MESSAGE_RECEIVED, onReceiveMessage);
  });
  return {
    t,
    isSidebarOpen,
    title,
    sidebarName,
    handleClose,
    showSideBar
  };
}
export {
  useSideBar as default
};
