const T = (t) => t.parentElement || t.getRootNode().host || null, y = (t) => {
  const o = t.getBoundingClientRect(), n = "offsetWidth" in t && Math.abs(o.width) / t.offsetWidth || 1, e = "offsetHeight" in t && Math.abs(o.height) / t.offsetHeight || 1;
  return {
    top: o.top,
    right: o.left + t.clientWidth * n,
    bottom: o.top + t.clientHeight * e,
    left: o.left
  };
}, d = (t) => {
  const o = parseInt(t, 10);
  return Number.isNaN(o) ? 0 : o;
}, b = (t, o, n, e, i, s) => t < n && o > e ? 0 : t < n ? -(n - t + i) : o > e ? o - t > e - n ? t + i - n : o - e + s : 0, D = (t, o) => {
  var s, r, p;
  const n = t.ownerDocument;
  let e = o, i = t;
  for (; i; ) {
    const u = i === n.body, l = u ? {
      top: 0,
      right: ((s = window.visualViewport) == null ? void 0 : s.width) ?? n.documentElement.clientWidth,
      bottom: ((r = window.visualViewport) == null ? void 0 : r.height) ?? n.documentElement.clientHeight,
      left: 0
    } : y(i), c = getComputedStyle(i), f = b(e.left, e.right, l.left, l.right, d(c.scrollPaddingLeft), d(c.scrollPaddingRight)), h = b(e.top, e.bottom, l.top, l.bottom, d(c.scrollPaddingTop), d(c.scrollPaddingBottom));
    if (f || h)
      if (u)
        (p = n.defaultView) == null || p.scrollBy(f, h);
      else {
        const {
          scrollLeft: w,
          scrollTop: a
        } = i;
        h && (i.scrollTop += h), f && (i.scrollLeft += f);
        const g = i.scrollLeft - w, m = i.scrollTop - a;
        e = {
          left: e.left - g,
          top: e.top - m,
          right: e.right - g,
          bottom: e.bottom - m
        };
      }
    i = u || c.position === "fixed" ? null : T(i);
  }
};
export {
  D as default
};
