import { j as h } from "../../_virtual/jsx-runtime.js";
import { useRef as v, useMemo as E, useCallback as w, useEffect as R, createElement as x, Fragment as L } from "react";
import { useDebouncedFn as b } from "../../hooks/useDebounce.js";
function y({
  element: s,
  loadMore: f,
  children: u,
  hasMore: i,
  threshold: o,
  axis: r,
  loader: d,
  ...m
}) {
  const t = v(null), p = b(f, 300), n = E(
    () => typeof o == "number" ? o : o[r],
    [o, r]
  ), a = w(
    (e) => (r === "x" ? (e.scrollLeft + n) / (e.scrollWidth - e.clientWidth) : (e.scrollTop + n) / (e.scrollHeight - e.clientHeight)) >= 1,
    [r, n]
  );
  function c(e) {
    i && a(e.target) && p();
  }
  if (R(() => {
    var l;
    const e = (l = t.current) == null ? void 0 : l.parentNode;
    return e == null || e.addEventListener("scroll", c), () => {
      e == null || e.removeEventListener("scroll", c);
    };
  }, [t.current]), !["x", "y"].includes(r))
    throw new Error("Invalid axis provided.");
  if (n < 0)
    throw new RangeError("Threshold is outside of the expected range");
  return x(
    s,
    { ...m, ref: t, onScroll: c },
    [
      u,
      i && /* @__PURE__ */ h.jsx(L, { children: d }, "infinite-scroll-loader-wrapper")
    ]
  );
}
export {
  y as InfiniteScroll
};
