"use strict";
const common_vendor = require("../../../../common/vendor.js");
require("../../../adapter-vue.js");
const TUIKit_components_TUIChat_utils_conversationDraft = require("../utils/conversationDraft.js");
const TUIKit_components_TUIChat_emojiConfig_index = require("../emoji-config/index.js");
const TUIKit_utils_env = require("../../../utils/env.js");
const TUIKit_components_TUIChat_utils_sendMessage = require("../utils/sendMessage.js");
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
  __name: "message-input-editor",
  props: {
    placeholder: {
      type: String,
      default: "this is placeholder"
    },
    replayOrReferenceMessage: {
      type: Object,
      default: () => ({}),
      required: false
    },
    isMuted: {
      type: Boolean,
      default: true
    },
    muteText: {
      type: String,
      default: ""
    },
    enableInput: {
      type: Boolean,
      default: true
    },
    enableAt: {
      type: Boolean,
      default: true
    },
    enableTyping: {
      type: Boolean,
      default: true
    },
    isGroup: {
      type: Boolean,
      default: false
    }
  },
  emits: ["onTyping", "onFocus", "onAt"],
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const inputText = common_vendor.ref("");
    common_vendor.ref();
    const inputBlur = common_vendor.ref(true);
    const inputContentEmpty = common_vendor.ref(true);
    const allInsertedAtInfo = /* @__PURE__ */ new Map();
    const currentConversation = common_vendor.ref();
    const currentConversationID = common_vendor.ref("");
    const currentQuoteMessage = common_vendor.ref();
    common_vendor.onMounted(() => {
      common_vendor.Jt.watch(common_vendor.o.CONV, {
        currentConversation: onCurrentConversationUpdated
      });
      common_vendor.Jt.watch(common_vendor.o.CHAT, {
        quoteMessage: onQuoteMessageUpdated
      });
      common_vendor.index.$on("insert-emoji", (data) => {
        var _a;
        inputText.value += (_a = data == null ? void 0 : data.emoji) == null ? void 0 : _a.name;
      });
      common_vendor.index.$on("send-message-in-emoji-picker", () => {
        handleSendMessage();
      });
    });
    common_vendor.onUnmounted(() => {
      if (currentConversationID.value) {
        TUIKit_components_TUIChat_utils_conversationDraft.DraftManager.setStore(currentConversationID.value, inputText.value, inputText.value, currentQuoteMessage.value);
      }
      common_vendor.index.$off("insertEmoji");
      common_vendor.index.$off("send-message-in-emoji-picker");
      common_vendor.Jt.unwatch(common_vendor.o.CONV, {
        currentConversation: onCurrentConversationUpdated
      });
      common_vendor.Jt.unwatch(common_vendor.o.CHAT, {
        quoteMessage: onQuoteMessageUpdated
      });
      reset();
    });
    const handleSendMessage = () => {
      const messageList = getEditorContent();
      resetEditor();
      TUIKit_components_TUIChat_utils_sendMessage.sendMessages(messageList, currentConversation.value);
    };
    const insertAt = (atInfo) => {
      if (!(allInsertedAtInfo == null ? void 0 : allInsertedAtInfo.has(atInfo == null ? void 0 : atInfo.id))) {
        allInsertedAtInfo == null ? void 0 : allInsertedAtInfo.set(atInfo == null ? void 0 : atInfo.id, atInfo == null ? void 0 : atInfo.label);
      }
      inputText.value += atInfo == null ? void 0 : atInfo.label;
    };
    const getEditorContent = () => {
      let text = inputText.value;
      text = TUIKit_components_TUIChat_emojiConfig_index.transformTextWithEmojiNamesToKeys(text);
      const atUserList = [];
      allInsertedAtInfo == null ? void 0 : allInsertedAtInfo.forEach((value, key) => {
        if (text == null ? void 0 : text.includes("@" + value)) {
          atUserList.push(key);
        }
      });
      const payload = {
        text
      };
      if (atUserList == null ? void 0 : atUserList.length) {
        payload.atUserList = atUserList;
      }
      return [
        {
          type: "text",
          payload
        }
      ];
    };
    const resetEditor = () => {
      inputText.value = "";
      inputContentEmpty.value = true;
      allInsertedAtInfo == null ? void 0 : allInsertedAtInfo.clear();
    };
    const setEditorContent = (content) => {
      inputText.value = content;
    };
    const onBlur = () => {
      inputBlur.value = true;
    };
    const onFocus = (e) => {
      var _a;
      inputBlur.value = false;
      emits("onFocus", (_a = e == null ? void 0 : e.detail) == null ? void 0 : _a.height);
    };
    const isEditorContentEmpty = () => {
      var _a;
      inputContentEmpty.value = ((_a = inputText == null ? void 0 : inputText.value) == null ? void 0 : _a.length) ? false : true;
    };
    const onInput = (e) => {
      var _a, _b;
      const text = (_a = e == null ? void 0 : e.detail) == null ? void 0 : _a.value;
      isEditorContentEmpty();
      if (props.isGroup && (text.endsWith("@") || text.endsWith("@\n"))) {
        (_b = common_vendor.i) == null ? void 0 : _b.hideKeyboard();
        emits("onAt", true);
      }
    };
    common_vendor.watch(
      () => [inputContentEmpty.value, inputBlur.value],
      (newVal, oldVal) => {
        if (newVal !== oldVal) {
          emits("onTyping", inputContentEmpty.value, inputBlur.value);
        }
      },
      {
        immediate: true,
        deep: true
      }
    );
    function onCurrentConversationUpdated(conversation) {
      const prevConversationID = currentConversationID.value;
      currentConversation.value = conversation;
      currentConversationID.value = conversation == null ? void 0 : conversation.conversationID;
      if (prevConversationID !== currentConversationID.value) {
        if (prevConversationID) {
          TUIKit_components_TUIChat_utils_conversationDraft.DraftManager.setStore(
            prevConversationID,
            inputText.value,
            inputText.value,
            currentQuoteMessage.value
          );
        }
        resetEditor();
        if (currentConversationID.value) {
          TUIKit_components_TUIChat_utils_conversationDraft.DraftManager.getStore(currentConversationID.value, setEditorContent);
        }
      }
    }
    function onQuoteMessageUpdated(options) {
      currentQuoteMessage.value = options;
    }
    function reset() {
      inputBlur.value = true;
      currentConversation.value = null;
      currentConversationID.value = "";
      currentQuoteMessage.value = null;
      resetEditor();
    }
    __expose({
      insertAt,
      resetEditor,
      setEditorContent,
      getEditorContent
    });
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: props.isMuted
      }, props.isMuted ? {
        b: common_vendor.t(props.muteText)
      } : {}, {
        c: props.placeholder,
        d: common_vendor.o$1(handleSendMessage),
        e: common_vendor.o$1([($event) => common_vendor.isRef(inputText) ? inputText.value = $event.detail.value : null, onInput]),
        f: common_vendor.o$1(onBlur),
        g: common_vendor.o$1(onFocus),
        h: common_vendor.unref(inputText),
        i: !common_vendor.unref(TUIKit_utils_env.isPC) ? 1 : ""
      });
    };
  }
});
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-6acc9af1"]]);
wx.createComponent(Component);
//# sourceMappingURL=../../../../../.sourcemap/mp-weixin/TUIKit/components/TUIChat/message-input/message-input-editor.js.map
