import { defineComponent, ref, onMounted, onUnmounted, watch, computed, openBlock, createElementBlock, normalizeClass, unref, createElementVNode, createBlock, createCommentVNode, Fragment, renderList, createVNode } from "vue";
import "../../../adapter-vue.mjs";
import TUIChatEngine, { TUIStore, StoreName } from "@tencentcloud/chat-uikit-engine";
import TUICore, { TUIConstants } from "@tencentcloud/tui-core";
import EmojiPicker from "./emoji-picker/index.vue.mjs";
import ImageUpload from "./image-upload/index.vue.mjs";
import FileUpload from "./file-upload/index.vue.mjs";
import VideoUpload from "./video-upload/index.vue.mjs";
import Evaluate from "./evaluate/index.vue.mjs";
import Words from "./words/index.vue.mjs";
import ToolbarItemContainer from "./toolbar-item-container/index.vue.mjs";
import _sfc_main$1 from "./user-selector/index.vue.mjs";
import { isPC, isUniFrameWork, isH5 } from "../../../utils/env.mjs";
import ChatConfig from "../config.mjs";
import { enableSampleTaskStatus } from "../../../utils/enableSampleTaskStatus.mjs";
const _hoisted_1 = {
  key: 0,
  class: /* @__PURE__ */ normalizeClass(["message-input-toolbar-list-end"])
};
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    displayType: { default: "none" }
  },
  emits: ["scrollToLatestMessage", "changeToolbarDisplayType", "insertEmoji"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const h5Dialog = ref();
    const currentConversation = ref();
    const isGroup = ref(false);
    const selectorShowType = ref("");
    const userSelectorRef = ref();
    const emojiPickerRef = ref();
    const currentUserSelectorExtension = ref();
    const currentExtensionList = ref([]);
    const featureConfig = ChatConfig.getFeatureConfig();
    const isRenderedEmojiPicker = ref(true);
    isRenderedEmojiPicker.value = featureConfig.InputEmoji || featureConfig.InputStickers;
    onMounted(() => {
      TUIStore.watch(StoreName.CONV, {
        currentConversation: onCurrentConversationUpdate
      });
    });
    onUnmounted(() => {
      TUIStore.unwatch(StoreName.CONV, {
        currentConversation: onCurrentConversationUpdate
      });
    });
    watch(() => props.displayType, (newValue) => {
      var _a;
      if (newValue === "none") {
        (_a = emojiPickerRef.value) == null ? void 0 : _a.closeEmojiPicker();
      } else {
        emits("scrollToLatestMessage");
      }
    });
    const onCurrentConversationUpdate = (conversation) => {
      var _a, _b;
      if ((conversation == null ? void 0 : conversation.conversationID) && conversation.conversationID !== ((_a = currentConversation.value) == null ? void 0 : _a.conversationID)) {
        getExtensionList(conversation.conversationID);
      }
      currentConversation.value = conversation;
      isGroup.value = ((_b = currentConversation == null ? void 0 : currentConversation.value) == null ? void 0 : _b.type) === TUIChatEngine.TYPES.CONV_GROUP;
    };
    const getExtensionList = (conversationID) => {
      if (!conversationID) {
        return;
      }
      const chatType = ChatConfig.getChatType();
      const params = { chatType };
      if (chatType === TUIConstants.TUIChat.TYPE.CUSTOMER_SERVICE) {
        params.filterVoice = true;
        params.filterVideo = true;
        enableSampleTaskStatus("customerService");
      }
      currentExtensionList.value = [
        ...TUICore.getExtensionList(TUIConstants.TUIChat.EXTENSION.INPUT_MORE.EXT_ID, params)
      ].filter((extension) => {
        var _a;
        if (((_a = extension == null ? void 0 : extension.data) == null ? void 0 : _a.name) === "search") {
          return featureConfig.MessageSearch;
        }
        return true;
      });
    };
    const extensionListShowInStart = computed(() => {
      if (isPC) {
        const extensionList = currentExtensionList.value.filter((extension) => {
          var _a;
          return ((_a = extension == null ? void 0 : extension.data) == null ? void 0 : _a.name) !== "search";
        });
        return extensionList;
      }
      return currentExtensionList.value;
    });
    const extensionListShowInEnd = computed(() => {
      if (isPC) {
        const searchExtension = currentExtensionList.value.find((extension) => {
          var _a;
          return ((_a = extension == null ? void 0 : extension.data) == null ? void 0 : _a.name) === "search";
        });
        return searchExtension ? [searchExtension] : [];
      }
      return [];
    });
    const onExtensionClick = (extension) => {
      var _a, _b, _c;
      switch ((_a = extension == null ? void 0 : extension.data) == null ? void 0 : _a.name) {
        case "voiceCall":
          onCallExtensionClicked(extension, 1);
          break;
        case "videoCall":
          onCallExtensionClicked(extension, 2);
          break;
        default:
          (_c = extension == null ? void 0 : extension.listener) == null ? void 0 : _c.onClicked((_b = currentConversation.value) == null ? void 0 : _b._conversation);
          break;
      }
    };
    const onCallExtensionClicked = (extension, callType) => {
      var _a, _b, _c, _d, _e, _f, _g;
      selectorShowType.value = (_a = extension == null ? void 0 : extension.data) == null ? void 0 : _a.name;
      if (((_b = currentConversation == null ? void 0 : currentConversation.value) == null ? void 0 : _b.type) === TUIChatEngine.TYPES.CONV_C2C) {
        (_f = (_c = extension.listener) == null ? void 0 : _c.onClicked) == null ? void 0 : _f.call(_c, {
          userIDList: [(_e = (_d = currentConversation == null ? void 0 : currentConversation.value) == null ? void 0 : _d.conversationID) == null ? void 0 : _e.slice(3)],
          type: callType
        });
      } else if (isGroup.value) {
        currentUserSelectorExtension.value = extension;
        ((_g = userSelectorRef == null ? void 0 : userSelectorRef.value) == null ? void 0 : _g.toggleShow) && userSelectorRef.value.toggleShow(true);
      }
    };
    const genExtensionIcon = (extension) => {
      return extension == null ? void 0 : extension.icon;
    };
    const genExtensionText = (extension) => {
      return extension == null ? void 0 : extension.text;
    };
    const onUserSelectorSubmit = (selectedInfo) => {
      var _a, _b, _c;
      (_c = (_b = (_a = currentUserSelectorExtension.value) == null ? void 0 : _a.listener) == null ? void 0 : _b.onClicked) == null ? void 0 : _c.call(_b, selectedInfo);
      currentUserSelectorExtension.value = void 0;
    };
    const onUserSelectorCancel = () => {
      currentUserSelectorExtension.value = void 0;
    };
    const insertEmoji = (emojiObj) => {
      emits("insertEmoji", emojiObj);
    };
    const dialogShowInH5 = (dialogDom) => {
      var _a, _b;
      if (!isH5) {
        return;
      }
      ((_a = h5Dialog.value) == null ? void 0 : _a.appendChild) && ((_b = h5Dialog.value) == null ? void 0 : _b.appendChild(dialogDom));
    };
    const dialogCloseInH5 = (dialogDom) => {
      var _a, _b;
      if (!isH5) {
        return;
      }
      if (dialogDom) {
        ((_a = h5Dialog.value) == null ? void 0 : _a.removeChild) && ((_b = h5Dialog.value) == null ? void 0 : _b.removeChild(dialogDom));
      }
    };
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", {
        class: normalizeClass([
          "message-input-toolbar",
          !unref(isPC) && "message-input-toolbar-h5",
          unref(isUniFrameWork) && "message-input-toolbar-uni"
        ])
      }, [
        createElementVNode("div", {
          class: normalizeClass([
            "message-input-toolbar-list",
            !unref(isPC) && "message-input-toolbar-h5-list",
            unref(isUniFrameWork) && "message-input-toolbar-uni-list"
          ])
        }, [
          unref(isRenderedEmojiPicker) ? (openBlock(), createBlock(EmojiPicker, {
            key: 0,
            ref_key: "emojiPickerRef",
            ref: emojiPickerRef,
            onInsertEmoji: insertEmoji,
            onDialogShowInH5: dialogShowInH5,
            onDialogCloseInH5: dialogCloseInH5,
            onChangeToolbarDisplayType: _cache[0] || (_cache[0] = (type) => emits("changeToolbarDisplayType", type))
          }, null, 512)) : createCommentVNode("", true),
          unref(featureConfig).InputImage ? (openBlock(), createBlock(ImageUpload, {
            key: 1,
            imageSourceType: "album"
          })) : createCommentVNode("", true),
          unref(featureConfig).InputFile ? (openBlock(), createBlock(FileUpload, { key: 2 })) : createCommentVNode("", true),
          unref(featureConfig).InputVideo ? (openBlock(), createBlock(VideoUpload, {
            key: 3,
            videoSourceType: "album"
          })) : createCommentVNode("", true),
          unref(featureConfig).InputEvaluation ? (openBlock(), createBlock(Evaluate, { key: 4 })) : createCommentVNode("", true),
          unref(featureConfig).InputQuickReplies ? (openBlock(), createBlock(Words, { key: 5 })) : createCommentVNode("", true),
          unref(extensionListShowInStart)[0] ? (openBlock(true), createElementBlock(Fragment, { key: 6 }, renderList(unref(extensionListShowInStart), (extension) => {
            return openBlock(), createBlock(ToolbarItemContainer, {
              key: extension.id,
              iconFile: genExtensionIcon(extension),
              title: genExtensionText(extension),
              iconWidth: unref(isUniFrameWork) ? "25px" : "20px",
              iconHeight: unref(isUniFrameWork) ? "25px" : "20px",
              needDialog: false,
              onOnIconClick: ($event) => onExtensionClick(extension)
            }, null, 8, ["iconFile", "title", "iconWidth", "iconHeight", "onOnIconClick"]);
          }), 128)) : createCommentVNode("", true)
        ], 2),
        unref(extensionListShowInEnd)[0] && unref(isPC) ? (openBlock(), createElementBlock("div", _hoisted_1, [
          (openBlock(true), createElementBlock(Fragment, null, renderList(unref(extensionListShowInEnd), (extension, index) => {
            return openBlock(), createBlock(ToolbarItemContainer, {
              key: index,
              iconFile: genExtensionIcon(extension),
              title: genExtensionText(extension),
              iconWidth: unref(isUniFrameWork) ? "25px" : "20px",
              iconHeight: unref(isUniFrameWork) ? "25px" : "20px",
              needDialog: false,
              onOnIconClick: ($event) => onExtensionClick(extension)
            }, null, 8, ["iconFile", "title", "iconWidth", "iconHeight", "onOnIconClick"]);
          }), 128))
        ])) : createCommentVNode("", true),
        createVNode(_sfc_main$1, {
          ref_key: "userSelectorRef",
          ref: userSelectorRef,
          type: unref(selectorShowType),
          currentConversation: unref(currentConversation),
          isGroup: unref(isGroup),
          onSubmit: onUserSelectorSubmit,
          onCancel: onUserSelectorCancel
        }, null, 8, ["type", "currentConversation", "isGroup"]),
        unref(isH5) ? (openBlock(), createElementBlock("div", {
          key: 1,
          ref_key: "h5Dialog",
          ref: h5Dialog,
          class: normalizeClass(["message-input-toolbar-h5-dialog"])
        }, null, 512)) : createCommentVNode("", true)
      ], 2);
    };
  }
});
export {
  _sfc_main as default
};
