import { defineComponent, ref, watch, computed, openBlock, createElementBlock, normalizeClass, unref, createElementVNode, createVNode, toDisplayString, Fragment, renderList, withCtx, createCommentVNode } from "vue";
import "../../../../../adapter-vue.mjs";
import TUIChatEngine, { TUIChatService, TUIStore, TUITranslateService } from "@tencentcloud/chat-uikit-engine";
import backIcon from "../../../../../assets/icon/back.svg.mjs";
import playIcon from "../../../../../assets/icon/video-play.png.mjs";
import Icon from "../../../../common/Icon.vue.mjs";
import MessageContainer from "./message-container.vue.mjs";
import MessageRecord from "../message-record/index.vue.mjs";
import { parseTextToRenderArray } from "../../../emoji-config/index.mjs";
import { isMobile, isUniFrameWork } from "../../../../../utils/env.mjs";
import { CUSTOM_BIG_EMOJI_URL } from "../../../emoji-config/custom-emoji.mjs";
import { DEFAULT_BIG_EMOJI_URL } from "../../../emoji-config/default-emoji.mjs";
const _hoisted_1 = { class: "header-container" };
const _hoisted_2 = { key: 0 };
const _hoisted_3 = { key: 1 };
const _hoisted_4 = { class: "title" };
const _hoisted_5 = { key: 0 };
const _hoisted_6 = {
  key: 0,
  class: "message-text"
};
const _hoisted_7 = {
  key: 0,
  class: "text"
};
const _hoisted_8 = ["src"];
const _hoisted_9 = {
  key: 1,
  class: "message-image"
};
const _hoisted_10 = ["src"];
const _hoisted_11 = {
  key: 2,
  class: "message-video"
};
const _hoisted_12 = ["onClick"];
const _hoisted_13 = ["src"];
const _hoisted_14 = ["poster"];
const _hoisted_15 = ["src"];
const _hoisted_16 = {
  key: 3,
  class: "message-audio"
};
const _hoisted_17 = {
  key: 4,
  class: "message-face"
};
const _hoisted_18 = ["src"];
const _hoisted_19 = {
  key: 5,
  class: "message-file"
};
const _hoisted_20 = { key: 6 };
const _hoisted_21 = ["onClickCapture"];
const _hoisted_22 = { key: 8 };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    messageID: { default: "" },
    isMounted: { type: Boolean, default: false }
  },
  emits: ["closeOverlay"],
  setup(__props, { emit: __emit }) {
    const emits = __emit;
    const props = __props;
    const TYPES = TUIChatEngine.TYPES;
    const isDownloadOccurError = ref(false);
    const messageListStack = ref([]);
    const currentMergeMessageInfo = ref({
      title: "",
      messageList: []
    });
    const simpleMessageListRef = ref();
    watch(() => messageListStack.value.length, async (newValue) => {
      isDownloadOccurError.value = false;
      if (newValue < 1) {
        return;
      }
      const stackTopMessageInfo = messageListStack.value[messageListStack.value.length - 1];
      if (stackTopMessageInfo.downloadKey && stackTopMessageInfo.messageList.length === 0) {
        try {
          const res = await TUIChatService.downloadMergedMessages({
            payload: stackTopMessageInfo,
            type: TUIChatEngine.TYPES.MSG_MERGER
          });
          messageListStack.value[messageListStack.value.length - 1] = res.payload;
        } catch (error) {
          isDownloadOccurError.value = true;
        }
      }
      currentMergeMessageInfo.value = messageListStack.value[messageListStack.value.length - 1];
    });
    watch(() => props.isMounted, (newValue) => {
      if (newValue) {
        if (!props.messageID) {
          throw new Error("messageID is required when first render of simple-message-list.");
        }
        const sdkMessagePayload = TUIStore.getMessageModel(props.messageID).getMessage().payload;
        messageListStack.value = [sdkMessagePayload];
      } else {
        messageListStack.value = [];
      }
    }, {
      immediate: true
    });
    const isReturn = computed(() => {
      return messageListStack.value.length > 1;
    });
    const isMergeMessageInfoLoaded = computed(() => {
      var _a;
      return ((_a = currentMergeMessageInfo.value) == null ? void 0 : _a.messageList) ? currentMergeMessageInfo.value.messageList.length > 0 : false;
    });
    function entryNextLevel(e, sdkMessage) {
      messageListStack.value.push(sdkMessage.messageBody[0].payload);
      e.stopPropagation();
    }
    function backPreviousLevel() {
      messageListStack.value.pop();
      if (messageListStack.value.length < 1) {
        emits("closeOverlay");
      }
    }
    function previewVideoInUniapp(url) {
      if (isUniFrameWork) {
        const encodedUrl = encodeURIComponent(url);
        uni.navigateTo({
          url: `/TUIKit/components/TUIChat/video-play?videoUrl=${encodedUrl}`
        });
      }
    }
    function resolveBigFaceUrl(bigFaceKey) {
      let url = "";
      if (bigFaceKey.indexOf("@custom") > -1) {
        url = CUSTOM_BIG_EMOJI_URL + bigFaceKey;
      } else {
        url = DEFAULT_BIG_EMOJI_URL + bigFaceKey;
        if (url.indexOf("@2x") === -1) {
          url += "@2x.png";
        } else {
          url += ".png";
        }
      }
      return url;
    }
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", {
        class: normalizeClass({
          "simple-message-list-container": true,
          "simple-message-list-container-mobile": unref(isMobile)
        })
      }, [
        createElementVNode("div", _hoisted_1, [
          createElementVNode("span", {
            class: "back",
            onClick: backPreviousLevel
          }, [
            createVNode(Icon, {
              class: "close-icon",
              file: unref(backIcon),
              size: "18px"
            }, null, 8, ["file"]),
            unref(isReturn) ? (openBlock(), createElementBlock("span", _hoisted_2, toDisplayString(unref(TUITranslateService).t("TUIChat.返回")), 1)) : (openBlock(), createElementBlock("span", _hoisted_3, toDisplayString(unref(TUITranslateService).t("TUIChat.关闭")), 1))
          ]),
          createElementVNode("span", _hoisted_4, toDisplayString(unref(currentMergeMessageInfo).title), 1)
        ]),
        unref(isDownloadOccurError) ? (openBlock(), createElementBlock("div", _hoisted_5, " Load Merge Message Error ")) : unref(isMergeMessageInfoLoaded) ? (openBlock(), createElementBlock("div", {
          key: 1,
          ref_key: "simpleMessageListRef",
          ref: simpleMessageListRef,
          class: "message-list"
        }, [
          (openBlock(true), createElementBlock(Fragment, null, renderList(unref(currentMergeMessageInfo).messageList, (item) => {
            return openBlock(), createElementBlock("div", {
              key: item.ID,
              class: normalizeClass({
                "message-item": true
              })
            }, [
              createVNode(MessageContainer, {
                sender: item.nick,
                avatar: item.avatar,
                type: item.messageBody[0].type,
                time: item.time
              }, {
                default: withCtx(() => [
                  item.messageBody[0].type === unref(TYPES).MSG_TEXT ? (openBlock(), createElementBlock("div", _hoisted_6, [
                    (openBlock(true), createElementBlock(Fragment, null, renderList(unref(parseTextToRenderArray)(item.messageBody[0].payload["text"]), (textInfo, index) => {
                      return openBlock(), createElementBlock("span", {
                        key: index,
                        class: "message-text-container"
                      }, [
                        textInfo.type === "text" ? (openBlock(), createElementBlock("span", _hoisted_7, toDisplayString(textInfo.content), 1)) : (openBlock(), createElementBlock("img", {
                          key: 1,
                          class: "simple-emoji",
                          src: textInfo.content,
                          alt: "small-face"
                        }, null, 8, _hoisted_8))
                      ]);
                    }), 128))
                  ])) : item.messageBody[0].type === unref(TYPES).MSG_IMAGE ? (openBlock(), createElementBlock("div", _hoisted_9, [
                    createElementVNode("img", {
                      class: "image",
                      src: item.messageBody[0].payload["imageInfoArray"][2]["url"],
                      mode: "widthFix",
                      alt: "image"
                    }, null, 8, _hoisted_10)
                  ])) : item.messageBody[0].type === unref(TYPES).MSG_VIDEO ? (openBlock(), createElementBlock("div", _hoisted_11, [
                    unref(isUniFrameWork) ? (openBlock(), createElementBlock("div", {
                      key: 0,
                      onClick: ($event) => previewVideoInUniapp(item.messageBody[0].payload["remoteVideoUrl"])
                    }, [
                      createElementVNode("image", {
                        class: "image",
                        src: item.messageBody[0].payload["thumbUrl"],
                        mode: "widthFix",
                        alt: "image"
                      }, null, 8, _hoisted_13),
                      createVNode(Icon, {
                        class: "video-play-icon",
                        file: unref(playIcon)
                      }, null, 8, ["file"])
                    ], 8, _hoisted_12)) : (openBlock(), createElementBlock("video", {
                      key: 1,
                      class: "video",
                      controls: "",
                      poster: item.messageBody[0].payload["thumbUrl"]
                    }, [
                      createElementVNode("source", {
                        src: item.messageBody[0].payload["remoteVideoUrl"],
                        type: "video/mp4"
                      }, null, 8, _hoisted_15)
                    ], 8, _hoisted_14))
                  ])) : item.messageBody[0].type === unref(TYPES).MSG_AUDIO ? (openBlock(), createElementBlock("div", _hoisted_16, [
                    createElementVNode("span", null, toDisplayString(unref(TUITranslateService).t("TUIChat.语音")) + " ", 1),
                    createElementVNode("span", null, toDisplayString(item.messageBody[0].payload.second) + "s", 1)
                  ])) : item.messageBody[0].type === unref(TYPES).MSG_FACE ? (openBlock(), createElementBlock("div", _hoisted_17, [
                    createElementVNode("img", {
                      class: "image",
                      src: resolveBigFaceUrl(item.messageBody[0].payload.data),
                      alt: "face"
                    }, null, 8, _hoisted_18)
                  ])) : item.messageBody[0].type === unref(TYPES).MSG_FILE ? (openBlock(), createElementBlock("div", _hoisted_19, toDisplayString(unref(TUITranslateService).t("TUIChat.[文件]")), 1)) : item.messageBody[0].type === unref(TYPES).MSG_LOCATION ? (openBlock(), createElementBlock("div", _hoisted_20, toDisplayString(unref(TUITranslateService).t("TUIChat.[地理位置]")), 1)) : item.messageBody[0].type === unref(TYPES).MSG_MERGER ? (openBlock(), createElementBlock("div", {
                    key: 7,
                    class: "message-merger",
                    onClickCapture: ($event) => entryNextLevel($event, item)
                  }, [
                    createVNode(MessageRecord, {
                      disabled: "",
                      renderData: item.messageBody[0].payload
                    }, null, 8, ["renderData"])
                  ], 40, _hoisted_21)) : item.messageBody[0].type === unref(TYPES).MSG_CUSTOM ? (openBlock(), createElementBlock("div", _hoisted_22, toDisplayString(unref(TUITranslateService).t("TUIChat.[自定义消息]")), 1)) : createCommentVNode("", true)
                ]),
                _: 2
              }, 1032, ["sender", "avatar", "type", "time"])
            ]);
          }), 128))
        ], 512)) : createCommentVNode("", true)
      ], 2);
    };
  }
});
export {
  _sfc_main as default
};
