"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
require("../../adapter-vue.js");
const TUIChatEngine = require("@tencentcloud/chat-uikit-engine");
const TUICore = require("@tencentcloud/tui-core");
const index = require("./chat-header/index.vue.js");
const index$1 = require("./message-list/index.vue.js");
const index$4 = require("./message-input/index.vue.js");
const index$2 = require("./mulitple-select-panel/index.vue.js");
const index_vue_vue_type_script_setup_true_lang = require("./forward/index.vue.js");
const index$3 = require("./message-input-toolbar/index.vue.js");
const env = require("../../utils/env.js");
const config = require("./config.js");
const _hoisted_1 = { class: "chat" };
const _sfc_main = /* @__PURE__ */ Vue.defineComponent({
  __name: "index",
  emits: ["closeChat"],
  setup(__props, { emit: __emit }) {
    const emits = __emit;
    const groupID = Vue.ref(void 0);
    const isGroup = Vue.ref(false);
    const isNotInGroup = Vue.ref(false);
    const notInGroupReason = Vue.ref();
    const currentConversationID = Vue.ref();
    const isMultipleSelectMode = Vue.ref(false);
    const inputToolbarDisplayType = Vue.ref("none");
    const messageInputRef = Vue.ref();
    const messageListRef = Vue.ref();
    const headerExtensionList = Vue.ref([]);
    const featureConfig = config.default.getFeatureConfig();
    Vue.onMounted(() => {
      TUIChatEngine.TUIStore.watch(TUIChatEngine.StoreName.CONV, {
        currentConversationID: onCurrentConversationIDUpdate,
        currentConversation: onCurrentConversationUpdate
      });
    });
    Vue.onUnmounted(() => {
      TUIChatEngine.TUIStore.unwatch(TUIChatEngine.StoreName.CONV, {
        currentConversationID: onCurrentConversationIDUpdate,
        currentConversation: onCurrentConversationUpdate
      });
      reset();
    });
    const isInputToolbarShow = Vue.computed(() => {
      return env.isUniFrameWork ? inputToolbarDisplayType.value !== "none" : true;
    });
    const leaveGroupReasonText = Vue.computed(() => {
      let text = "";
      switch (notInGroupReason.value) {
        case 4:
          text = TUIChatEngine.TUITranslateService.t("TUIChat.您已被管理员移出群聊");
          break;
        case 5:
          text = TUIChatEngine.TUITranslateService.t("TUIChat.该群聊已被解散");
          break;
        case 8:
          text = TUIChatEngine.TUITranslateService.t("TUIChat.您已退出该群聊");
          break;
        default:
          text = TUIChatEngine.TUITranslateService.t("TUIChat.您已退出该群聊");
          break;
      }
      return text;
    });
    const reset = () => {
      TUIChatEngine.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" && env.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;
      config.default.setChatType(conversationType);
      TUICore.notifyEvent(TUICore.TUIConstants.TUIChat.EVENT.CHAT_STATE_CHANGED, TUICore.TUIConstants.TUIChat.EVENT_SUB_KEY.CHAT_OPENED, { groupID: groupID.value });
      TUICore.callService({
        serviceName: TUICore.TUIConstants.TUICustomerServicePlugin.SERVICE.NAME,
        method: TUICore.TUIConstants.TUICustomerServicePlugin.SERVICE.METHOD.ACTIVE_CONVERSATION,
        params: { conversationID }
      });
      if (config.default.getChatType() === TUICore.TUIConstants.TUIChat.TYPE.GROUP) {
        headerExtensionList.value = TUICore.getExtensionList(TUICore.TUIConstants.TUIChat.EXTENSION.CHAT_HEADER.EXT_ID);
      }
    }
    return (_ctx, _cache) => {
      return Vue.openBlock(), Vue.createElementBlock("div", _hoisted_1, [
        Vue.createElementVNode("div", {
          class: Vue.normalizeClass(["tui-chat", !Vue.unref(env.isPC) && "tui-chat-h5"])
        }, [
          !Vue.unref(currentConversationID) ? (Vue.openBlock(), Vue.createElementBlock("div", {
            key: 0,
            class: Vue.normalizeClass(["tui-chat-default", !Vue.unref(env.isPC) && "tui-chat-h5-default"])
          }, [
            Vue.renderSlot(_ctx.$slots, "default", {}, void 0, true)
          ], 2)) : Vue.createCommentVNode("", true),
          Vue.unref(currentConversationID) ? (Vue.openBlock(), Vue.createElementBlock("div", {
            key: 1,
            class: Vue.normalizeClass(["tui-chat", !Vue.unref(env.isPC) && "tui-chat-h5"])
          }, [
            Vue.createVNode(index.default, {
              class: Vue.normalizeClass([
                "tui-chat-header",
                !Vue.unref(env.isPC) && "tui-chat-H5-header",
                Vue.unref(env.isUniFrameWork) && "tui-chat-uniapp-header"
              ]),
              headerExtensionList: Vue.unref(headerExtensionList),
              onCloseChat: closeChat
            }, null, 8, ["class", "headerExtensionList"]),
            Vue.createVNode(index_vue_vue_type_script_setup_true_lang.default, { onToggleMultipleSelectMode: toggleMultipleSelectMode }),
            Vue.createVNode(index$1.default, {
              ref_key: "messageListRef",
              ref: messageListRef,
              class: Vue.normalizeClass(["tui-chat-message-list", !Vue.unref(env.isPC) && "tui-chat-h5-message-list"]),
              isGroup: Vue.unref(isGroup),
              groupID: Vue.unref(groupID),
              isNotInGroup: Vue.unref(isNotInGroup),
              isMultipleSelectMode: Vue.unref(isMultipleSelectMode),
              onHandleEditor: handleEditor,
              onCloseInputToolBar: _cache[0] || (_cache[0] = () => changeToolbarDisplayType("none")),
              onToggleMultipleSelectMode: toggleMultipleSelectMode
            }, null, 8, ["class", "isGroup", "groupID", "isNotInGroup", "isMultipleSelectMode"]),
            Vue.unref(isNotInGroup) ? (Vue.openBlock(), Vue.createElementBlock("div", {
              key: 0,
              class: Vue.normalizeClass({
                "tui-chat-leave-group": true,
                "tui-chat-leave-group-mobile": Vue.unref(env.isMobile)
              })
            }, Vue.toDisplayString(Vue.unref(leaveGroupReasonText)), 3)) : Vue.unref(isMultipleSelectMode) ? (Vue.openBlock(), Vue.createBlock(index$2.default, {
              key: 1,
              onOneByOneForwardMessage: oneByOneForwardMessage,
              onMergeForwardMessage: mergeForwardMessage,
              onToggleMultipleSelectMode: toggleMultipleSelectMode
            })) : (Vue.openBlock(), Vue.createElementBlock(Vue.Fragment, { key: 2 }, [
              Vue.unref(isInputToolbarShow) ? (Vue.openBlock(), Vue.createBlock(index$3.default, {
                key: 0,
                class: Vue.normalizeClass([
                  "tui-chat-message-input-toolbar",
                  !Vue.unref(env.isPC) && "tui-chat-h5-message-input-toolbar",
                  Vue.unref(env.isUniFrameWork) && "tui-chat-uni-message-input-toolbar"
                ]),
                displayType: Vue.unref(inputToolbarDisplayType),
                onInsertEmoji: insertEmoji,
                onChangeToolbarDisplayType: changeToolbarDisplayType,
                onScrollToLatestMessage: scrollToLatestMessage
              }, null, 8, ["class", "displayType"])) : Vue.createCommentVNode("", true),
              Vue.createVNode(index$4.default, {
                ref_key: "messageInputRef",
                ref: messageInputRef,
                class: Vue.normalizeClass([
                  "tui-chat-message-input",
                  !Vue.unref(env.isPC) && "tui-chat-h5-message-input",
                  Vue.unref(env.isUniFrameWork) && "tui-chat-uni-message-input",
                  Vue.unref(env.isWeChat) && "tui-chat-wx-message-input"
                ]),
                enableAt: Vue.unref(featureConfig).InputMention,
                isMuted: false,
                muteText: Vue.unref(TUIChatEngine.TUITranslateService).t("TUIChat.您已被管理员禁言"),
                placeholder: Vue.unref(TUIChatEngine.TUITranslateService).t("TUIChat.请输入消息"),
                inputToolbarDisplayType: Vue.unref(inputToolbarDisplayType),
                onChangeToolbarDisplayType: changeToolbarDisplayType
              }, null, 8, ["class", "enableAt", "muteText", "placeholder", "inputToolbarDisplayType"])
            ], 64))
          ], 2)) : Vue.createCommentVNode("", true),
          !Vue.unref(isNotInGroup) && Vue.unref(env.isUniFrameWork) && Vue.unref(isGroup) && Vue.unref(headerExtensionList).length > 0 ? (Vue.openBlock(), Vue.createElementBlock("div", {
            key: 2,
            class: "group-profile",
            onClick: handleGroup
          }, Vue.toDisplayString(Vue.unref(headerExtensionList)[0].text), 1)) : Vue.createCommentVNode("", true)
        ], 2)
      ]);
    };
  }
});
exports.default = _sfc_main;
