import { defineComponent, ref, onMounted, onUnmounted, computed, openBlock, createElementBlock, createElementVNode, normalizeClass, unref, renderSlot, createCommentVNode, createVNode, toDisplayString, createBlock, Fragment } from "vue";
import "../../adapter-vue.mjs";
import TUIChatEngine, { TUIStore, StoreName, TUITranslateService, TUIConversationService } from "@tencentcloud/chat-uikit-engine";
import TUICore, { TUIConstants } from "@tencentcloud/tui-core";
import ChatHeader from "./chat-header/index.vue.mjs";
import MessageList from "./message-list/index.vue.mjs";
import MessageInput from "./message-input/index.vue.mjs";
import MultipleSelectPanel from "./mulitple-select-panel/index.vue.mjs";
import _sfc_main$1 from "./forward/index.vue.mjs";
import MessageInputToolbar from "./message-input-toolbar/index.vue.mjs";
import { isUniFrameWork, isPC, isMobile, isWeChat } from "../../utils/env.mjs";
import ChatConfig from "./config.mjs";
const _hoisted_1 = { class: "chat" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  emits: ["closeChat"],
  setup(__props, { emit: __emit }) {
    const emits = __emit;
    const groupID = ref(void 0);
    const isGroup = ref(false);
    const isNotInGroup = ref(false);
    const notInGroupReason = ref();
    const currentConversationID = ref();
    const isMultipleSelectMode = ref(false);
    const inputToolbarDisplayType = ref("none");
    const messageInputRef = ref();
    const messageListRef = ref();
    const headerExtensionList = ref([]);
    const featureConfig = ChatConfig.getFeatureConfig();
    onMounted(() => {
      TUIStore.watch(StoreName.CONV, {
        currentConversationID: onCurrentConversationIDUpdate,
        currentConversation: onCurrentConversationUpdate
      });
    });
    onUnmounted(() => {
      TUIStore.unwatch(StoreName.CONV, {
        currentConversationID: onCurrentConversationIDUpdate,
        currentConversation: onCurrentConversationUpdate
      });
      reset();
    });
    const isInputToolbarShow = computed(() => {
      return isUniFrameWork ? inputToolbarDisplayType.value !== "none" : true;
    });
    const leaveGroupReasonText = computed(() => {
      let text = "";
      switch (notInGroupReason.value) {
        case 4:
          text = TUITranslateService.t("TUIChat.您已被管理员移出群聊");
          break;
        case 5:
          text = TUITranslateService.t("TUIChat.该群聊已被解散");
          break;
        case 8:
          text = TUITranslateService.t("TUIChat.您已退出该群聊");
          break;
        default:
          text = TUITranslateService.t("TUIChat.您已退出该群聊");
          break;
      }
      return text;
    });
    const reset = () => {
      TUIConversationService.switchConversation("");
    };
    const closeChat = (conversationID) => {
      emits("closeChat", conversationID);
      reset();
    };
    const insertEmoji = (emojiObj) => {
      var _a;
      (_a = messageInputRef.value) == null ? void 0 : _a.insertEmoji(emojiObj);
    };
    const handleEditor = (message, type) => {
      var _a, _b, _c;
      if (!message || !type) return;
      switch (type) {
        case "reference":
          break;
        case "reply":
          break;
        case "reedit":
          if ((_a = message == null ? void 0 : message.payload) == null ? void 0 : _a.text) {
            (_c = messageInputRef == null ? void 0 : messageInputRef.value) == null ? void 0 : _c.reEdit((_b = message == null ? void 0 : message.payload) == null ? void 0 : _b.text);
          }
          break;
      }
    };
    const handleGroup = () => {
      headerExtensionList.value[0].listener.onClicked({ groupID: groupID.value });
    };
    function changeToolbarDisplayType(type) {
      inputToolbarDisplayType.value = inputToolbarDisplayType.value === type ? "none" : type;
      if (inputToolbarDisplayType.value !== "none" && isUniFrameWork) {
        uni.$emit("scroll-to-bottom");
      }
    }
    function scrollToLatestMessage() {
      var _a;
      (_a = messageListRef.value) == null ? void 0 : _a.scrollToLatestMessage();
    }
    function toggleMultipleSelectMode(visible) {
      isMultipleSelectMode.value = visible === void 0 ? !isMultipleSelectMode.value : visible;
    }
    function mergeForwardMessage() {
      var _a;
      (_a = messageListRef.value) == null ? void 0 : _a.mergeForwardMessage();
    }
    function oneByOneForwardMessage() {
      var _a;
      (_a = messageListRef.value) == null ? void 0 : _a.oneByOneForwardMessage();
    }
    function onCurrentConversationUpdate(conversation) {
      if ((conversation == null ? void 0 : conversation.operationType) > 0) {
        headerExtensionList.value = [];
        isNotInGroup.value = true;
        notInGroupReason.value = conversation == null ? void 0 : conversation.operationType;
      } else {
        isNotInGroup.value = false;
        notInGroupReason.value = void 0;
      }
    }
    function onCurrentConversationIDUpdate(conversationID) {
      if (currentConversationID.value === conversationID) {
        return;
      }
      isGroup.value = false;
      let conversationType = TUIChatEngine.TYPES.CONV_C2C;
      if (conversationID.startsWith(TUIChatEngine.TYPES.CONV_GROUP)) {
        conversationType = TUIChatEngine.TYPES.CONV_GROUP;
        isGroup.value = true;
        groupID.value = conversationID.replace(TUIChatEngine.TYPES.CONV_GROUP, "");
      }
      headerExtensionList.value = [];
      currentConversationID.value = conversationID;
      isMultipleSelectMode.value = false;
      ChatConfig.setChatType(conversationType);
      TUICore.notifyEvent(TUIConstants.TUIChat.EVENT.CHAT_STATE_CHANGED, TUIConstants.TUIChat.EVENT_SUB_KEY.CHAT_OPENED, { groupID: groupID.value });
      TUICore.callService({
        serviceName: TUIConstants.TUICustomerServicePlugin.SERVICE.NAME,
        method: TUIConstants.TUICustomerServicePlugin.SERVICE.METHOD.ACTIVE_CONVERSATION,
        params: { conversationID }
      });
      if (ChatConfig.getChatType() === TUIConstants.TUIChat.TYPE.GROUP) {
        headerExtensionList.value = TUICore.getExtensionList(TUIConstants.TUIChat.EXTENSION.CHAT_HEADER.EXT_ID);
      }
    }
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", _hoisted_1, [
        createElementVNode("div", {
          class: normalizeClass(["tui-chat", !unref(isPC) && "tui-chat-h5"])
        }, [
          !unref(currentConversationID) ? (openBlock(), createElementBlock("div", {
            key: 0,
            class: normalizeClass(["tui-chat-default", !unref(isPC) && "tui-chat-h5-default"])
          }, [
            renderSlot(_ctx.$slots, "default", {}, void 0, true)
          ], 2)) : createCommentVNode("", true),
          unref(currentConversationID) ? (openBlock(), createElementBlock("div", {
            key: 1,
            class: normalizeClass(["tui-chat", !unref(isPC) && "tui-chat-h5"])
          }, [
            createVNode(ChatHeader, {
              class: normalizeClass([
                "tui-chat-header",
                !unref(isPC) && "tui-chat-H5-header",
                unref(isUniFrameWork) && "tui-chat-uniapp-header"
              ]),
              headerExtensionList: unref(headerExtensionList),
              onCloseChat: closeChat
            }, null, 8, ["class", "headerExtensionList"]),
            createVNode(_sfc_main$1, { onToggleMultipleSelectMode: toggleMultipleSelectMode }),
            createVNode(MessageList, {
              ref_key: "messageListRef",
              ref: messageListRef,
              class: normalizeClass(["tui-chat-message-list", !unref(isPC) && "tui-chat-h5-message-list"]),
              isGroup: unref(isGroup),
              groupID: unref(groupID),
              isNotInGroup: unref(isNotInGroup),
              isMultipleSelectMode: unref(isMultipleSelectMode),
              onHandleEditor: handleEditor,
              onCloseInputToolBar: _cache[0] || (_cache[0] = () => changeToolbarDisplayType("none")),
              onToggleMultipleSelectMode: toggleMultipleSelectMode
            }, null, 8, ["class", "isGroup", "groupID", "isNotInGroup", "isMultipleSelectMode"]),
            unref(isNotInGroup) ? (openBlock(), createElementBlock("div", {
              key: 0,
              class: normalizeClass({
                "tui-chat-leave-group": true,
                "tui-chat-leave-group-mobile": unref(isMobile)
              })
            }, toDisplayString(unref(leaveGroupReasonText)), 3)) : unref(isMultipleSelectMode) ? (openBlock(), createBlock(MultipleSelectPanel, {
              key: 1,
              onOneByOneForwardMessage: oneByOneForwardMessage,
              onMergeForwardMessage: mergeForwardMessage,
              onToggleMultipleSelectMode: toggleMultipleSelectMode
            })) : (openBlock(), createElementBlock(Fragment, { key: 2 }, [
              unref(isInputToolbarShow) ? (openBlock(), createBlock(MessageInputToolbar, {
                key: 0,
                class: normalizeClass([
                  "tui-chat-message-input-toolbar",
                  !unref(isPC) && "tui-chat-h5-message-input-toolbar",
                  unref(isUniFrameWork) && "tui-chat-uni-message-input-toolbar"
                ]),
                displayType: unref(inputToolbarDisplayType),
                onInsertEmoji: insertEmoji,
                onChangeToolbarDisplayType: changeToolbarDisplayType,
                onScrollToLatestMessage: scrollToLatestMessage
              }, null, 8, ["class", "displayType"])) : createCommentVNode("", true),
              createVNode(MessageInput, {
                ref_key: "messageInputRef",
                ref: messageInputRef,
                class: normalizeClass([
                  "tui-chat-message-input",
                  !unref(isPC) && "tui-chat-h5-message-input",
                  unref(isUniFrameWork) && "tui-chat-uni-message-input",
                  unref(isWeChat) && "tui-chat-wx-message-input"
                ]),
                enableAt: unref(featureConfig).InputMention,
                isMuted: false,
                muteText: unref(TUITranslateService).t("TUIChat.您已被管理员禁言"),
                placeholder: unref(TUITranslateService).t("TUIChat.请输入消息"),
                inputToolbarDisplayType: unref(inputToolbarDisplayType),
                onChangeToolbarDisplayType: changeToolbarDisplayType
              }, null, 8, ["class", "enableAt", "muteText", "placeholder", "inputToolbarDisplayType"])
            ], 64))
          ], 2)) : createCommentVNode("", true),
          !unref(isNotInGroup) && unref(isUniFrameWork) && unref(isGroup) && unref(headerExtensionList).length > 0 ? (openBlock(), createElementBlock("div", {
            key: 2,
            class: "group-profile",
            onClick: handleGroup
          }, toDisplayString(unref(headerExtensionList)[0].text), 1)) : createCommentVNode("", true)
        ], 2)
      ]);
    };
  }
});
export {
  _sfc_main as default
};
