"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const index_esm = require("../../../@vant/use/dist/index.esm.js");
const useExpose = require("../composables/use-expose.js");
const useTabStatus = require("../composables/use-tab-status.js");
const index = require("../loading/index.js");
const props = require("../utils/props.js");
const create = require("../utils/create.js");
const dom = require("../utils/dom.js");
const [name, bem, t] = create.createNamespace("list");
const listProps = {
  error: Boolean,
  offset: props.makeNumericProp(300),
  loading: Boolean,
  disabled: Boolean,
  finished: Boolean,
  scroller: Object,
  errorText: String,
  direction: props.makeStringProp("down"),
  loadingText: {
    type: String,
    default: ""
  },
  finishedText: String,
  immediateCheck: props.truthProp
};
var stdin_default = Vue.defineComponent({
  name,
  props: listProps,
  emits: ["load", "update:error", "update:loading"],
  setup(props2, {
    emit,
    slots
  }) {
    const loading = Vue.ref(props2.loading);
    const root = Vue.ref();
    const placeholder = Vue.ref();
    const tabStatus = useTabStatus.useAllTabStatus();
    const scrollParent = index_esm.useScrollParent(root);
    const scroller = Vue.computed(() => props2.scroller || scrollParent.value);
    const check = () => {
      Vue.nextTick(() => {
        if (loading.value || props2.finished || props2.disabled || props2.error || // skip check when inside an inactive tab
        (tabStatus == null ? void 0 : tabStatus.value) === false) {
          return;
        }
        const {
          direction
        } = props2;
        const offset = +props2.offset;
        const scrollParentRect = index_esm.useRect(scroller);
        if (!scrollParentRect.height || dom.isHidden(root)) {
          return;
        }
        let isReachEdge = false;
        const placeholderRect = index_esm.useRect(placeholder);
        if (direction === "up") {
          isReachEdge = scrollParentRect.top - placeholderRect.top <= offset;
        } else {
          isReachEdge = placeholderRect.bottom - scrollParentRect.bottom <= offset;
        }
        if (isReachEdge) {
          loading.value = true;
          emit("update:loading", true);
          emit("load");
        }
      });
    };
    const renderFinishedText = () => {
      if (props2.finished) {
        const text = slots.finished ? slots.finished() : props2.finishedText;
        if (text) {
          return Vue.createVNode("div", {
            "class": bem("finished-text")
          }, [text]);
        }
      }
    };
    const clickErrorText = () => {
      emit("update:error", false);
      check();
    };
    const renderErrorText = () => {
      if (props2.error) {
        const text = slots.error ? slots.error() : props2.errorText;
        if (text) {
          return Vue.createVNode("div", {
            "role": "button",
            "class": bem("error-text"),
            "tabindex": 0,
            "onClick": clickErrorText
          }, [text]);
        }
      }
    };
    const renderLoading = () => {
      if (loading.value && !props2.finished && !props2.disabled) {
        return Vue.createVNode("div", {
          "class": bem("loading")
        }, [slots.loading ? slots.loading() : props2.loadingText != null && Vue.createVNode(index.Loading, {
          "class": bem("loading-icon")
        }, {
          default: () => [props2.loadingText || t("loading")]
        })]);
      }
    };
    Vue.watch(() => [props2.loading, props2.finished, props2.error], check);
    if (tabStatus) {
      Vue.watch(tabStatus, (tabActive) => {
        if (tabActive) {
          check();
        }
      });
    }
    Vue.onUpdated(() => {
      loading.value = props2.loading;
    });
    Vue.onMounted(() => {
      if (props2.immediateCheck) {
        check();
      }
    });
    useExpose.useExpose({
      check
    });
    index_esm.useEventListener("scroll", check, {
      target: scroller,
      passive: true
    });
    return () => {
      var _a;
      const Content = (_a = slots.default) == null ? void 0 : _a.call(slots);
      const Placeholder = Vue.createVNode("div", {
        "ref": placeholder,
        "class": bem("placeholder")
      }, null);
      return Vue.createVNode("div", {
        "ref": root,
        "role": "feed",
        "class": bem(),
        "aria-busy": loading.value
      }, [props2.direction === "down" ? Content : Placeholder, renderLoading(), renderFinishedText(), renderErrorText(), props2.direction === "up" ? Content : Placeholder]);
    };
  }
});
exports.default = stdin_default;
exports.listProps = listProps;
