import TUIChatEngine, { TUIStore, StoreName, TUIChatService, TUITranslateService } from "@tencentcloud/chat-uikit-engine";
import { Toast } from "../../common/Toast/index.mjs";
import { isEnabledMessageReadReceiptGlobal } from "./utils.mjs";
import { enableSampleTaskStatus } from "../../../utils/enableSampleTaskStatus.mjs";
import OfflinePushInfoManager from "../offlinePushInfoManager/index.mjs";
import TOAST_TYPE from "../../common/Toast/type.mjs";
const sendMessageErrorCodeMap = /* @__PURE__ */ new Map([
  [3123, "文本包含本地审核拦截词"],
  [4004, "图片消息失败,无效的图片格式"],
  [4005, "文件消息失败,禁止发送违规封禁的文件"],
  [7004, "文件不存在,请检查文件路径是否正确"],
  [7005, "文件大小超出了限制,如果上传文件,最大限制是100MB"],
  [8001, "消息长度超出限制,消息长度不要超过12K"],
  [80001, "消息或者资料中文本存在敏感内容,发送失败"],
  [80004, "消息中图片存在敏感内容,发送失败"]
]);
const sendMessages = async (messageList, currentConversation) => {
  if (TUIStore.getData(StoreName.CHAT, "messageSource")) {
    TUIStore.update(StoreName.CHAT, "messageSource", void 0);
  }
  messageList == null ? void 0 : messageList.forEach(async (content) => {
    var _a, _b, _c, _d, _e, _f, _g;
    try {
      const options = {
        to: ((_a = currentConversation == null ? void 0 : currentConversation.groupProfile) == null ? void 0 : _a.groupID) || ((_b = currentConversation == null ? void 0 : currentConversation.userProfile) == null ? void 0 : _b.userID),
        conversationType: currentConversation == null ? void 0 : currentConversation.type,
        payload: {},
        needReadReceipt: isEnabledMessageReadReceiptGlobal()
      };
      let textMessageContent;
      const sendMessageOptions = {
        offlinePushInfo: {}
      };
      const offlinePushInfoCreateParams = {
        conversation: currentConversation,
        payload: content.payload,
        messageType: ""
      };
      switch (content == null ? void 0 : content.type) {
        case "text":
          textMessageContent = JSON.parse(JSON.stringify((_c = content.payload) == null ? void 0 : _c.text));
          if (!textMessageContent) {
            break;
          }
          options.payload = {
            text: textMessageContent
          };
          offlinePushInfoCreateParams.messageType = TUIChatEngine.TYPES.MSG_TEXT;
          sendMessageOptions.offlinePushInfo = OfflinePushInfoManager.create(offlinePushInfoCreateParams);
          if ((_d = content.payload) == null ? void 0 : _d.atUserList) {
            options.payload.atUserList = content.payload.atUserList;
            await TUIChatService.sendTextAtMessage(options, sendMessageOptions);
          } else {
            await TUIChatService.sendTextMessage(options, sendMessageOptions);
          }
          break;
        case "image":
          options.payload = {
            file: (_e = content.payload) == null ? void 0 : _e.file
          };
          offlinePushInfoCreateParams.messageType = TUIChatEngine.TYPES.MSG_IMAGE;
          sendMessageOptions.offlinePushInfo = OfflinePushInfoManager.create(offlinePushInfoCreateParams);
          await TUIChatService.sendImageMessage(options, sendMessageOptions);
          break;
        case "video":
          options.payload = {
            file: (_f = content.payload) == null ? void 0 : _f.file
          };
          offlinePushInfoCreateParams.messageType = TUIChatEngine.TYPES.MSG_VIDEO;
          sendMessageOptions.offlinePushInfo = OfflinePushInfoManager.create(offlinePushInfoCreateParams);
          await TUIChatService.sendVideoMessage(options, sendMessageOptions);
          break;
        case "file":
          options.payload = {
            file: (_g = content.payload) == null ? void 0 : _g.file
          };
          offlinePushInfoCreateParams.messageType = TUIChatEngine.TYPES.MSG_FILE;
          sendMessageOptions.offlinePushInfo = OfflinePushInfoManager.create(offlinePushInfoCreateParams);
          await TUIChatService.sendFileMessage(options, sendMessageOptions);
          break;
        default:
          break;
      }
      enableSampleTaskStatus("sendMessage");
    } catch (error) {
      Toast({
        message: sendMessageErrorCodeMap.get(error == null ? void 0 : error.code) ? TUITranslateService.t(`TUIChat.${sendMessageErrorCodeMap.get(error.code)}`) : error == null ? void 0 : error.message,
        type: TOAST_TYPE.ERROR
      });
      if (TUIStore.getData(StoreName.CHAT, "quoteMessage")) {
        TUIStore.update(StoreName.CHAT, "quoteMessage", {});
      }
    }
  });
};
const sendTyping = (inputContentEmpty, inputBlur) => {
  if (!inputContentEmpty && !inputBlur) {
    TUIChatService.enterTypingState();
  } else {
    TUIChatService.leaveTypingState();
  }
};
export {
  sendMessageErrorCodeMap,
  sendMessages,
  sendTyping
};
