import A from "../../@babel/runtime/helpers/esm/defineProperty.mjs";
import n from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import g from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import { useContext as U } from "../../@rc-component/context/es/context.mjs";
import * as t from "react";
import "../../rc-util/node_modules/react-is/index.mjs";
import X from "../../classnames/index.mjs";
import L from "../../rc-util/es/Dom/addEventListener.mjs";
import { getOffset as W } from "../../rc-util/es/Dom/css.mjs";
import _ from "../../rc-util/es/getScrollBarSize.mjs";
import j from "./context/TableContext.mjs";
import { useLayoutState as I } from "./hooks/useFrame.mjs";
var P = function(m, C) {
  var B, w, c = m.scrollBodyRef, E = m.onScroll, M = m.offsetScroll, a = m.container, b = U(j, "prefixCls"), s = ((B = c.current) === null || B === void 0 ? void 0 : B.scrollWidth) || 0, o = ((w = c.current) === null || w === void 0 ? void 0 : w.clientWidth) || 0, f = s && o * (o / s), k = t.useRef(), $ = I({
    scrollLeft: 0,
    isHiddenScrollBar: !1
  }), R = g($, 2), u = R[0], S = R[1], i = t.useRef({
    delta: 0,
    x: 0
  }), D = t.useState(!1), H = g(D, 2), p = H[0], h = H[1], N = function() {
    h(!1);
  }, O = function(e) {
    e.persist(), i.current.delta = e.pageX - u.scrollLeft, i.current.x = 0, h(!0), e.preventDefault();
  }, T = function(e) {
    var l, y = e || ((l = window) === null || l === void 0 ? void 0 : l.event), d = y.buttons;
    if (!p || d === 0) {
      p && h(!1);
      return;
    }
    var v = i.current.x + e.pageX - i.current.x - i.current.delta;
    v <= 0 && (v = 0), v + f >= o && (v = o - f), E({
      scrollLeft: v / o * (s + 2)
    }), i.current.x = e.pageX;
  }, x = function() {
    if (c.current) {
      var e = W(c.current).top, l = e + c.current.offsetHeight, y = a === window ? document.documentElement.scrollTop + window.innerHeight : W(a).top + a.clientHeight;
      l - _() <= y || e >= y - M ? S(function(d) {
        return n(n({}, d), {}, {
          isHiddenScrollBar: !0
        });
      }) : S(function(d) {
        return n(n({}, d), {}, {
          isHiddenScrollBar: !1
        });
      });
    }
  }, z = function(e) {
    S(function(l) {
      return n(n({}, l), {}, {
        scrollLeft: e / s * o || 0
      });
    });
  };
  return t.useImperativeHandle(C, function() {
    return {
      setScrollLeft: z
    };
  }), t.useEffect(function() {
    var r = L(document.body, "mouseup", N, !1), e = L(document.body, "mousemove", T, !1);
    return x(), function() {
      r.remove(), e.remove();
    };
  }, [f, p]), t.useEffect(function() {
    var r = L(a, "scroll", x, !1), e = L(window, "resize", x, !1);
    return function() {
      r.remove(), e.remove();
    };
  }, [a]), t.useEffect(function() {
    u.isHiddenScrollBar || S(function(r) {
      var e = c.current;
      return e ? n(n({}, r), {}, {
        scrollLeft: e.scrollLeft / e.scrollWidth * e.clientWidth
      }) : r;
    });
  }, [u.isHiddenScrollBar]), s <= o || !f || u.isHiddenScrollBar ? null : /* @__PURE__ */ t.createElement("div", {
    style: {
      height: _(),
      width: o,
      bottom: M
    },
    className: "".concat(b, "-sticky-scroll")
  }, /* @__PURE__ */ t.createElement("div", {
    onMouseDown: O,
    ref: k,
    className: X("".concat(b, "-sticky-scroll-bar"), A({}, "".concat(b, "-sticky-scroll-bar-active"), p)),
    style: {
      width: "".concat(f, "px"),
      transform: "translate3d(".concat(u.scrollLeft, "px, 0, 0)")
    }
  }));
};
const oe = /* @__PURE__ */ t.forwardRef(P);
export {
  oe as default
};
