import { jsx as e, Fragment as D, jsxs as g } from "react/jsx-runtime";
import { useRef as f, useCallback as a, useEffect as w } from "react";
import { Virtuoso as I } from "../../shop-minis-react/node_modules/.pnpm/react-virtuoso@4.14.0_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-virtuoso/dist/index.js";
import { RefreshIndicator as M } from "../../internal/components/refresh-indicator.js";
import { usePullToRefresh as U } from "../../internal/usePullToRefresh.js";
import { findVirtuosoScrollableElement as V } from "../../internal/utils/virtuoso-dom.js";
import { cn as x } from "../../lib/utils.js";
/* empty css                         */
import { Pagination as k } from "./pagination.js";
const H = 200, q = 100;
function W({
  items: L,
  height: N,
  renderItem: p,
  className: _,
  showScrollbar: F = !1,
  header: u,
  fetchMore: r,
  loadingComponent: E,
  isFetchingMore: h,
  onRefresh: n,
  refreshing: m,
  enablePullToRefresh: i = !0,
  ...R
}) {
  const l = f(null), y = f(null), s = f(null), { state: o, bindToElement: v } = U({
    onRefresh: n,
    threshold: H,
    enabled: i && !!n
  }), d = a(() => {
    if (l.current) return;
    const t = Promise.resolve(r?.()).finally(() => {
      l.current === t && (l.current = null);
    });
    l.current = t;
  }, [r]), A = a(
    (t, c) => /* @__PURE__ */ e(D, { children: p(c, t) }),
    [p]
  ), B = a(() => r ? /* @__PURE__ */ e(
    k,
    {
      fetchMore: d,
      loadingComponent: E,
      isFetchingMore: h
    }
  ) : null, [d, r, E, h]), S = x(F ? void 0 : "no-scrollbars", _);
  w(() => {
    if (s.current && i && n) {
      let t;
      const j = setTimeout(() => {
        if (!s.current) return;
        const b = V(
          s.current
        );
        t = v(b);
      }, q);
      return () => {
        clearTimeout(j), t && t();
      };
    }
  }, [v, i, n]);
  const T = a(() => {
    const t = m ? Math.max(o.pullDistance, 140) : o.pullDistance, c = Math.min(
      Math.max(t, 0),
      140
    );
    return /* @__PURE__ */ g(D, { children: [
      i && n && /* @__PURE__ */ e(
        "div",
        {
          className: "flex items-center justify-center",
          style: {
            height: c,
            overflow: "hidden"
          },
          children: /* @__PURE__ */ e(
            M,
            {
              pullDistance: o.pullDistance,
              threshold: H,
              isRefreshing: m ?? !1,
              canRefresh: o.canRefresh,
              className: "relative top-0 inset-x-auto"
            }
          )
        }
      ),
      u && /* @__PURE__ */ e("div", { children: u })
    ] });
  }, [u, i, n, o, m]);
  return /* @__PURE__ */ e(
    "div",
    {
      ref: s,
      className: x("relative transition-all duration-200", S),
      style: {
        height: N
      },
      children: /* @__PURE__ */ e(
        I,
        {
          ref: y,
          className: "h-full w-full",
          data: L,
          itemContent: A,
          components: {
            Header: T,
            Footer: B
          },
          endReached: r ? d : void 0,
          ...R
        }
      )
    }
  );
}
export {
  W as List
};
//# sourceMappingURL=list.js.map
