import { getOwnerGlobalObject, isBrowser } from '../../lib/globalObject.js';
import { getParentOrShadowHost } from './getParentOrShadowHost.js';
export function scrollYCenterIntoNearestScrollable(element) {
    var globalObject = getOwnerGlobalObject(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) {
    var globalObject = getOwnerGlobalObject(element);
    if (!isBrowser(globalObject)) {
        return;
    }
    var parent = getParentOrShadowHost(element);
    while (parent) {
        var overflow = globalObject.getComputedStyle(parent).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;
}
//# sourceMappingURL=scrollYCenterIntoNearestScrollable.js.map