import { useState as B, useCallback as a, useEffect as E, useMemo as I } from "react";
import L from "../../../hooks/useResizeObserver.js";
const O = 50, x = (d, i) => d.start === i.start && d.end === i.end, P = (d) => {
  const { enabled: i, overscan: u, rows: r, scrollContainer: k, viewportHeightFallback: w } = d, c = k?.view ?? null, [, , { blockSize: f }] = L(c), [l, H] = B({
    itemHeights: {},
    scrollTop: 0,
    viewportHeight: 0,
    visibleRange: { start: 0, end: 10 }
  }), v = a(() => c, [c]), p = a(() => {
    const e = Object.values(l.itemHeights);
    return e.length === 0 ? O : e.reduce((t, n) => t + n, 0) / e.length;
  }, [l.itemHeights]), h = a(
    (e) => l.itemHeights[e] ?? p(),
    [p, l.itemHeights]
  ), T = a(
    (e) => {
      let t = 0;
      for (let n = 0; n < e; n++)
        t += h(n);
      return t;
    },
    [h]
  ), V = a(() => {
    let e = 0;
    for (let t = 0; t < r.length; t++)
      e += h(t);
    return e;
  }, [h, r.length]), m = a(
    (e, t) => {
      if (!i || r.length === 0)
        return { start: 0, end: r.length };
      let n = 0, g = 0;
      for (; n < r.length; ) {
        const o = h(n);
        if (g + o > e - u * o)
          break;
        g += o, n++;
      }
      let s = n;
      for (; s < r.length; ) {
        const o = h(s);
        if (g > e + t + u * o)
          break;
        g += o, s++;
      }
      return {
        start: Math.max(0, n - u),
        end: Math.min(r.length, s + u)
      };
    },
    [i, h, u, r.length]
  ), b = a(() => {
    const e = v();
    if (!e)
      return;
    const t = e.clientHeight || f || w || 0, n = e.scrollTop, g = m(n, t);
    H(
      (s) => s.scrollTop === n && s.viewportHeight === t && x(s.visibleRange, g) ? s : {
        ...s,
        scrollTop: n,
        viewportHeight: t,
        visibleRange: g
      }
    );
  }, [m, v, f, w]);
  E(() => {
    if (i && c)
      return b(), c.addEventListener("scroll", b, { passive: !0 }), () => {
        c.removeEventListener("scroll", b);
      };
  }, [i, b, c]), E(() => {
    i && H((e) => {
      const t = m(e.scrollTop, e.viewportHeight);
      return x(e.visibleRange, t) ? e : {
        ...e,
        visibleRange: t
      };
    });
  }, [m, i, r]);
  const z = a(
    (e, t) => {
      if (!t)
        return;
      const n = t.getBoundingClientRect(), g = window.getComputedStyle(t), s = n.height + Number.parseFloat(g.marginTop || "0") + Number.parseFloat(g.marginBottom || "0");
      s <= 0 || H((o) => {
        if (o.itemHeights[e] === s)
          return o;
        const R = {
          ...o,
          itemHeights: {
            ...o.itemHeights,
            [e]: s
          }
        };
        return {
          ...R,
          visibleRange: m(R.scrollTop, R.viewportHeight)
        };
      });
    },
    [m]
  ), C = I(
    () => i ? r.slice(l.visibleRange.start, l.visibleRange.end).map((e, t) => ({
      index: l.visibleRange.start + t,
      row: e
    })) : [],
    [i, r, l.visibleRange.end, l.visibleRange.start]
  ), F = i ? T(l.visibleRange.start) : 0, M = i ? Math.max(0, V() - T(l.visibleRange.end)) : 0;
  return {
    isVirtualized: i,
    measureRow: z,
    paddingBottom: M,
    paddingTop: F,
    virtualRows: C
  };
};
export {
  P as default
};
//# sourceMappingURL=useTableVirtualization.js.map
