import { useState, useRef, useMemo, useEffect } from 'react';

function canScroll(el, direction = "vertical") {
  const overflow = window.getComputedStyle(el).getPropertyValue("overflow");
  if (overflow === "hidden")
    return false;
  if (direction === "vertical") {
    return el.scrollHeight > el.clientHeight;
  } else if (direction === "horizontal") {
    return el.scrollWidth > el.clientWidth;
  }
  return false;
}
const useTableBodyHasScrollBar = (tableBodyEl, data) => {
  const [hasScrollBar, setHasScrollBar] = useState(false);
  const antTableBodyRef = useRef();
  const observeTableBodyResize = useMemo(
    () => new ResizeObserver((entries) => {
      const target = entries[0].target;
      if (target) {
        setHasScrollBar(canScroll(target));
      }
    }),
    []
  );
  useEffect(() => {
    var _a;
    const tableWrapper = (_a = tableBodyEl == null ? void 0 : tableBodyEl.current) == null ? void 0 : _a.querySelector(".ant-table-body");
    if (tableWrapper) {
      if (antTableBodyRef.current) {
        observeTableBodyResize.unobserve(antTableBodyRef.current);
      }
      antTableBodyRef.current = tableWrapper;
      setHasScrollBar(canScroll(antTableBodyRef.current));
      observeTableBodyResize.observe(antTableBodyRef.current);
    }
    return () => {
      observeTableBodyResize.disconnect();
    };
  }, [tableBodyEl, data, observeTableBodyResize]);
  return hasScrollBar;
};

export { useTableBodyHasScrollBar };
