import { defineComponent, ref, getCurrentInstance, onMounted, onUnmounted, watch, openBlock, createElementBlock, renderSlot, createElementVNode, toDisplayString, unref } from "vue";
import "../../../adapter-vue.mjs";
import { TUITranslateService } from "@tencentcloud/chat-uikit-engine";
import { isUniFrameWork } from "../../../utils/env.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    isFetching: { type: Boolean, default: false },
    isTerminateObserve: { type: Boolean, default: false }
  },
  emits: ["onExposed"],
  setup(__props, { emit: __emit }) {
    var _a;
    const emits = __emit;
    const props = __props;
    let observer = null;
    const selfDomRef = ref();
    const thisInstance = ((_a = getCurrentInstance()) == null ? void 0 : _a.proxy) || getCurrentInstance();
    onMounted(() => {
      if (props.isTerminateObserve) {
        return;
      }
      if (!isUniFrameWork) {
        observer = new IntersectionObserver(([entry]) => {
          if (entry.isIntersecting) {
            emits("onExposed");
          }
        }, {
          threshold: 1
        });
        if (selfDomRef.value) {
          observer.observe(selfDomRef.value);
        }
      } else {
        observer = uni.createIntersectionObserver(thisInstance).relativeToViewport();
        observer.observe(".fetch-more-block", () => {
          emits("onExposed");
        });
      }
    });
    onUnmounted(() => {
      if (observer) {
        observer.disconnect();
        observer = null;
      }
    });
    watch(() => props.isTerminateObserve, (isTerminateObserve) => {
      if (!observer) {
        return;
      }
      if (isTerminateObserve) {
        observer.disconnect();
      } else if (selfDomRef.value) {
        observer.disconnect();
        if (!isUniFrameWork) {
          observer.observe(selfDomRef.value);
        } else {
          observer.observe(".fetch-more-block", () => {
            emits("onExposed");
          });
        }
      }
    });
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", {
        ref_key: "selfDomRef",
        ref: selfDomRef,
        class: "fetch-more-block"
      }, [
        props.isFetching ? renderSlot(_ctx.$slots, "fetching", { key: 0 }, () => [
          createElementVNode("div", null, toDisplayString(unref(TUITranslateService).t("TUIChat.正在加载")), 1)
        ], true) : renderSlot(_ctx.$slots, "fetchEnd", { key: 1 }, () => [
          createElementVNode("div", null, toDisplayString(unref(TUITranslateService).t("TUIChat.加载结束")), 1)
        ], true)
      ], 512);
    };
  }
});
export {
  _sfc_main as default
};
