import vr from "../../@babel/runtime/helpers/esm/extends.mjs";
import j from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import dr from "../../@babel/runtime/helpers/esm/typeof.mjs";
import S from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import Re from "../../@babel/runtime/helpers/esm/defineProperty.mjs";
import mr from "../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import * as i from "react";
import { useRef as h, useState as Y } from "react";
import { r as xe } from "../../react-dom/index.mjs";
import hr from "../../classnames/index.mjs";
import Sr from "../../rc-resize-observer/es/index.mjs";
import gr from "./Filler.mjs";
import He from "./ScrollBar.mjs";
import pr from "./hooks/useChildren.mjs";
import yr from "./hooks/useHeights.mjs";
import Mr from "./hooks/useScrollTo.mjs";
import Rr from "./hooks/useDiffItem.mjs";
import xr from "./hooks/useFrameWheel.mjs";
import Hr from "./hooks/useMobileTouchMove.mjs";
import _r from "./hooks/useOriginScroll.mjs";
import G from "../../rc-util/es/hooks/useLayoutEffect.mjs";
import { getSpinSize as _e } from "./utils/scrollbarUtil.mjs";
import ze from "../../rc-util/es/hooks/useEvent.mjs";
import "../../rc-util/node_modules/react-is/index.mjs";
import "../../rc-util/es/warning.mjs";
import { useGetSize as zr } from "./hooks/useGetSize.mjs";
var Tr = ["prefixCls", "className", "height", "itemHeight", "fullHeight", "style", "data", "children", "itemKey", "virtual", "direction", "scrollWidth", "component", "onScroll", "onVirtualScroll", "onVisibleChange", "innerProps", "extraRender", "styles"], br = [], wr = {
  overflowY: "auto",
  overflowAnchor: "none"
};
function Er(o, Te) {
  var U = o.prefixCls, g = U === void 0 ? "rc-virtual-list" : U, be = o.className, s = o.height, p = o.itemHeight, X = o.fullHeight, we = X === void 0 ? !0 : X, Ee = o.style, O = o.data, Ce = o.children, w = o.itemKey, Le = o.virtual, Be = o.direction, a = o.scrollWidth, q = o.component, Oe = q === void 0 ? "div" : q, P = o.onScroll, J = o.onVirtualScroll, Q = o.onVisibleChange, Pe = o.innerProps, D = o.extraRender, f = o.styles, De = mr(o, Tr), c = !!(Le !== !1 && s && p), R = c && O && (p * O.length > s || !!a), u = Be === "rtl", Ne = hr(g, Re({}, "".concat(g, "-rtl"), u), be), n = O || br, x = h(), Z = h(), Ve = Y(0), ee = S(Ve, 2), v = ee[0], We = ee[1], Ie = Y(0), re = S(Ie, 2), H = re[0], E = re[1], Fe = Y(!1), te = S(Fe, 2), ke = te[0], oe = te[1], le = function() {
    oe(!0);
  }, ne = function() {
    oe(!1);
  }, y = i.useCallback(function(e) {
    return typeof w == "function" ? w(e) : e == null ? void 0 : e[w];
  }, [w]), Ae = {
    getKey: y
  };
  function C(e) {
    We(function(r) {
      var t;
      typeof e == "function" ? t = e(r) : t = e;
      var l = rr(t);
      return x.current.scrollTop = l, l;
    });
  }
  var ie = h({
    start: 0,
    end: n.length
  }), Ke = h(), $e = Rr(n, y), je = S($e, 1), Ye = je[0];
  Ke.current = Ye;
  var Ge = yr(y, null, null), L = S(Ge, 4), Ue = L[0], ae = L[1], N = L[2], Xe = L[3], B = i.useMemo(function() {
    if (!c)
      return {
        scrollHeight: void 0,
        start: 0,
        end: n.length - 1,
        offset: void 0
      };
    if (!R) {
      var e;
      return {
        scrollHeight: ((e = Z.current) === null || e === void 0 ? void 0 : e.offsetHeight) || 0,
        start: 0,
        end: n.length - 1,
        offset: void 0
      };
    }
    for (var r = 0, t, l, m, sr = n.length, b = 0; b < sr; b += 1) {
      var cr = n[b], ur = y(cr), Me = N.get(ur), $ = r + (Me === void 0 ? p : Me);
      $ >= v && t === void 0 && (t = b, l = r), $ > v + s && m === void 0 && (m = b), r = $;
    }
    return t === void 0 && (t = 0, l = 0, m = Math.ceil(s / p)), m === void 0 && (m = n.length - 1), m = Math.min(m + 1, n.length - 1), {
      scrollHeight: r,
      start: t,
      end: m,
      offset: l
    };
  }, [R, c, v, n, Xe, s]), M = B.scrollHeight, _ = B.start, z = B.end, fe = B.offset;
  ie.current.start = _, ie.current.end = z;
  var qe = i.useState({
    width: 0,
    height: s
  }), se = S(qe, 2), d = se[0], Je = se[1], Qe = function(r) {
    Je({
      width: r.width || r.offsetWidth,
      height: r.height || r.offsetHeight
    });
  }, ce = h(), ue = h(), Ze = i.useMemo(function() {
    return _e(d.width, a);
  }, [d.width, a]), er = i.useMemo(function() {
    return _e(d.height, M);
  }, [d.height, M]), V = M - s, W = h(V);
  W.current = V;
  function rr(e) {
    var r = e;
    return Number.isNaN(W.current) || (r = Math.min(r, W.current)), r = Math.max(r, 0), r;
  }
  var ve = v <= 0, de = v >= V, tr = _r(ve, de), I = function() {
    return {
      x: u ? -H : H,
      y: v
    };
  }, F = h(I()), k = ze(function() {
    if (J) {
      var e = I();
      (F.current.x !== e.x || F.current.y !== e.y) && (J(e), F.current = e);
    }
  });
  function me(e, r) {
    var t = e;
    r ? (xe.flushSync(function() {
      E(t);
    }), k()) : C(t);
  }
  function or(e) {
    var r = e.currentTarget.scrollTop;
    r !== v && C(r), P == null || P(e), k();
  }
  var A = function(r) {
    var t = r, l = a - d.width;
    return t = Math.max(t, 0), t = Math.min(t, l), t;
  }, lr = ze(function(e, r) {
    r ? (xe.flushSync(function() {
      E(function(t) {
        var l = t + (u ? -e : e);
        return A(l);
      });
    }), k()) : C(function(t) {
      var l = t + e;
      return l;
    });
  }), nr = xr(c, ve, de, !!a, lr), he = S(nr, 2), K = he[0], Se = he[1];
  Hr(c, x, function(e, r) {
    return tr(e, r) ? !1 : (K({
      preventDefault: function() {
      },
      deltaY: e
    }), !0);
  }), G(function() {
    function e(t) {
      c && t.preventDefault();
    }
    var r = x.current;
    return r.addEventListener("wheel", K), r.addEventListener("DOMMouseScroll", Se), r.addEventListener("MozMousePixelScroll", e), function() {
      r.removeEventListener("wheel", K), r.removeEventListener("DOMMouseScroll", Se), r.removeEventListener("MozMousePixelScroll", e);
    };
  }, [c]), G(function() {
    a && E(function(e) {
      return A(e);
    });
  }, [d.width, a]);
  var ge = function() {
    var r, t;
    (r = ce.current) === null || r === void 0 || r.delayHidden(), (t = ue.current) === null || t === void 0 || t.delayHidden();
  }, pe = Mr(x, n, N, p, y, function() {
    return ae(!0);
  }, C, ge);
  i.useImperativeHandle(Te, function() {
    return {
      getScrollInfo: I,
      scrollTo: function(r) {
        function t(l) {
          return l && dr(l) === "object" && ("left" in l || "top" in l);
        }
        t(r) ? (r.left !== void 0 && E(A(r.left)), pe(r.top)) : pe(r);
      }
    };
  }), G(function() {
    if (Q) {
      var e = n.slice(_, z + 1);
      Q(e, n);
    }
  }, [_, z, n]);
  var ir = zr(n, y, N, p), ar = D == null ? void 0 : D({
    start: _,
    end: z,
    virtual: R,
    offsetX: H,
    offsetY: fe,
    rtl: u,
    getSize: ir
  }), fr = pr(n, _, z, a, Ue, Ce, Ae), T = null;
  s && (T = j(Re({}, we ? "height" : "maxHeight", s), wr), c && (T.overflowY = "hidden", a && (T.overflowX = "hidden"), ke && (T.pointerEvents = "none")));
  var ye = {};
  return u && (ye.dir = "rtl"), /* @__PURE__ */ i.createElement("div", vr({
    style: j(j({}, Ee), {}, {
      position: "relative"
    }),
    className: Ne
  }, ye, De), /* @__PURE__ */ i.createElement(Sr, {
    onResize: Qe
  }, /* @__PURE__ */ i.createElement(Oe, {
    className: "".concat(g, "-holder"),
    style: T,
    ref: x,
    onScroll: or,
    onMouseEnter: ge
  }, /* @__PURE__ */ i.createElement(gr, {
    prefixCls: g,
    height: M,
    offsetX: H,
    offsetY: fe,
    scrollWidth: a,
    onInnerResize: ae,
    ref: Z,
    innerProps: Pe,
    rtl: u,
    extra: ar
  }, fr))), R && M > s && /* @__PURE__ */ i.createElement(He, {
    ref: ce,
    prefixCls: g,
    scrollOffset: v,
    scrollRange: M,
    rtl: u,
    onScroll: me,
    onStartMove: le,
    onStopMove: ne,
    spinSize: er,
    containerSize: d.height,
    style: f == null ? void 0 : f.verticalScrollBar,
    thumbStyle: f == null ? void 0 : f.verticalScrollBarThumb
  }), R && a && /* @__PURE__ */ i.createElement(He, {
    ref: ue,
    prefixCls: g,
    scrollOffset: H,
    scrollRange: a,
    rtl: u,
    onScroll: me,
    onStartMove: le,
    onStopMove: ne,
    spinSize: Ze,
    containerSize: d.width,
    horizontal: !0,
    style: f == null ? void 0 : f.horizontalScrollBar,
    thumbStyle: f == null ? void 0 : f.horizontalScrollBarThumb
  }));
}
var Cr = /* @__PURE__ */ i.forwardRef(Er);
Cr.displayName = "List";
export {
  Er as RawList,
  Cr as default
};
