import { useState, useEffect } from "react";
import { inBrowser } from "../utils";
const overflowScrollReg = /scroll|auto/i;
const defaultRoot = inBrowser ? window : void 0;
function isElement(node) {
  const ELEMENT_NODE_TYPE = 1;
  return node.tagName !== "HTML" && node.tagName !== "BODY" && node.nodeType === ELEMENT_NODE_TYPE;
}
function getScrollParent(el, root = defaultRoot) {
  if (root === void 0) {
    root = window;
  }
  let node = el;
  while (node && node !== root && isElement(node)) {
    const { overflowY } = window.getComputedStyle(node);
    if (overflowScrollReg.test(overflowY)) {
      if (node.tagName !== "BODY") {
        return node;
      }
      const htmlOverflowY = window.getComputedStyle(node.parentNode).overflowY;
      if (overflowScrollReg.test(htmlOverflowY)) {
        return node;
      }
    }
    node = node.parentNode;
  }
  return root;
}
function useScrollParent(el) {
  const [scrollParent, setScrollParent] = useState();
  useEffect(() => {
    if (el) {
      setScrollParent(getScrollParent(el.current));
    }
  }, []);
  return scrollParent;
}
var stdin_default = useScrollParent;
export {
  stdin_default as default,
  getScrollParent
};
