import { globalObject, isBrowser } from '@skbkontur/global-object';
import { getParentOrShadowHost } from "../getParentOrShadowHost";
export function scrollYCenterIntoNearestScrollable(element) {
  if (!isBrowser(globalObject)) {
    return;
  }
  var parent = findNearestScrollableParent(element);
  if (!parent) {
    return;
  }
  var elementRect = element.getBoundingClientRect();
  var parentRect = parent.getBoundingClientRect();
  var scrollTopPos = elementRect.top - parentRect.top;
  var scrollBottomPos = elementRect.bottom - parentRect.bottom;
  var scrollCenterPos = (scrollTopPos + scrollBottomPos) / 2;
  if (scrollCenterPos !== 0) {
    parent.scrollTo({
      top: scrollCenterPos
    });
  }
}
export function findNearestScrollableParent(element) {
  if (!isBrowser(globalObject)) {
    return;
  }
  var parent = getParentOrShadowHost(element);
  while (parent) {
    var _globalObject$getComp = globalObject.getComputedStyle(parent),
      overflow = _globalObject$getComp.overflow;
    var overflowValues = overflow.split(' ');
    var isScrollbale = overflowValues.every(function (val) {
      return val === 'auto' || val === 'scroll';
    });
    if (isScrollbale) {
      return parent;
    }
    parent = getParentOrShadowHost(parent);
  }
  return globalObject.document.documentElement;
}