import g from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import h from "../../@babel/runtime/helpers/esm/defineProperty.mjs";
import S from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import * as e from "react";
import K from "../../classnames/index.mjs";
import T from "../../rc-util/es/raf.mjs";
function Q(t, b) {
  var i = "touches" in t ? t.touches[0] : t;
  return i[b ? "pageX" : "pageY"];
}
var he = /* @__PURE__ */ e.forwardRef(function(t, b) {
  var i, W = t.prefixCls, Z = t.rtl, m = t.scrollOffset, I = t.scrollRange, U = t.onStartMove, ee = t.onStopMove, te = t.onScroll, s = t.horizontal, R = t.spinSize, $ = t.containerSize, re = t.style, ae = t.thumbStyle, ne = e.useState(!1), Y = S(ne, 2), l = Y[0], z = Y[1], oe = e.useState(null), N = S(oe, 2), P = N[0], ie = N[1], se = e.useState(null), x = S(se, 2), C = x[0], ue = x[1], p = !Z, H = e.useRef(), O = e.useRef(), ce = e.useState(!1), X = S(ce, 2), j = X[0], B = X[1], V = e.useRef(), w = function() {
    clearTimeout(V.current), B(!0), V.current = setTimeout(function() {
      B(!1);
    }, 3e3);
  }, v = I - $ || 0, E = $ - R || 0, le = v > 0, f = e.useMemo(function() {
    if (m === 0 || v === 0)
      return 0;
    var a = m / v;
    return a * E;
  }, [m, v, E]), ve = function(r) {
    r.stopPropagation(), r.preventDefault();
  }, y = e.useRef({
    top: f,
    dragging: l,
    pageY: P,
    startTop: C
  });
  y.current = {
    top: f,
    dragging: l,
    pageY: P,
    startTop: C
  };
  var _ = function(r) {
    z(!0), ie(Q(r, s)), ue(y.current.top), U(), r.stopPropagation(), r.preventDefault();
  };
  e.useEffect(function() {
    var a = function(L) {
      L.preventDefault();
    }, r = H.current, u = O.current;
    return r.addEventListener("touchstart", a), u.addEventListener("touchstart", _), function() {
      r.removeEventListener("touchstart", a), u.removeEventListener("touchstart", _);
    };
  }, []);
  var k = e.useRef();
  k.current = v;
  var A = e.useRef();
  A.current = E, e.useEffect(function() {
    if (l) {
      var a, r = function(L) {
        var D = y.current, fe = D.dragging, de = D.pageY, me = D.startTop;
        if (T.cancel(a), fe) {
          var F = Q(L, s) - de, M = me;
          !p && s ? M -= F : M += F;
          var G = k.current, J = A.current, ge = J ? M / J : 0, d = Math.ceil(ge * G);
          d = Math.max(d, 0), d = Math.min(d, G), a = T(function() {
            te(d, s);
          });
        }
      }, u = function() {
        z(!1), ee();
      };
      return window.addEventListener("mousemove", r), window.addEventListener("touchmove", r), window.addEventListener("mouseup", u), window.addEventListener("touchend", u), function() {
        window.removeEventListener("mousemove", r), window.removeEventListener("touchmove", r), window.removeEventListener("mouseup", u), window.removeEventListener("touchend", u), T.cancel(a);
      };
    }
  }, [l]), e.useEffect(function() {
    w();
  }, [m]), e.useImperativeHandle(b, function() {
    return {
      delayHidden: w
    };
  });
  var c = "".concat(W, "-scrollbar"), n = {
    position: "absolute",
    visibility: j && le ? null : "hidden"
  }, o = {
    position: "absolute",
    background: "rgba(0, 0, 0, 0.5)",
    borderRadius: 99,
    cursor: "pointer",
    userSelect: "none"
  };
  return s ? (n.height = 8, n.left = 0, n.right = 0, n.bottom = 0, o.height = "100%", o.width = R, p ? o.left = f : o.right = f) : (n.width = 8, n.top = 0, n.bottom = 0, p ? n.right = 0 : n.left = 0, o.width = "100%", o.height = R, o.top = f), /* @__PURE__ */ e.createElement("div", {
    ref: H,
    className: K(c, (i = {}, h(i, "".concat(c, "-horizontal"), s), h(i, "".concat(c, "-vertical"), !s), h(i, "".concat(c, "-visible"), j), i)),
    style: g(g({}, n), re),
    onMouseDown: ve,
    onMouseMove: w
  }, /* @__PURE__ */ e.createElement("div", {
    ref: O,
    className: K("".concat(c, "-thumb"), h({}, "".concat(c, "-thumb-moving"), l)),
    style: g(g({}, o), ae),
    onMouseDown: _
  }));
});
process.env.NODE_ENV !== "production" && (he.displayName = "ScrollBar");
export {
  he as default
};
