import { jsx as M } from "react/jsx-runtime";
import * as w from "react";
import { useRef as _, useState as F, useEffect as T, useMemo as k } from "react";
import { flushSync as D } from "react-dom";
function x(r, l, e) {
  let t = e.initialDeps ?? [], s, n = !0;
  function o() {
    var i, a, u;
    let c;
    e.key && ((i = e.debug) != null && i.call(e)) && (c = Date.now());
    const p = r();
    if (!(p.length !== t.length || p.some((E, m) => t[m] !== E)))
      return s;
    t = p;
    let v;
    if (e.key && ((a = e.debug) != null && a.call(e)) && (v = Date.now()), s = l(...p), e.key && ((u = e.debug) != null && u.call(e))) {
      const E = Math.round((Date.now() - c) * 100) / 100, m = Math.round((Date.now() - v) * 100) / 100, f = m / 16, g = (d, b) => {
        for (d = String(d); d.length < b; )
          d = " " + d;
        return d;
      };
      console.info(
        `%c⏱ ${g(m, 5)} /${g(E, 5)} ms`,
        `
            font-size: .6rem;
            font-weight: bold;
            color: hsl(${Math.max(
          0,
          Math.min(120 - 120 * f, 120)
        )}deg 100% 31%);`,
        e?.key
      );
    }
    return e?.onChange && !(n && e.skipInitialOnChange) && e.onChange(s), n = !1, s;
  }
  return o.updateDeps = (i) => {
    t = i;
  }, o;
}
function I(r, l) {
  if (r === void 0)
    throw new Error("Unexpected undefined");
  return r;
}
const N = (r, l) => Math.abs(r - l) < 1.01, W = (r, l, e) => {
  let t;
  return function(...s) {
    r.clearTimeout(t), t = r.setTimeout(() => l.apply(this, s), e);
  };
}, z = (r) => {
  const { offsetWidth: l, offsetHeight: e } = r;
  return { width: l, height: e };
}, L = (r) => r, V = (r) => {
  const l = Math.max(r.startIndex - r.overscan, 0), e = Math.min(r.endIndex + r.overscan, r.count - 1), t = [];
  for (let s = l; s <= e; s++)
    t.push(s);
  return t;
}, j = (r, l) => {
  const e = r.scrollElement;
  if (!e)
    return;
  const t = r.targetWindow;
  if (!t)
    return;
  const s = (o) => {
    const { width: i, height: a } = o;
    l({ width: Math.round(i), height: Math.round(a) });
  };
  if (s(z(e)), !t.ResizeObserver)
    return () => {
    };
  const n = new t.ResizeObserver((o) => {
    const i = () => {
      const a = o[0];
      if (a?.borderBoxSize) {
        const u = a.borderBoxSize[0];
        if (u) {
          s({ width: u.inlineSize, height: u.blockSize });
          return;
        }
      }
      s(z(e));
    };
    r.options.useAnimationFrameWithResizeObserver ? requestAnimationFrame(i) : i();
  });
  return n.observe(e, { box: "border-box" }), () => {
    n.unobserve(e);
  };
}, C = {
  passive: !0
}, y = typeof window > "u" ? !0 : "onscrollend" in window, B = (r, l) => {
  const e = r.scrollElement;
  if (!e)
    return;
  const t = r.targetWindow;
  if (!t)
    return;
  let s = 0;
  const n = r.options.useScrollendEvent && y ? () => {
  } : W(
    t,
    () => {
      l(s, !1);
    },
    r.options.isScrollingResetDelay
  ), o = (c) => () => {
    const { horizontal: p, isRtl: h } = r.options;
    s = p ? e.scrollLeft * (h && -1 || 1) : e.scrollTop, n(), l(s, c);
  }, i = o(!0), a = o(!1);
  e.addEventListener("scroll", i, C);
  const u = r.options.useScrollendEvent && y;
  return u && e.addEventListener("scrollend", a, C), () => {
    e.removeEventListener("scroll", i), u && e.removeEventListener("scrollend", a);
  };
}, P = (r, l, e) => {
  if (l?.borderBoxSize) {
    const t = l.borderBoxSize[0];
    if (t)
      return Math.round(
        t[e.options.horizontal ? "inlineSize" : "blockSize"]
      );
  }
  return r[e.options.horizontal ? "offsetWidth" : "offsetHeight"];
}, H = (r, {
  adjustments: l = 0,
  behavior: e
}, t) => {
  var s, n;
  const o = r + l;
  (n = (s = t.scrollElement) == null ? void 0 : s.scrollTo) == null || n.call(s, {
    [t.options.horizontal ? "left" : "top"]: o,
    behavior: e
  });
};
class $ {
  constructor(l) {
    this.unsubs = [], this.scrollElement = null, this.targetWindow = null, this.isScrolling = !1, this.scrollState = null, this.measurementsCache = [], this.itemSizeCache = /* @__PURE__ */ new Map(), this.laneAssignments = /* @__PURE__ */ new Map(), this.pendingMeasuredCacheIndexes = [], this.prevLanes = void 0, this.lanesChangedFlag = !1, this.lanesSettling = !1, this.scrollRect = null, this.scrollOffset = null, this.scrollDirection = null, this.scrollAdjustments = 0, this.elementsCache = /* @__PURE__ */ new Map(), this.now = () => {
      var e, t, s;
      return ((s = (t = (e = this.targetWindow) == null ? void 0 : e.performance) == null ? void 0 : t.now) == null ? void 0 : s.call(t)) ?? Date.now();
    }, this.observer = /* @__PURE__ */ (() => {
      let e = null;
      const t = () => e || (!this.targetWindow || !this.targetWindow.ResizeObserver ? null : e = new this.targetWindow.ResizeObserver((s) => {
        s.forEach((n) => {
          const o = () => {
            const i = n.target, a = this.indexFromElement(i);
            if (!i.isConnected) {
              this.observer.unobserve(i);
              return;
            }
            this.shouldMeasureDuringScroll(a) && this.resizeItem(
              a,
              this.options.measureElement(i, n, this)
            );
          };
          this.options.useAnimationFrameWithResizeObserver ? requestAnimationFrame(o) : o();
        });
      }));
      return {
        disconnect: () => {
          var s;
          (s = t()) == null || s.disconnect(), e = null;
        },
        observe: (s) => {
          var n;
          return (n = t()) == null ? void 0 : n.observe(s, { box: "border-box" });
        },
        unobserve: (s) => {
          var n;
          return (n = t()) == null ? void 0 : n.unobserve(s);
        }
      };
    })(), this.range = null, this.setOptions = (e) => {
      Object.entries(e).forEach(([t, s]) => {
        typeof s > "u" && delete e[t];
      }), this.options = {
        debug: !1,
        initialOffset: 0,
        overscan: 1,
        paddingStart: 0,
        paddingEnd: 0,
        scrollPaddingStart: 0,
        scrollPaddingEnd: 0,
        horizontal: !1,
        getItemKey: L,
        rangeExtractor: V,
        onChange: () => {
        },
        measureElement: P,
        initialRect: { width: 0, height: 0 },
        scrollMargin: 0,
        gap: 0,
        indexAttribute: "data-index",
        initialMeasurementsCache: [],
        lanes: 1,
        isScrollingResetDelay: 150,
        enabled: !0,
        isRtl: !1,
        useScrollendEvent: !1,
        useAnimationFrameWithResizeObserver: !1,
        ...e
      };
    }, this.notify = (e) => {
      var t, s;
      (s = (t = this.options).onChange) == null || s.call(t, this, e);
    }, this.maybeNotify = x(
      () => (this.calculateRange(), [
        this.isScrolling,
        this.range ? this.range.startIndex : null,
        this.range ? this.range.endIndex : null
      ]),
      (e) => {
        this.notify(e);
      },
      {
        key: process.env.NODE_ENV !== "production" && "maybeNotify",
        debug: () => this.options.debug,
        initialDeps: [
          this.isScrolling,
          this.range ? this.range.startIndex : null,
          this.range ? this.range.endIndex : null
        ]
      }
    ), this.cleanup = () => {
      this.unsubs.filter(Boolean).forEach((e) => e()), this.unsubs = [], this.observer.disconnect(), this.rafId != null && this.targetWindow && (this.targetWindow.cancelAnimationFrame(this.rafId), this.rafId = null), this.scrollState = null, this.scrollElement = null, this.targetWindow = null;
    }, this._didMount = () => () => {
      this.cleanup();
    }, this._willUpdate = () => {
      var e;
      const t = this.options.enabled ? this.options.getScrollElement() : null;
      if (this.scrollElement !== t) {
        if (this.cleanup(), !t) {
          this.maybeNotify();
          return;
        }
        this.scrollElement = t, this.scrollElement && "ownerDocument" in this.scrollElement ? this.targetWindow = this.scrollElement.ownerDocument.defaultView : this.targetWindow = ((e = this.scrollElement) == null ? void 0 : e.window) ?? null, this.elementsCache.forEach((s) => {
          this.observer.observe(s);
        }), this.unsubs.push(
          this.options.observeElementRect(this, (s) => {
            this.scrollRect = s, this.maybeNotify();
          })
        ), this.unsubs.push(
          this.options.observeElementOffset(this, (s, n) => {
            this.scrollAdjustments = 0, this.scrollDirection = n ? this.getScrollOffset() < s ? "forward" : "backward" : null, this.scrollOffset = s, this.isScrolling = n, this.scrollState && this.scheduleScrollReconcile(), this.maybeNotify();
          })
        ), this._scrollToOffset(this.getScrollOffset(), {
          adjustments: void 0,
          behavior: void 0
        });
      }
    }, this.rafId = null, this.getSize = () => this.options.enabled ? (this.scrollRect = this.scrollRect ?? this.options.initialRect, this.scrollRect[this.options.horizontal ? "width" : "height"]) : (this.scrollRect = null, 0), this.getScrollOffset = () => this.options.enabled ? (this.scrollOffset = this.scrollOffset ?? (typeof this.options.initialOffset == "function" ? this.options.initialOffset() : this.options.initialOffset), this.scrollOffset) : (this.scrollOffset = null, 0), this.getFurthestMeasurement = (e, t) => {
      const s = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map();
      for (let o = t - 1; o >= 0; o--) {
        const i = e[o];
        if (s.has(i.lane))
          continue;
        const a = n.get(
          i.lane
        );
        if (a == null || i.end > a.end ? n.set(i.lane, i) : i.end < a.end && s.set(i.lane, !0), s.size === this.options.lanes)
          break;
      }
      return n.size === this.options.lanes ? Array.from(n.values()).sort((o, i) => o.end === i.end ? o.index - i.index : o.end - i.end)[0] : void 0;
    }, this.getMeasurementOptions = x(
      () => [
        this.options.count,
        this.options.paddingStart,
        this.options.scrollMargin,
        this.options.getItemKey,
        this.options.enabled,
        this.options.lanes
      ],
      (e, t, s, n, o, i) => (this.prevLanes !== void 0 && this.prevLanes !== i && (this.lanesChangedFlag = !0), this.prevLanes = i, this.pendingMeasuredCacheIndexes = [], {
        count: e,
        paddingStart: t,
        scrollMargin: s,
        getItemKey: n,
        enabled: o,
        lanes: i
      }),
      {
        key: !1
      }
    ), this.getMeasurements = x(
      () => [this.getMeasurementOptions(), this.itemSizeCache],
      ({ count: e, paddingStart: t, scrollMargin: s, getItemKey: n, enabled: o, lanes: i }, a) => {
        if (!o)
          return this.measurementsCache = [], this.itemSizeCache.clear(), this.laneAssignments.clear(), [];
        if (this.laneAssignments.size > e)
          for (const h of this.laneAssignments.keys())
            h >= e && this.laneAssignments.delete(h);
        this.lanesChangedFlag && (this.lanesChangedFlag = !1, this.lanesSettling = !0, this.measurementsCache = [], this.itemSizeCache.clear(), this.laneAssignments.clear(), this.pendingMeasuredCacheIndexes = []), this.measurementsCache.length === 0 && !this.lanesSettling && (this.measurementsCache = this.options.initialMeasurementsCache, this.measurementsCache.forEach((h) => {
          this.itemSizeCache.set(h.key, h.size);
        }));
        const u = this.lanesSettling ? 0 : this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
        this.pendingMeasuredCacheIndexes = [], this.lanesSettling && this.measurementsCache.length === e && (this.lanesSettling = !1);
        const c = this.measurementsCache.slice(0, u), p = new Array(i).fill(
          void 0
        );
        for (let h = 0; h < u; h++) {
          const v = c[h];
          v && (p[v.lane] = h);
        }
        for (let h = u; h < e; h++) {
          const v = n(h), E = this.laneAssignments.get(h);
          let m, f;
          if (E !== void 0 && this.options.lanes > 1) {
            m = E;
            const S = p[m], O = S !== void 0 ? c[S] : void 0;
            f = O ? O.end + this.options.gap : t + s;
          } else {
            const S = this.options.lanes === 1 ? c[h - 1] : this.getFurthestMeasurement(c, h);
            f = S ? S.end + this.options.gap : t + s, m = S ? S.lane : h % this.options.lanes, this.options.lanes > 1 && this.laneAssignments.set(h, m);
          }
          const g = a.get(v), d = typeof g == "number" ? g : this.options.estimateSize(h), b = f + d;
          c[h] = {
            index: h,
            start: f,
            size: d,
            end: b,
            key: v,
            lane: m
          }, p[m] = h;
        }
        return this.measurementsCache = c, c;
      },
      {
        key: process.env.NODE_ENV !== "production" && "getMeasurements",
        debug: () => this.options.debug
      }
    ), this.calculateRange = x(
      () => [
        this.getMeasurements(),
        this.getSize(),
        this.getScrollOffset(),
        this.options.lanes
      ],
      (e, t, s, n) => this.range = e.length > 0 && t > 0 ? U({
        measurements: e,
        outerSize: t,
        scrollOffset: s,
        lanes: n
      }) : null,
      {
        key: process.env.NODE_ENV !== "production" && "calculateRange",
        debug: () => this.options.debug
      }
    ), this.getVirtualIndexes = x(
      () => {
        let e = null, t = null;
        const s = this.calculateRange();
        return s && (e = s.startIndex, t = s.endIndex), this.maybeNotify.updateDeps([this.isScrolling, e, t]), [
          this.options.rangeExtractor,
          this.options.overscan,
          this.options.count,
          e,
          t
        ];
      },
      (e, t, s, n, o) => n === null || o === null ? [] : e({
        startIndex: n,
        endIndex: o,
        overscan: t,
        count: s
      }),
      {
        key: process.env.NODE_ENV !== "production" && "getVirtualIndexes",
        debug: () => this.options.debug
      }
    ), this.indexFromElement = (e) => {
      const t = this.options.indexAttribute, s = e.getAttribute(t);
      return s ? parseInt(s, 10) : (console.warn(
        `Missing attribute name '${t}={index}' on measured element.`
      ), -1);
    }, this.shouldMeasureDuringScroll = (e) => {
      var t;
      if (!this.scrollState || this.scrollState.behavior !== "smooth")
        return !0;
      const s = this.scrollState.index ?? ((t = this.getVirtualItemForOffset(this.scrollState.lastTargetOffset)) == null ? void 0 : t.index);
      if (s !== void 0 && this.range) {
        const n = Math.max(
          this.options.overscan,
          Math.ceil((this.range.endIndex - this.range.startIndex) / 2)
        ), o = Math.max(0, s - n), i = Math.min(
          this.options.count - 1,
          s + n
        );
        return e >= o && e <= i;
      }
      return !0;
    }, this.measureElement = (e) => {
      if (!e) {
        this.elementsCache.forEach((o, i) => {
          o.isConnected || (this.observer.unobserve(o), this.elementsCache.delete(i));
        });
        return;
      }
      const t = this.indexFromElement(e), s = this.options.getItemKey(t), n = this.elementsCache.get(s);
      n !== e && (n && this.observer.unobserve(n), this.observer.observe(e), this.elementsCache.set(s, e)), (!this.isScrolling || this.scrollState) && this.shouldMeasureDuringScroll(t) && this.resizeItem(t, this.options.measureElement(e, void 0, this));
    }, this.resizeItem = (e, t) => {
      var s;
      const n = this.measurementsCache[e];
      if (!n) return;
      const o = this.itemSizeCache.get(n.key) ?? n.size, i = t - o;
      i !== 0 && (((s = this.scrollState) == null ? void 0 : s.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(n, i, this) : n.start < this.getScrollOffset() + this.scrollAdjustments) && (process.env.NODE_ENV !== "production" && this.options.debug && console.info("correction", i), this._scrollToOffset(this.getScrollOffset(), {
        adjustments: this.scrollAdjustments += i,
        behavior: void 0
      })), this.pendingMeasuredCacheIndexes.push(n.index), this.itemSizeCache = new Map(this.itemSizeCache.set(n.key, t)), this.notify(!1));
    }, this.getVirtualItems = x(
      () => [this.getVirtualIndexes(), this.getMeasurements()],
      (e, t) => {
        const s = [];
        for (let n = 0, o = e.length; n < o; n++) {
          const i = e[n], a = t[i];
          s.push(a);
        }
        return s;
      },
      {
        key: process.env.NODE_ENV !== "production" && "getVirtualItems",
        debug: () => this.options.debug
      }
    ), this.getVirtualItemForOffset = (e) => {
      const t = this.getMeasurements();
      if (t.length !== 0)
        return I(
          t[R(
            0,
            t.length - 1,
            (s) => I(t[s]).start,
            e
          )]
        );
    }, this.getMaxScrollOffset = () => {
      if (!this.scrollElement) return 0;
      if ("scrollHeight" in this.scrollElement)
        return this.options.horizontal ? this.scrollElement.scrollWidth - this.scrollElement.clientWidth : this.scrollElement.scrollHeight - this.scrollElement.clientHeight;
      {
        const e = this.scrollElement.document.documentElement;
        return this.options.horizontal ? e.scrollWidth - this.scrollElement.innerWidth : e.scrollHeight - this.scrollElement.innerHeight;
      }
    }, this.getOffsetForAlignment = (e, t, s = 0) => {
      if (!this.scrollElement) return 0;
      const n = this.getSize(), o = this.getScrollOffset();
      t === "auto" && (t = e >= o + n ? "end" : "start"), t === "center" ? e += (s - n) / 2 : t === "end" && (e -= n);
      const i = this.getMaxScrollOffset();
      return Math.max(Math.min(i, e), 0);
    }, this.getOffsetForIndex = (e, t = "auto") => {
      e = Math.max(0, Math.min(e, this.options.count - 1));
      const s = this.getSize(), n = this.getScrollOffset(), o = this.measurementsCache[e];
      if (!o) return;
      if (t === "auto")
        if (o.end >= n + s - this.options.scrollPaddingEnd)
          t = "end";
        else if (o.start <= n + this.options.scrollPaddingStart)
          t = "start";
        else
          return [n, t];
      if (t === "end" && e === this.options.count - 1)
        return [this.getMaxScrollOffset(), t];
      const i = t === "end" ? o.end + this.options.scrollPaddingEnd : o.start - this.options.scrollPaddingStart;
      return [
        this.getOffsetForAlignment(i, t, o.size),
        t
      ];
    }, this.scrollToOffset = (e, { align: t = "start", behavior: s = "auto" } = {}) => {
      const n = this.getOffsetForAlignment(e, t), o = this.now();
      this.scrollState = {
        index: null,
        align: t,
        behavior: s,
        startedAt: o,
        lastTargetOffset: n,
        stableFrames: 0
      }, this._scrollToOffset(n, { adjustments: void 0, behavior: s }), this.scheduleScrollReconcile();
    }, this.scrollToIndex = (e, {
      align: t = "auto",
      behavior: s = "auto"
    } = {}) => {
      e = Math.max(0, Math.min(e, this.options.count - 1));
      const n = this.getOffsetForIndex(e, t);
      if (!n)
        return;
      const [o, i] = n, a = this.now();
      this.scrollState = {
        index: e,
        align: i,
        behavior: s,
        startedAt: a,
        lastTargetOffset: o,
        stableFrames: 0
      }, this._scrollToOffset(o, { adjustments: void 0, behavior: s }), this.scheduleScrollReconcile();
    }, this.scrollBy = (e, { behavior: t = "auto" } = {}) => {
      const s = this.getScrollOffset() + e, n = this.now();
      this.scrollState = {
        index: null,
        align: "start",
        behavior: t,
        startedAt: n,
        lastTargetOffset: s,
        stableFrames: 0
      }, this._scrollToOffset(s, { adjustments: void 0, behavior: t }), this.scheduleScrollReconcile();
    }, this.getTotalSize = () => {
      var e;
      const t = this.getMeasurements();
      let s;
      if (t.length === 0)
        s = this.options.paddingStart;
      else if (this.options.lanes === 1)
        s = ((e = t[t.length - 1]) == null ? void 0 : e.end) ?? 0;
      else {
        const n = Array(this.options.lanes).fill(null);
        let o = t.length - 1;
        for (; o >= 0 && n.some((i) => i === null); ) {
          const i = t[o];
          n[i.lane] === null && (n[i.lane] = i.end), o--;
        }
        s = Math.max(...n.filter((i) => i !== null));
      }
      return Math.max(
        s - this.options.scrollMargin + this.options.paddingEnd,
        0
      );
    }, this._scrollToOffset = (e, {
      adjustments: t,
      behavior: s
    }) => {
      this.options.scrollToFn(e, { behavior: s, adjustments: t }, this);
    }, this.measure = () => {
      this.itemSizeCache = /* @__PURE__ */ new Map(), this.laneAssignments = /* @__PURE__ */ new Map(), this.notify(!1);
    }, this.setOptions(l);
  }
  scheduleScrollReconcile() {
    if (!this.targetWindow) {
      this.scrollState = null;
      return;
    }
    this.rafId == null && (this.rafId = this.targetWindow.requestAnimationFrame(() => {
      this.rafId = null, this.reconcileScroll();
    }));
  }
  reconcileScroll() {
    if (!this.scrollState || !this.scrollElement) return;
    if (this.now() - this.scrollState.startedAt > 5e3) {
      this.scrollState = null;
      return;
    }
    const t = this.scrollState.index != null ? this.getOffsetForIndex(this.scrollState.index, this.scrollState.align) : void 0, s = t ? t[0] : this.scrollState.lastTargetOffset, n = 1, o = s !== this.scrollState.lastTargetOffset;
    if (!o && N(s, this.getScrollOffset())) {
      if (this.scrollState.stableFrames++, this.scrollState.stableFrames >= n) {
        this.scrollState = null;
        return;
      }
    } else
      this.scrollState.stableFrames = 0, o && (this.scrollState.lastTargetOffset = s, this.scrollState.behavior = "auto", this._scrollToOffset(s, {
        adjustments: void 0,
        behavior: "auto"
      }));
    this.scheduleScrollReconcile();
  }
}
const R = (r, l, e, t) => {
  for (; r <= l; ) {
    const s = (r + l) / 2 | 0, n = e(s);
    if (n < t)
      r = s + 1;
    else if (n > t)
      l = s - 1;
    else
      return s;
  }
  return r > 0 ? r - 1 : 0;
};
function U({
  measurements: r,
  outerSize: l,
  scrollOffset: e,
  lanes: t
}) {
  const s = r.length - 1, n = (a) => r[a].start;
  if (r.length <= t)
    return {
      startIndex: 0,
      endIndex: s
    };
  let o = R(
    0,
    s,
    n,
    e
  ), i = o;
  if (t === 1)
    for (; i < s && r[i].end < e + l; )
      i++;
  else if (t > 1) {
    const a = Array(t).fill(0);
    for (; i < s && a.some((c) => c < e + l); ) {
      const c = r[i];
      a[c.lane] = c.end, i++;
    }
    const u = Array(t).fill(e + l);
    for (; o >= 0 && u.some((c) => c >= e); ) {
      const c = r[o];
      u[c.lane] = c.start, o--;
    }
    o = Math.max(0, o - o % t), i = Math.min(s, i + (t - 1 - i % t));
  }
  return { startIndex: o, endIndex: i };
}
const A = typeof document < "u" ? w.useLayoutEffect : w.useEffect;
function q({
  useFlushSync: r = !0,
  ...l
}) {
  const e = w.useReducer(() => ({}), {})[1], t = {
    ...l,
    onChange: (n, o) => {
      var i;
      r && o ? D(e) : e(), (i = l.onChange) == null || i.call(l, n, o);
    }
  }, [s] = w.useState(
    () => new $(t)
  );
  return s.setOptions(t), A(() => s._didMount(), []), A(() => s._willUpdate()), s;
}
function K(r) {
  return q({
    observeElementRect: j,
    observeElementOffset: B,
    scrollToFn: H,
    ...r
  });
}
const G = 5, X = 0;
function Y(r, l) {
  if (l <= 0)
    throw new Error(`chunkArray: size must be positive, received ${l}`);
  const e = [];
  for (let t = 0; t < r.length; t += l)
    e.push(r.slice(t, t + l));
  return e;
}
function J({
  items: r,
  columns: l,
  scrollElement: e,
  renderItem: t,
  estimateRowSize: s,
  getItemKey: n,
  overscan: o = G,
  rowGapPx: i = X,
  gridClassName: a = "grid grid-cols-1 gap-4"
}) {
  const u = _(null), [c, p] = F(0);
  T(() => {
    const f = u.current;
    if (!f)
      return;
    const g = () => {
      const b = e.getBoundingClientRect(), O = f.getBoundingClientRect().top - b.top + e.scrollTop;
      p(Math.max(0, Math.round(O)));
    };
    g();
    const d = new ResizeObserver(g);
    return d.observe(f), d.observe(e), () => d.disconnect();
  }, [e]);
  const h = k(() => Y(r, l).map(
    (g, d) => g.map((b, S) => ({
      item: b,
      index: d * l + S
    }))
  ), [r, l]), v = K({
    count: h.length,
    getScrollElement: () => e,
    estimateSize: () => s + i,
    overscan: o,
    scrollMargin: c
  }), E = v.getVirtualItems(), m = v.getTotalSize();
  return /* @__PURE__ */ M(
    "div",
    {
      className: "relative w-full",
      "data-testid": "virtualized-grid",
      ref: u,
      style: { height: m },
      children: E.map((f) => {
        const g = h[f.index];
        return g ? /* @__PURE__ */ M(
          "div",
          {
            className: "absolute top-0 left-0 w-full",
            "data-index": f.index,
            ref: v.measureElement,
            style: {
              transform: `translateY(${f.start - c}px)`,
              paddingBottom: i
            },
            children: /* @__PURE__ */ M("div", { className: a, children: g.map(({ item: d, index: b }) => /* @__PURE__ */ M("div", { children: t(d, b) }, n(d))) })
          },
          f.key
        ) : null;
      })
    }
  );
}
function te(r) {
  const { items: l, columns: e, scrollElement: t } = r;
  if (e <= 0)
    throw new Error(
      `VirtualizedGrid: columns must be positive, received ${e}`
    );
  return l.length === 0 || !t ? null : /* @__PURE__ */ M(J, { ...r, scrollElement: t });
}
export {
  te as VirtualizedGrid
};
