import { defineComponent, ref, computed, onMounted, unref, openBlock, createElementBlock, normalizeClass, toDisplayString, createCommentVNode } from "vue";
import "../../../../../adapter-vue.mjs";
import { TUIStore, TUITranslateService, StoreName } from "@tencentcloud/chat-uikit-engine";
import { getBoundingClientRect, getScrollInfo } from "@tencentcloud/universal-api";
import { isUniFrameWork } from "../../../../../utils/env.mjs";
import { Toast } from "../../../../common/Toast/index.mjs";
import { MessageQuoteTypeEnum } from "./interface.mjs";
import { transformTextWithKeysToEmojiNames } from "../../../emoji-config/index.mjs";
import TOAST_TYPE from "../../../../common/Toast/type.mjs";
const _hoisted_1 = {
  key: 0,
  class: "revoked-text"
};
const _hoisted_2 = {
  key: 1,
  class: "max-double-line"
};
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    message: { default: () => ({}) }
  },
  emits: ["scrollTo", "blinkMessage"],
  setup(__props, { emit: __emit }) {
    const emits = __emit;
    const props = __props;
    let selfAddValue = 0;
    const messageQuoteText = ref("");
    const hasQuoteContent = ref(false);
    const messageQuoteContent = ref({});
    const isMessageRevoked = computed(() => {
      var _a;
      try {
        const cloudCustomData = JSON.parse(((_a = props.message) == null ? void 0 : _a.cloudCustomData) || "{}");
        const quotedMessageModel = TUIStore.getMessageModel(cloudCustomData.messageReply.messageID);
        return quotedMessageModel == null ? void 0 : quotedMessageModel.isRevoked;
      } catch (error) {
        return true;
      }
    });
    onMounted(() => {
      var _a;
      try {
        const cloudCustomData = JSON.parse(((_a = props.message) == null ? void 0 : _a.cloudCustomData) || "{}");
        hasQuoteContent.value = Boolean(cloudCustomData.messageReply);
        if (hasQuoteContent.value) {
          messageQuoteContent.value = cloudCustomData.messageReply;
          messageQuoteText.value = performQuoteContent(messageQuoteContent.value);
        }
      } catch (error) {
        hasQuoteContent.value = false;
      }
    });
    function performQuoteContent(params) {
      let messageKey = "";
      let quoteContent = "";
      switch (params.messageType) {
        case MessageQuoteTypeEnum.TYPE_TEXT:
          messageKey = "[文本]";
          break;
        case MessageQuoteTypeEnum.TYPE_CUSTOM:
          messageKey = "[自定义消息]";
          break;
        case MessageQuoteTypeEnum.TYPE_IMAGE:
          messageKey = "[图片]";
          break;
        case MessageQuoteTypeEnum.TYPE_SOUND:
          messageKey = "[音频]";
          break;
        case MessageQuoteTypeEnum.TYPE_VIDEO:
          messageKey = "[视频]";
          break;
        case MessageQuoteTypeEnum.TYPE_FILE:
          messageKey = "[文件]";
          break;
        case MessageQuoteTypeEnum.TYPE_LOCATION:
          messageKey = "[地理位置]";
          break;
        case MessageQuoteTypeEnum.TYPE_FACE:
          messageKey = "[动画表情]";
          break;
        case MessageQuoteTypeEnum.TYPE_GROUP_TIPS:
          messageKey = "[群提示]";
          break;
        case MessageQuoteTypeEnum.TYPE_MERGER:
          messageKey = "[聊天记录]";
          break;
        default:
          messageKey = "[消息]";
          break;
      }
      if ([
        MessageQuoteTypeEnum.TYPE_TEXT,
        MessageQuoteTypeEnum.TYPE_MERGER
      ].includes(params.messageType)) {
        quoteContent = params.messageAbstract;
      }
      return quoteContent ? quoteContent : TUITranslateService.t(`TUIChat.${messageKey}`);
    }
    async function scrollToOriginalMessage() {
      var _a;
      if (isMessageRevoked.value) {
        return;
      }
      const originMessageID = (_a = messageQuoteContent.value) == null ? void 0 : _a.messageID;
      const currentMessageList = TUIStore.getData(StoreName.CHAT, "messageList");
      const isOriginalMessageInScreen = currentMessageList.some((msg) => msg.ID === originMessageID);
      if (originMessageID && isOriginalMessageInScreen) {
        try {
          const scrollViewRect = await getBoundingClientRect("#messageScrollList", "messageList");
          const originalMessageRect = await getBoundingClientRect("#tui-" + originMessageID, "messageList");
          const { scrollTop } = await getScrollInfo("#messageScrollList", "messageList");
          const finalScrollTop = originalMessageRect.top + scrollTop - scrollViewRect.top - selfAddValue++ % 2;
          const isNeedScroll = originalMessageRect.top < scrollViewRect.top;
          if (!isUniFrameWork && window) {
            const scrollView = document.getElementById("messageScrollList");
            if (isNeedScroll && scrollView) {
              scrollView.scrollTop = finalScrollTop;
            }
          } else if (isUniFrameWork && isNeedScroll) {
            emits("scrollTo", finalScrollTop);
          }
          emits("blinkMessage", originMessageID);
        } catch (error) {
          console.error(error);
        }
      } else {
        Toast({
          message: TUITranslateService.t("TUIChat.无法定位到原消息"),
          type: TOAST_TYPE.WARNING
        });
      }
    }
    return (_ctx, _cache) => {
      return unref(hasQuoteContent) ? (openBlock(), createElementBlock("div", {
        key: 0,
        class: normalizeClass({
          "reference-content": true,
          "reverse": _ctx.message.flow === "out"
        }),
        onClick: scrollToOriginalMessage
      }, [
        unref(isMessageRevoked) ? (openBlock(), createElementBlock("div", _hoisted_1, toDisplayString(unref(TUITranslateService).t("TUIChat.引用内容已撤回")), 1)) : (openBlock(), createElementBlock("div", _hoisted_2, toDisplayString(unref(messageQuoteContent).messageSender) + ": " + toDisplayString(unref(transformTextWithKeysToEmojiNames)(unref(messageQuoteText))), 1))
      ], 2)) : createCommentVNode("", true);
    };
  }
});
export {
  _sfc_main as default
};
