import { defineComponent, ref, watch, nextTick, openBlock, createElementBlock, normalizeStyle, unref, normalizeClass, Fragment, renderList, toDisplayString, createTextVNode, createCommentVNode, createElementVNode } from "vue";
import "../../../../../adapter-vue.mjs";
import { TUITranslateService } from "@tencentcloud/chat-uikit-engine";
import { getBoundingClientRectSync } from "@tencentcloud/universal-api";
import { translator } from "../../../utils/translation.mjs";
const _hoisted_1 = ["src"];
const _hoisted_2 = {
  key: 1,
  class: "text-plain"
};
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "translation-content",
  props: {
    message: { default: () => ({}) },
    translationContentVisible: { type: Boolean },
    isSingleTranslation: { type: Boolean },
    translationWrapperRef: {}
  },
  emits: ["toggleErrorStatus"],
  setup(__props, { emit: __emit }) {
    const emits = __emit;
    const props = __props;
    const translationFinished = ref(false);
    const translationErrorText = ref("");
    const translationTextList = ref([]);
    const calculateHeight = ref(0);
    const calculateWidth = ref(0);
    const translationLoadingRef = ref();
    const translationContentRef = ref();
    watch(() => props.translationContentVisible, (newVal) => {
      if (newVal) {
        translator.get(props.message).then((result) => {
          translationFinished.value = true;
          translationTextList.value = result;
          nextTick(() => {
            const { height: originHeight, width: originWidth } = getBoundingClientRectSync(translationLoadingRef.value);
            const { height, width } = getBoundingClientRectSync(translationContentRef.value);
            calculateHeight.value = originHeight;
            calculateWidth.value = originWidth;
            requestAnimationFrame(() => {
              calculateHeight.value = height;
              calculateWidth.value = width;
              if (props.isSingleTranslation) {
                nextTick(() => {
                  const { bottom } = getBoundingClientRectSync(props.translationWrapperRef);
                  const { bottom: bottomWindow } = getBoundingClientRectSync("#messageScrollList");
                  if (bottom > bottomWindow) {
                    const timer = setTimeout(() => {
                      props.translationWrapperRef.scrollIntoView({ block: "end", behavior: "smooth" });
                      clearTimeout(timer);
                    }, 150);
                  }
                });
              }
            });
          });
        }).catch((err) => {
          translationFinished.value = true;
          const { height: originHeight } = getBoundingClientRectSync(translationLoadingRef.value);
          calculateHeight.value = originHeight;
          translationTextList.value = [];
          emits("toggleErrorStatus", true);
          translationErrorText.value = err.message;
        });
      }
    }, { immediate: true });
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", {
        class: "message-translation-container",
        style: normalizeStyle({
          height: unref(calculateHeight) > 0 ? `${unref(calculateHeight)}px` : "auto",
          width: unref(calculateWidth) > 0 ? `${unref(calculateWidth)}px` : "auto"
        })
      }, [
        unref(translationFinished) ? (openBlock(), createElementBlock("div", {
          key: 0,
          ref_key: "translationContentRef",
          ref: translationContentRef,
          class: normalizeClass({
            "translation-content": true,
            "occur": unref(calculateHeight) > 0
          })
        }, [
          unref(translationTextList).length > 0 ? (openBlock(true), createElementBlock(Fragment, { key: 0 }, renderList(unref(translationTextList), (text, index) => {
            return openBlock(), createElementBlock("span", { key: index }, [
              text.type === "face" ? (openBlock(), createElementBlock("img", {
                key: 0,
                class: "text-face",
                src: text.value
              }, null, 8, _hoisted_1)) : (openBlock(), createElementBlock("span", _hoisted_2, toDisplayString(text.value), 1))
            ]);
          }), 128)) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [
            createTextVNode(toDisplayString(unref(translationErrorText)), 1)
          ], 64))
        ], 2)) : createCommentVNode("", true),
        createElementVNode("div", {
          ref_key: "translationLoadingRef",
          ref: translationLoadingRef,
          class: normalizeClass({
            "loading": true,
            "loading-end": unref(translationFinished)
          })
        }, toDisplayString(unref(TUITranslateService).t("TUIChat.翻译中")) + "... ", 3)
      ], 4);
    };
  }
});
export {
  _sfc_main as default
};
