import { jsx as p } from "react/jsx-runtime";
import { useState as z, useRef as H, useCallback as m, useEffect as B } from "react";
import F from "./VirtualListItemWrapper.js";
import $ from "../../hooks/useWindowResize.js";
import x from "./useVirtualListResizeObserver.js";
const J = (S) => {
  const {
    items: c,
    renderItem: y,
    overscanCount: h = 3,
    defaultHeight: l = 50,
    scrollContainerRef: d,
    containerClassName: L = "",
    listItemWrapperClassName: T = "",
    className: V = "",
    ...k
  } = S, [r, v] = z({
    itemHeights: {},
    visibleRange: { start: 0, end: 10 },
    scrollTop: 0,
    viewportHeight: 0
  }), b = H(null), R = H(null), C = H(-1), g = m(
    () => d?.current || b.current,
    [d]
  ), n = m(
    (e) => !Number.isNaN(e) && e > 0 ? e : l,
    [l]
  ), M = m(
    (e) => {
      let t = 0;
      for (let s = 0; s < e; s++) {
        const i = r.itemHeights[s] ?? l;
        t += n(i);
      }
      return t;
    },
    [r.itemHeights, n, l]
  ), P = m(() => {
    const e = Object.values(r.itemHeights).reduce((a, o) => a + n(o), 0), t = Object.keys(r.itemHeights).length, s = t > 0 ? n(e / t) : l, i = c.length - t;
    return n(e + i * s);
  }, [c.length, r.itemHeights, n, l]), N = m(() => {
    const e = g();
    if (!e)
      return r.visibleRange;
    const t = e.clientHeight, s = e.scrollTop;
    let i = 0, a = 0;
    for (; i < c.length; ) {
      const u = n(r.itemHeights[i] ?? l);
      if (a + u > s - h * u)
        break;
      a += u, i++;
    }
    let o = i;
    for (; o < c.length; ) {
      const u = n(r.itemHeights[o] ?? l);
      if (a > s + t + h * u)
        break;
      a += u, o++;
    }
    return {
      start: Math.max(0, i - h),
      end: Math.min(c.length, o + h)
    };
  }, [
    c.length,
    h,
    r.itemHeights,
    r.visibleRange,
    n,
    l,
    g
  ]), W = m(
    (e, t) => {
      const s = t.getBoundingClientRect(), i = window.getComputedStyle(t), a = n(
        s.height + Number.parseFloat(i.marginTop || "0") + Number.parseFloat(i.marginBottom || "0")
      );
      v((o) => o.itemHeights[e] === a ? o : {
        ...o,
        itemHeights: { ...o.itemHeights, [e]: a }
      }), C.current = Math.max(C.current, e);
    },
    [n]
  );
  x(R, W, [r.visibleRange]);
  const f = m(() => {
    const e = g();
    if (!e)
      return;
    const t = N();
    v((s) => ({
      ...s,
      visibleRange: t,
      scrollTop: e.scrollTop,
      viewportHeight: e.clientHeight
    }));
  }, [N, g]);
  B(() => {
    const e = g();
    if (e)
      return e.addEventListener("scroll", f, { passive: !0 }), () => e.removeEventListener("scroll", f);
  }, [f, g]), $(f);
  const j = (e, t) => {
    const s = y(e, t), i = M(t);
    return /* @__PURE__ */ p(
      F,
      {
        "data-index": t,
        translateY: i,
        className: T,
        children: s
      },
      t
    );
  }, { start: w, end: E } = r.visibleRange, I = P(), O = {
    height: d ? I : "100%",
    position: "relative",
    overflow: d ? "visible" : "auto"
  };
  return /* @__PURE__ */ p(
    "div",
    {
      ...k,
      ref: b,
      className: `VirtualList overflow-y-auto ${V}`,
      style: O,
      children: /* @__PURE__ */ p(
        "div",
        {
          className: "position-relative",
          style: {
            height: d ? "auto" : I
          },
          children: /* @__PURE__ */ p("div", { ref: R, className: `VirtualListItemContainer ${L}`, children: c.slice(w, E).map((e, t) => j(e, w + t)) })
        }
      )
    }
  );
};
export {
  J as VirtualList,
  J as default
};
//# sourceMappingURL=VirtualList.js.map
