import { defineComponent, ref, watch, nextTick, openBlock, createElementBlock, normalizeStyle, unref, normalizeClass, toDisplayString, createCommentVNode, createElementVNode } from "vue";
import "../../../../../adapter-vue.mjs";
import { TUITranslateService } from "@tencentcloud/chat-uikit-engine";
import { getBoundingClientRectSync } from "@tencentcloud/universal-api";
import { convertor } from "../../../utils/convertVoiceToText.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "convert-content",
  props: {
    message: { default: () => ({}) },
    contentVisible: { type: Boolean },
    convertWrapperRef: {},
    isSingleConvert: { type: Boolean, default: false }
  },
  emits: ["toggleErrorStatus"],
  setup(__props, { emit: __emit }) {
    const emits = __emit;
    const props = __props;
    const convertFinished = ref(false);
    const convertText = ref("");
    const calculateHeight = ref(0);
    const calculateWidth = ref(0);
    const convertLoadingRef = ref();
    const convertContentRef = ref();
    watch(() => props.contentVisible, (newVal) => {
      if (newVal) {
        convertor.get(props.message).then((text) => {
          convertFinished.value = true;
          convertText.value = text;
          nextTick(() => {
            const { height: originHeight, width: originWidth } = getBoundingClientRectSync(convertLoadingRef.value);
            const { height, width } = getBoundingClientRectSync(convertContentRef.value);
            calculateHeight.value = originHeight;
            calculateWidth.value = originWidth;
            requestAnimationFrame(() => {
              calculateHeight.value = height;
              calculateWidth.value = width;
              if (props.isSingleConvert) {
                nextTick(() => {
                  const { bottom } = getBoundingClientRectSync(props.convertWrapperRef);
                  const { bottom: bottomWindow } = getBoundingClientRectSync("#messageScrollList");
                  if (bottom > bottomWindow) {
                    const timer = setTimeout(() => {
                      props.convertWrapperRef.scrollIntoView({ block: "end", behavior: "smooth" });
                      clearTimeout(timer);
                    }, 150);
                  }
                });
              }
            });
          });
        }).catch((err) => {
          convertFinished.value = true;
          emits("toggleErrorStatus", true);
          const { height: originHeight } = getBoundingClientRectSync(convertLoadingRef.value);
          calculateHeight.value = originHeight;
          convertText.value = err.message;
        });
      }
    }, {
      immediate: true
    });
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", {
        class: "message-convert-container",
        style: normalizeStyle({
          height: unref(calculateHeight) > 0 ? `${unref(calculateHeight)}px` : "auto",
          width: unref(calculateWidth) > 0 ? `${unref(calculateWidth)}px` : "auto"
        })
      }, [
        unref(convertFinished) ? (openBlock(), createElementBlock("div", {
          key: 0,
          ref_key: "convertContentRef",
          ref: convertContentRef,
          class: normalizeClass({
            "convert-content": true,
            "occur": unref(calculateHeight) > 0
          })
        }, toDisplayString(unref(convertText)), 3)) : createCommentVNode("", true),
        createElementVNode("div", {
          ref_key: "convertLoadingRef",
          ref: convertLoadingRef,
          class: normalizeClass({
            "loading": true,
            "loading-end": unref(convertFinished)
          })
        }, toDisplayString(unref(TUITranslateService).t("TUIChat.转换中")) + "... ", 3)
      ], 4);
    };
  }
});
export {
  _sfc_main as default
};
