import { defineComponent, ref, onMounted, onUnmounted, openBlock, createBlock, unref, withCtx, createVNode } from "vue";
import "../../../adapter-vue.mjs";
import TUIChatEngine, { TUIStore, StoreName, TUITranslateService, TUIChatService } from "@tencentcloud/chat-uikit-engine";
import Overlay from "../../common/Overlay/index.vue.mjs";
import Transfer from "../../common/Transfer/index.vue.mjs";
import { Toast } from "../../common/Toast/index.mjs";
import { isUniFrameWork } from "../../../utils/env.mjs";
import { isEnabledMessageReadReceiptGlobal } from "../utils/utils.mjs";
import OfflinePushInfoManager from "../offlinePushInfoManager/index.mjs";
import TOAST_TYPE from "../../common/Toast/type.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  emits: ["toggleMultipleSelectMode"],
  setup(__props, { emit: __emit }) {
    const emits = __emit;
    let selectedToForwardMessageIDList = [];
    let isMergeForward = false;
    const isShowForwardPanel = ref(false);
    const customConversationList = ref();
    onMounted(() => {
      TUIStore.watch(StoreName.CUSTOM, {
        singleForwardMessageID: onSingleForwardMessageIDUpdated,
        multipleForwardMessageID: onMultipleForwardMessageIDUpdated
      });
    });
    onUnmounted(() => {
      TUIStore.unwatch(StoreName.CUSTOM, {
        singleForwardMessageID: onSingleForwardMessageIDUpdated,
        multipleForwardMessageID: onMultipleForwardMessageIDUpdated
      });
      clearStoreData();
    });
    function onSingleForwardMessageIDUpdated(messageID) {
      if (typeof messageID !== "undefined") {
        isMergeForward = false;
        selectedToForwardMessageIDList = [messageID];
        openForwardPanel();
      }
    }
    function onMultipleForwardMessageIDUpdated(params) {
      if (!params) {
        return;
      }
      isMergeForward = false;
      const {
        isMergeForward: _isMergeForward,
        messageIDList: selectedMessageIDList
      } = params || {};
      if ((selectedMessageIDList == null ? void 0 : selectedMessageIDList.length) > 0) {
        isMergeForward = _isMergeForward;
        selectedToForwardMessageIDList = selectedMessageIDList;
        openForwardPanel();
      } else {
        Toast({
          message: TUITranslateService.t("TUIChat.未选择消息"),
          type: TOAST_TYPE.ERROR
        });
      }
    }
    function clearStoreData() {
      TUIStore.update(StoreName.CUSTOM, "singleForwardMessageID", void 0);
      TUIStore.update(StoreName.CUSTOM, "multipleForwardMessageID", void 0);
    }
    function closeForwardPanel() {
      clearStoreData();
      isShowForwardPanel.value = false;
    }
    function openForwardPanel() {
      getTransforRenderDataList();
      isShowForwardPanel.value = true;
    }
    function finishSelected(selectedConvIDWrapperList) {
      if ((selectedConvIDWrapperList == null ? void 0 : selectedConvIDWrapperList.length) === 0) return;
      const selectedConversationList = selectedConvIDWrapperList.map((IDWrapper) => TUIStore.getConversationModel(IDWrapper.userID));
      const unsentMessageQueue = selectedToForwardMessageIDList.map((messageID) => TUIStore.getMessageModel(messageID)).sort((a, b) => a.time - b.time);
      const forwardPromises = selectedConversationList.map((conversation) => {
        const offlinePushInfoCreateParams = {
          conversation,
          messageType: TUIChatEngine.TYPES.MSG_MERGER
        };
        return TUIChatService.sendForwardMessage(
          [conversation],
          unsentMessageQueue,
          {
            needMerge: isMergeForward,
            offlinePushInfo: OfflinePushInfoManager.create(offlinePushInfoCreateParams),
            params: {
              needReadReceipt: isEnabledMessageReadReceiptGlobal()
            }
          }
        );
      });
      Promise.allSettled(forwardPromises).then((results) => {
        for (const result of results) {
          const { status } = result;
          if (status === "rejected") {
            const errorMessage = result.reason.code === 80001 ? TUITranslateService.t("TUIChat.内容包含敏感词汇") : result.reason.message;
            Toast({
              message: errorMessage,
              type: TOAST_TYPE.ERROR
            });
            break;
          }
        }
      });
      closeForwardPanel();
      emits("toggleMultipleSelectMode", false);
    }
    function getTransforRenderDataList() {
      const conversationList = TUIStore.getData(StoreName.CONV, "conversationList");
      customConversationList.value = conversationList.map((conversation) => {
        return {
          // To achieve reusability of Transfer, userID is used here instead of ConversationID
          userID: conversation.conversationID,
          nick: conversation.getShowName(),
          avatar: conversation.getAvatar()
        };
      });
    }
    return (_ctx, _cache) => {
      return openBlock(), createBlock(Overlay, {
        visible: unref(isShowForwardPanel),
        useMask: false
      }, {
        default: withCtx(() => [
          createVNode(Transfer, {
            title: unref(TUITranslateService).t("TUIChat.转发"),
            isSearch: false,
            isCustomItem: false,
            list: unref(customConversationList),
            isHiddenBackIcon: unref(isUniFrameWork),
            onCancel: closeForwardPanel,
            onSubmit: finishSelected
          }, null, 8, ["title", "list", "isHiddenBackIcon"])
        ]),
        _: 1
      }, 8, ["visible"]);
    };
  }
});
export {
  _sfc_main as default
};
