import { defineComponent, ref, onMounted, onUnmounted, watchEffect, computed, unref, openBlock, createElementBlock, normalizeClass, renderSlot, createCommentVNode, createElementVNode, Fragment, renderList, createVNode, toDisplayString } from "vue";
import TUIChatEngine, { TUITranslateService, TUIStore, StoreName } from "@tencentcloud/chat-uikit-engine";
import { TUIGlobal } from "@tencentcloud/universal-api";
import "../../../../adapter-vue.mjs";
import Icon from "../../../common/Icon.vue.mjs";
import { Toast } from "../../../common/Toast/index.mjs";
import delIcon from "../../../../assets/icon/msg-del.svg.mjs";
import copyIcon from "../../../../assets/icon/msg-copy.svg.mjs";
import quoteIcon from "../../../../assets/icon/msg-quote.svg.mjs";
import revokeIcon from "../../../../assets/icon/msg-revoke.svg.mjs";
import forwardIcon from "../../../../assets/icon/msg-forward.svg.mjs";
import translateIcon from "../../../../assets/icon/translate.svg.mjs";
import multipleSelectIcon from "../../../../assets/icon/multiple-select.svg.mjs";
import convertText from "../../../../assets/icon/convertText_zh.svg.mjs";
import { enableSampleTaskStatus } from "../../../../utils/enableSampleTaskStatus.mjs";
import { transformTextWithKeysToEmojiNames } from "../../emoji-config/index.mjs";
import { isPC, isUniFrameWork, isH5 } from "../../../../utils/env.mjs";
import ChatConfig from "../../config.mjs";
import CopyManager from "../../utils/copy.mjs";
import TOAST_TYPE from "../../../common/Toast/type.mjs";
const _hoisted_1 = ["onClick", "onMousedown"];
const _hoisted_2 = { class: "list-item-text" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    messageItem: { default: () => ({}) },
    isMultipleSelectMode: { type: Boolean, default: false }
  },
  emits: ["toggleMultipleSelectMode"],
  setup(__props, { expose: __expose, emit: __emit }) {
    const emits = __emit;
    const props = __props;
    const featureConfig = ChatConfig.getFeatureConfig();
    const TYPES = TUIChatEngine.TYPES;
    const actionItems = ref([
      {
        key: "open",
        text: TUITranslateService.t("TUIChat.打开"),
        iconUrl: copyIcon,
        renderCondition() {
          var _a;
          if (!featureConfig.DownloadFile || !message.value) return false;
          return isPC && (((_a = message.value) == null ? void 0 : _a.type) === TYPES.MSG_FILE || message.value.type === TYPES.MSG_VIDEO || message.value.type === TYPES.MSG_IMAGE);
        },
        clickEvent: openMessage
      },
      {
        key: "copy",
        text: TUITranslateService.t("TUIChat.复制"),
        iconUrl: copyIcon,
        renderCondition() {
          if (!featureConfig.CopyMessage || !message.value) return false;
          return message.value.type === TYPES.MSG_TEXT;
        },
        clickEvent: copyMessage
      },
      {
        key: "revoke",
        text: TUITranslateService.t("TUIChat.撤回"),
        iconUrl: revokeIcon,
        renderCondition() {
          if (!featureConfig.RevokeMessage || !message.value) return false;
          return message.value.flow === "out" && message.value.status === "success";
        },
        clickEvent: revokeMessage
      },
      {
        key: "delete",
        text: TUITranslateService.t("TUIChat.删除"),
        iconUrl: delIcon,
        renderCondition() {
          if (!featureConfig.DeleteMessage || !message.value) return false;
          return message.value.status === "success";
        },
        clickEvent: deleteMessage
      },
      {
        key: "forward",
        text: TUITranslateService.t("TUIChat.转发"),
        iconUrl: forwardIcon,
        renderCondition() {
          if (!featureConfig.ForwardMessage || !message.value) return false;
          return message.value.status === "success";
        },
        clickEvent: forwardSingleMessage
      },
      {
        key: "quote",
        text: TUITranslateService.t("TUIChat.引用"),
        iconUrl: quoteIcon,
        renderCondition() {
          if (!featureConfig.QuoteMessage || !message.value) return false;
          const _message = TUIStore.getMessageModel(message.value.ID);
          return message.value.status === "success" && !_message.getSignalingInfo();
        },
        clickEvent: quoteMessage
      },
      {
        key: "translate",
        text: TUITranslateService.t("TUIChat.翻译"),
        visible: false,
        iconUrl: translateIcon,
        renderCondition() {
          if (!featureConfig.TranslateMessage || !message.value) return false;
          return message.value.status === "success" && message.value.type === TYPES.MSG_TEXT;
        },
        clickEvent: translateMessage
      },
      {
        key: "convert",
        text: TUITranslateService.t("TUIChat.转文字"),
        visible: false,
        iconUrl: convertText,
        renderCondition() {
          if (!featureConfig.VoiceToText || !message.value) return false;
          return message.value.status === "success" && message.value.type === TYPES.MSG_AUDIO;
        },
        clickEvent: convertVoiceToText
      },
      {
        key: "multi-select",
        text: TUITranslateService.t("TUIChat.多选"),
        iconUrl: multipleSelectIcon,
        renderCondition() {
          if (!featureConfig.MultiSelection || !message.value) return false;
          return message.value.status === "success";
        },
        clickEvent: multipleSelectMessage
      }
    ]);
    const message = ref();
    const messageToolDom = ref();
    onMounted(() => {
      TUIStore.watch(StoreName.CHAT, {
        translateTextInfo: onMessageTranslationInfoUpdated,
        voiceToTextInfo: onMessageConvertInfoUpdated
      });
    });
    onUnmounted(() => {
      TUIStore.unwatch(StoreName.CHAT, {
        translateTextInfo: onMessageTranslationInfoUpdated,
        voiceToTextInfo: onMessageConvertInfoUpdated
      });
    });
    watchEffect(() => {
      message.value = TUIStore.getMessageModel(props.messageItem.ID);
    });
    const isAllActionItemInvalid = computed(() => {
      for (let i = 0; i < actionItems.value.length; ++i) {
        if (actionItems.value[i].renderCondition()) {
          return false;
        }
      }
      return true;
    });
    function getFunction(index) {
      actionItems.value[index].clickEvent();
    }
    function openMessage() {
      var _a;
      let url = "";
      switch ((_a = message.value) == null ? void 0 : _a.type) {
        case TUIChatEngine.TYPES.MSG_FILE:
          url = message.value.payload.fileUrl;
          break;
        case TUIChatEngine.TYPES.MSG_VIDEO:
          url = message.value.payload.remoteVideoUrl;
          break;
        case TUIChatEngine.TYPES.MSG_IMAGE:
          url = message.value.payload.imageInfoArray[0].url;
          break;
      }
      window == null ? void 0 : window.open(url, "_blank");
    }
    function revokeMessage() {
      if (!message.value) return;
      const messageModel = TUIStore.getMessageModel(message.value.ID);
      messageModel.revokeMessage().then(() => {
        enableSampleTaskStatus("revokeMessage");
      }).catch((error) => {
        if (error.code === 20016) {
          const message2 = TUITranslateService.t("TUIChat.已过撤回时限");
          Toast({
            message: message2,
            type: TOAST_TYPE.ERROR
          });
        }
      });
    }
    function deleteMessage() {
      if (!message.value) return;
      const messageModel = TUIStore.getMessageModel(message.value.ID);
      messageModel.deleteMessage();
    }
    async function copyMessage() {
      var _a, _b, _c, _d, _e;
      if (isUniFrameWork) {
        (_c = TUIGlobal) == null ? void 0 : _c.setClipboardData({
          data: transformTextWithKeysToEmojiNames((_b = (_a = message.value) == null ? void 0 : _a.payload) == null ? void 0 : _b.text)
        });
      } else {
        CopyManager.copySelection((_e = (_d = message.value) == null ? void 0 : _d.payload) == null ? void 0 : _e.text);
      }
    }
    function beforeCopy(key) {
      if (key !== "copy" || isH5) {
        return;
      }
      CopyManager.saveCurrentSelection();
    }
    function forwardSingleMessage() {
      if (!message.value) return;
      TUIStore.update(StoreName.CUSTOM, "singleForwardMessageID", message.value.ID);
    }
    function quoteMessage() {
      if (!message.value) return;
      message.value.quoteMessage();
    }
    function translateMessage() {
      const enable = TUIStore.getData(StoreName.APP, "enabledTranslationPlugin");
      if (!enable) {
        Toast({
          message: TUITranslateService.t("TUIChat.请开通翻译功能"),
          type: TOAST_TYPE.WARNING
        });
        return;
      }
      if (!message.value) return;
      const index = actionItems.value.findIndex((item) => item.key === "translate");
      TUIStore.update(StoreName.CHAT, "translateTextInfo", {
        conversationID: message.value.conversationID,
        messageID: message.value.ID,
        visible: !actionItems.value[index].visible
      });
    }
    function convertVoiceToText() {
      const enable = TUIStore.getData(StoreName.APP, "enabledVoiceToText");
      if (!enable) {
        Toast({
          message: TUITranslateService.t("TUIChat.请开通语音转文字功能")
        });
        return;
      }
      if (!message.value) return;
      const index = actionItems.value.findIndex((item) => item.key === "convert");
      TUIStore.update(StoreName.CHAT, "voiceToTextInfo", {
        conversationID: message.value.conversationID,
        messageID: message.value.ID,
        visible: !actionItems.value[index].visible
      });
    }
    function multipleSelectMessage() {
      emits("toggleMultipleSelectMode");
    }
    function onMessageTranslationInfoUpdated(info) {
      if (info === void 0) return;
      const translationInfoList = info.get(props.messageItem.conversationID) || [];
      const idx = actionItems.value.findIndex((item) => item.key === "translate");
      for (let i = 0; i < translationInfoList.length; ++i) {
        const { messageID, visible } = translationInfoList[i];
        if (messageID === props.messageItem.ID) {
          actionItems.value[idx].text = TUITranslateService.t(visible ? "TUIChat.隐藏" : "TUIChat.翻译");
          actionItems.value[idx].visible = !!visible;
          return;
        }
      }
      actionItems.value[idx].text = TUITranslateService.t("TUIChat.翻译");
    }
    function onMessageConvertInfoUpdated(info) {
      if (info === void 0) return;
      const convertInfoList = info.get(props.messageItem.conversationID) || [];
      const idx = actionItems.value.findIndex((item) => item.key === "convert");
      for (let i = 0; i < convertInfoList.length; ++i) {
        const { messageID, visible } = convertInfoList[i];
        if (messageID === props.messageItem.ID) {
          actionItems.value[idx].text = TUITranslateService.t(visible ? "TUIChat.隐藏" : "TUIChat.转文字");
          actionItems.value[idx].visible = !!visible;
          return;
        }
      }
      actionItems.value[idx].text = TUITranslateService.t("TUIChat.转文字");
    }
    __expose({
      messageToolDom
    });
    return (_ctx, _cache) => {
      return !unref(isAllActionItemInvalid) && !_ctx.messageItem.hasRiskContent ? (openBlock(), createElementBlock("div", {
        key: 0,
        ref_key: "messageToolDom",
        ref: messageToolDom,
        class: normalizeClass(["dialog-item", !unref(isPC) ? "dialog-item-h5" : "dialog-item-web"])
      }, [
        unref(featureConfig).EmojiReaction ? renderSlot(_ctx.$slots, "TUIEmojiPlugin", { key: 0 }, void 0, true) : createCommentVNode("", true),
        createElementVNode("div", {
          class: normalizeClass(["dialog-item-list", !unref(isPC) ? "dialog-item-list-h5" : "dialog-item-list-web"])
        }, [
          (openBlock(true), createElementBlock(Fragment, null, renderList(unref(actionItems), (item, index) => {
            return openBlock(), createElementBlock(Fragment, null, [
              item.renderCondition() ? (openBlock(), createElementBlock("div", {
                key: item.key,
                class: "list-item",
                onClick: ($event) => getFunction(index),
                onMousedown: ($event) => beforeCopy(item.key)
              }, [
                createVNode(Icon, {
                  file: item.iconUrl,
                  size: "15px"
                }, null, 8, ["file"]),
                createElementVNode("span", _hoisted_2, toDisplayString(item.text), 1)
              ], 40, _hoisted_1)) : createCommentVNode("", true)
            ], 64);
          }), 256))
        ], 2)
      ], 2)) : createCommentVNode("", true);
    };
  }
});
export {
  _sfc_main as default
};
