import React__default from 'react';

/**
 * @author Hanz
 * @date 2022/10/9 10:12 AM
 * @description 查找最近的祖先滚动容器
 */
//判断是否滚动元素
function isOverflowElement(element) {
  var _window$getComputedSt = window.getComputedStyle(element),
    overflow = _window$getComputedSt.overflow,
    overflowX = _window$getComputedSt.overflowX,
    overflowY = _window$getComputedSt.overflowY;
  return /auto|scroll|overlay/.test(overflow + overflowY + overflowX);
}
//最近的祖先滚动元素
function getNearestOverflowAncestor(node) {
  var _a, _b;
  var parentNode = node.parentNode || node.ownerDocument;
  var nodeName = ((_a = parentNode === null || parentNode === void 0 ? void 0 : parentNode.nodeName) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || '';
  if (['html', 'body', '#document'].includes(nodeName)) {
    return (_b = node === null || node === void 0 ? void 0 : node.ownerDocument) === null || _b === void 0 ? void 0 : _b.body;
  }
  if (parentNode instanceof window.HTMLElement && isOverflowElement(parentNode)) {
    return parentNode;
  }
  return getNearestOverflowAncestor(parentNode);
}
//渲染容器
function usePopupContainer(wrapRef, customizeContainer) {
  var popupRef = React__default.useRef();
  //浮层渲染容器
  var getPopupContainer = function getPopupContainer(triggerNode) {
    var _a, _b, _c, _d;
    var container;
    if (!customizeContainer) {
      var rootNode = (_b = (_a = wrapRef.current) === null || _a === void 0 ? void 0 : _a.getRootDomNode) === null || _b === void 0 ? void 0 : _b.call(_a);
      if (rootNode) {
        container = getNearestOverflowAncestor(rootNode);
        var scrollHeight = container.scrollHeight;
        var clientHeight = container.clientHeight;
        if (scrollHeight <= clientHeight) {
          container = document.body;
        }
      } else {
        container = document.body;
      }
      var nodeName = (_c = container.nodeName) === null || _c === void 0 ? void 0 : _c.toLowerCase();
      if (nodeName !== 'body') {
        if (((_d = window.getComputedStyle(container)) === null || _d === void 0 ? void 0 : _d.position) === 'static') {
          container.style.position = 'relative';
        }
      }
      popupRef.current = container;
    } else {
      container = customizeContainer(triggerNode) || document.body;
    }
    return container;
  };
  React__default.useEffect(function () {
    return function () {
      if (popupRef.current) popupRef.current.style.position = '';
    };
  }, []);
  return getPopupContainer;
}

export { getNearestOverflowAncestor as g, usePopupContainer as u };
