import { defineComponent, ref, computed, watch, onMounted, onUnmounted, unref, openBlock, createElementBlock, createVNode, createElementVNode, toDisplayString, createCommentVNode } from "vue";
import "../../../../adapter-vue.mjs";
import { TUITranslateService, TUIStore, StoreName } from "@tencentcloud/chat-uikit-engine";
import Icon from "../../../common/Icon.vue.mjs";
import doubleArrowIcon from "../../../../assets/icon/double-arrow.svg.mjs";
import { getBoundingClientRect } from "@tencentcloud/universal-api";
import { JSONToObject } from "../../../../utils/type-check.mjs";
const _hoisted_1 = { class: "scroll-button-text" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  emits: ["scrollToLatestMessage"],
  setup(__props, { expose: __expose, emit: __emit }) {
    const emits = __emit;
    const messageList = ref([]);
    const currentConversationID = ref("");
    const currentLastMessageTime = ref(0);
    const newMessageCount = ref(0);
    const isScrollOverOneScreen = ref(false);
    const isExistLastMessage = ref(false);
    const isScrollButtonVisible = ref(false);
    const scrollButtonContent = computed(
      () => newMessageCount.value ? `${newMessageCount.value}${TUITranslateService.t("TUIChat.条新消息")}` : TUITranslateService.t("TUIChat.回到最新位置")
    );
    watch(
      () => [isScrollOverOneScreen.value, isExistLastMessage.value],
      () => {
        isScrollButtonVisible.value = isScrollOverOneScreen.value || isExistLastMessage.value;
        if (!isScrollButtonVisible.value) {
          resetNewMessageCount();
        }
      },
      { immediate: true }
    );
    onMounted(() => {
      TUIStore.watch(StoreName.CHAT, {
        messageList: onMessageListUpdated,
        newMessageList: onNewMessageListUpdated
      });
      TUIStore.watch(StoreName.CONV, {
        currentConversation: onCurrentConversationUpdated
      });
    });
    onUnmounted(() => {
      TUIStore.unwatch(StoreName.CHAT, {
        messageList: onMessageListUpdated,
        newMessageList: onNewMessageListUpdated
      });
      TUIStore.unwatch(StoreName.CONV, {
        currentConversation: onCurrentConversationUpdated
      });
    });
    function isTypingMessage(message) {
      var _a, _b;
      return ((_b = JSONToObject((_a = message.payload) == null ? void 0 : _a.data)) == null ? void 0 : _b.businessID) === "user_typing_status";
    }
    function onMessageListUpdated(newMessageList) {
      var _a, _b;
      messageList.value = newMessageList || [];
      const lastMessage = (_b = messageList.value) == null ? void 0 : _b[((_a = messageList.value) == null ? void 0 : _a.length) - 1];
      isExistLastMessage.value = !!(lastMessage && (lastMessage == null ? void 0 : lastMessage.time) < (currentLastMessageTime == null ? void 0 : currentLastMessageTime.value));
    }
    function onNewMessageListUpdated(newMessageList) {
      if (Array.isArray(newMessageList) && isScrollButtonVisible.value) {
        newMessageList.forEach((message) => {
          if (message && message.conversationID === currentConversationID.value && !message.isDeleted && !message.isRevoked && !isTypingMessage(message)) {
            newMessageCount.value += 1;
          }
        });
      }
    }
    function onCurrentConversationUpdated(conversation) {
      var _a;
      if ((conversation == null ? void 0 : conversation.conversationID) !== currentConversationID.value) {
        resetNewMessageCount();
      }
      currentConversationID.value = (conversation == null ? void 0 : conversation.conversationID) || "";
      currentLastMessageTime.value = ((_a = conversation == null ? void 0 : conversation.lastMessage) == null ? void 0 : _a.lastTime) || 0;
    }
    async function judgeScrollOverOneScreen(e) {
      var _a, _b, _c, _d, _e;
      if (e.target) {
        try {
          const { height } = await getBoundingClientRect(`#${(_a = e.target) == null ? void 0 : _a.id}`, "messageList") || {};
          const scrollHeight = ((_b = e.target) == null ? void 0 : _b.scrollHeight) || ((_c = e.detail) == null ? void 0 : _c.scrollHeight);
          const scrollTop = ((_d = e.target) == null ? void 0 : _d.scrollTop) || ((_e = e.detail) == null ? void 0 : _e.scrollTop) || 0;
          if (scrollHeight - scrollTop > 2 * height) {
            isScrollOverOneScreen.value = true;
            return;
          }
          isScrollOverOneScreen.value = false;
        } catch (error) {
          isScrollOverOneScreen.value = false;
        }
      }
    }
    function resetMessageSource() {
      if (TUIStore.getData(StoreName.CHAT, "messageSource") !== void 0) {
        TUIStore.update(StoreName.CHAT, "messageSource", void 0);
      }
    }
    function resetNewMessageCount() {
      newMessageCount.value = 0;
    }
    function scrollToMessageListBottom() {
      resetMessageSource();
      resetNewMessageCount();
      emits("scrollToLatestMessage");
    }
    __expose({
      judgeScrollOverOneScreen,
      isScrollButtonVisible
    });
    return (_ctx, _cache) => {
      return unref(isScrollButtonVisible) ? (openBlock(), createElementBlock("div", {
        key: 0,
        class: "scroll-button",
        onClick: scrollToMessageListBottom
      }, [
        createVNode(Icon, {
          width: "10px",
          height: "10px",
          file: unref(doubleArrowIcon)
        }, null, 8, ["file"]),
        createElementVNode("div", _hoisted_1, toDisplayString(unref(scrollButtonContent)), 1)
      ])) : createCommentVNode("", true);
    };
  }
});
export {
  _sfc_main as default
};
