import { jsxs, jsx } from 'react/jsx-runtime';
import React__default, { useState, useMemo, useRef, useLayoutEffect, useImperativeHandle } from 'react';

/*!
 * react-base-virtual-list
 * Author: phphe <phphe@outlook.com> (https://github.com/phphe)
 * Released under the MIT License.
 */
const Q = React__default.forwardRef, W = {
  /**
   * The visible space of the list. It is only used before DOM created(SSR).
   */
  listSize: 1e3,
  /**
   * Whether to enable the virtual list feature.
   */
  virtual: true
}, X = Q(function(e, L) {
  var M, V;
  const [c, O] = useState(e.itemSize || 100), h = useMemo(() => e.buffer || Math.max(c * 5, 100), [e.buffer, c]), u = e.items.length, r = useRef(null), I = useRef(null), g = useRef(0), f = useRef(), [$, j] = useState([]), [b, N] = useState(0), [x, v] = useState(e.listSize), [B, D] = useState([]), T = useRef(false), P = useMemo(() => {
    var C;
    const n = c * u;
    let t = b - h, i = n - b - x - h, l = 0, s = 0;
    t <= 0 ? (t = 0, l = 0) : l = Math.floor(t / c), i < 0 && (i = 0), n <= x ? s = u : s = u - Math.floor(i / c), e.virtual || (l = 0, s = u);
    let o = Array.from({ length: s - l }, (S, R) => R + l).concat(e.persistentIndices || []);
    return (C = e.persistentIndices) != null && C.length && (o = [...new Set(o)].sort((S, R) => S - R)), { visible: o.map((S) => e.items[S]), visibleIndices: o, topSpace: t, bottomSpace: i, totalSpace: n };
  }, [e.items, c, u, b, h, x, e.virtual, e.persistentIndices]), { visible: U, visibleIndices: y, topSpace: k, bottomSpace: F, totalSpace: A } = P, z = { paddingTop: `${k}px`, boxSizing: "border-box" };
  F < c * 5 ? z.paddingBottom = `${F}px` : z.height = `${A}px`, useLayoutEffect(() => {
    if (r.current && (v(r.current.clientHeight), e.itemSize == null)) {
      const n = I.current;
      let t = parseFloat(getComputedStyle(n).rowGap);
      t = isNaN(t) ? 0 : t;
      let i = 0, l = 0;
      const s = new Set(e.persistentIndices || []);
      let w = -1;
      for (const o of n.children) {
        if (w++, s.has(y[w]))
          continue;
        const a = getComputedStyle(o);
        a.display !== "none" && (a.position !== "static" && a.position !== "relative" || (l += o.offsetHeight + parseFloat(a.marginTop) + parseFloat(a.marginBottom) + t, i++));
      }
      O(l / i);
    }
  }, [e.itemSize, e.items, B]);
  const G = function(n) {
    var t;
    if (e.virtual && !f.current) {
      if (v(r.current.clientHeight), T.current)
        T.current = false;
      else {
        const i = r.current.scrollTop;
        Math.abs(g.current - i) > (e.triggerDistance ?? c) && (N(i), g.current = i);
      }
      (t = e.onScroll) == null || t.call(this, n);
    }
  };
  return useImperativeHandle(L, () => ({
    scrollToIndex(n, t = "start") {
      f.current = {
        index: n,
        block: t
      };
      const i = n * c;
      r.current.scrollTop = i, N(i), g.current = i, j([]);
    },
    getRootElement() {
      return r.current;
    },
    forceUpdate() {
      D([]);
    }
  }), [c]), useLayoutEffect(() => {
    if (f.current) {
      const { index: n, block: t } = f.current;
      f.current = void 0;
      const i = y.indexOf(n), l = I.current.children[i];
      l && (l.scrollIntoView({ block: t }), T.current = true);
    }
  }, [$]), useLayoutEffect(() => {
    const { ResizeObserver: n } = window, t = n && new n(() => {
      v(r.current.clientHeight);
    });
    return t == null || t.observe(r.current), () => {
      t == null || t.disconnect();
    };
  }, []), /* @__PURE__ */ jsxs("div", { ref: r, onScroll: G, className: e.className, style: { overflow: "auto", ...e.style }, children: [
    (M = e.renderHead) == null ? void 0 : M.call(e),
    /* @__PURE__ */ jsx("div", { ref: I, className: e.innerClassName, style: { display: "flex", flexDirection: "column", ...e.virtual && z }, children: U.map((n, t) => e.renderItem(n, y[t])) }),
    (V = e.renderFoot) == null ? void 0 : V.call(e)
  ] });
});
X.defaultProps = W;

export { X as VirtualList, W as defaultProps };
