import { offset as tt, shift as et, flip as it, size as nt, hide as ot, arrow as st, computePosition as rt, limitShift as ct } from "../../core/dist/floating-ui.core.js";
import { createCoords as b, rectToClientRect as P, round as A, floor as z, max as T, min as S } from "../../utils/dist/floating-ui.utils.js";
import { getOverflowAncestors as V, isElement as y, getWindow as E, getComputedStyle as x, getDocumentElement as C, isHTMLElement as R, isWebKit as X, getNodeName as $, isOverflowElement as M, getNodeScroll as N, getParentNode as W, isLastTraversableNode as D, isTableElement as lt, isContainingBlock as j, getContainingBlock as ft } from "../../utils/dist/floating-ui.utils.dom.js";
function G(t) {
  const e = x(t);
  let n = parseFloat(e.width) || 0, i = parseFloat(e.height) || 0;
  const o = R(t), c = o ? t.offsetWidth : n, s = o ? t.offsetHeight : i, r = A(n) !== c || A(i) !== s;
  return r && (n = c, i = s), {
    width: n,
    height: i,
    $: r
  };
}
function _(t) {
  return y(t) ? t : t.contextElement;
}
function F(t) {
  const e = _(t);
  if (!R(e))
    return b(1);
  const n = e.getBoundingClientRect(), {
    width: i,
    height: o,
    $: c
  } = G(e);
  let s = (c ? A(n.width) : n.width) / i, r = (c ? A(n.height) : n.height) / o;
  return (!s || !Number.isFinite(s)) && (s = 1), (!r || !Number.isFinite(r)) && (r = 1), {
    x: s,
    y: r
  };
}
const ut = /* @__PURE__ */ b(0);
function J(t) {
  const e = E(t);
  return !X() || !e.visualViewport ? ut : {
    x: e.visualViewport.offsetLeft,
    y: e.visualViewport.offsetTop
  };
}
function ht(t, e, n) {
  return e === void 0 && (e = !1), !n || e && n !== E(t) ? !1 : e;
}
function L(t, e, n, i) {
  e === void 0 && (e = !1), n === void 0 && (n = !1);
  const o = t.getBoundingClientRect(), c = _(t);
  let s = b(1);
  e && (i ? y(i) && (s = F(i)) : s = F(t));
  const r = ht(c, n, i) ? J(c) : b(0);
  let l = (o.left + r.x) / s.x, f = (o.top + r.y) / s.y, u = o.width / s.x, h = o.height / s.y;
  if (c) {
    const w = E(c), g = i && y(i) ? E(i) : i;
    let v = w, a = v.frameElement;
    for (; a && i && g !== v; ) {
      const p = F(a), d = a.getBoundingClientRect(), m = x(a), O = d.left + (a.clientLeft + parseFloat(m.paddingLeft)) * p.x, B = d.top + (a.clientTop + parseFloat(m.paddingTop)) * p.y;
      l *= p.x, f *= p.y, u *= p.x, h *= p.y, l += O, f += B, v = E(a), a = v.frameElement;
    }
  }
  return P({
    width: u,
    height: h,
    x: l,
    y: f
  });
}
const at = [":popover-open", ":modal"];
function k(t) {
  return at.some((e) => {
    try {
      return t.matches(e);
    } catch {
      return !1;
    }
  });
}
function dt(t) {
  let {
    elements: e,
    rect: n,
    offsetParent: i,
    strategy: o
  } = t;
  const c = o === "fixed", s = C(i), r = e ? k(e.floating) : !1;
  if (i === s || r && c)
    return n;
  let l = {
    scrollLeft: 0,
    scrollTop: 0
  }, f = b(1);
  const u = b(0), h = R(i);
  if ((h || !h && !c) && (($(i) !== "body" || M(s)) && (l = N(i)), R(i))) {
    const w = L(i);
    f = F(i), u.x = w.x + i.clientLeft, u.y = w.y + i.clientTop;
  }
  return {
    width: n.width * f.x,
    height: n.height * f.y,
    x: n.x * f.x - l.scrollLeft * f.x + u.x,
    y: n.y * f.y - l.scrollTop * f.y + u.y
  };
}
function gt(t) {
  return Array.from(t.getClientRects());
}
function Q(t) {
  return L(C(t)).left + N(t).scrollLeft;
}
function pt(t) {
  const e = C(t), n = N(t), i = t.ownerDocument.body, o = T(e.scrollWidth, e.clientWidth, i.scrollWidth, i.clientWidth), c = T(e.scrollHeight, e.clientHeight, i.scrollHeight, i.clientHeight);
  let s = -n.scrollLeft + Q(t);
  const r = -n.scrollTop;
  return x(i).direction === "rtl" && (s += T(e.clientWidth, i.clientWidth) - o), {
    width: o,
    height: c,
    x: s,
    y: r
  };
}
function mt(t, e) {
  const n = E(t), i = C(t), o = n.visualViewport;
  let c = i.clientWidth, s = i.clientHeight, r = 0, l = 0;
  if (o) {
    c = o.width, s = o.height;
    const f = X();
    (!f || f && e === "fixed") && (r = o.offsetLeft, l = o.offsetTop);
  }
  return {
    width: c,
    height: s,
    x: r,
    y: l
  };
}
function wt(t, e) {
  const n = L(t, !0, e === "fixed"), i = n.top + t.clientTop, o = n.left + t.clientLeft, c = R(t) ? F(t) : b(1), s = t.clientWidth * c.x, r = t.clientHeight * c.y, l = o * c.x, f = i * c.y;
  return {
    width: s,
    height: r,
    x: l,
    y: f
  };
}
function U(t, e, n) {
  let i;
  if (e === "viewport")
    i = mt(t, n);
  else if (e === "document")
    i = pt(C(t));
  else if (y(e))
    i = wt(e, n);
  else {
    const o = J(t);
    i = {
      ...e,
      x: e.x - o.x,
      y: e.y - o.y
    };
  }
  return P(i);
}
function Y(t, e) {
  const n = W(t);
  return n === e || !y(n) || D(n) ? !1 : x(n).position === "fixed" || Y(n, e);
}
function vt(t, e) {
  const n = e.get(t);
  if (n)
    return n;
  let i = V(t, [], !1).filter((r) => y(r) && $(r) !== "body"), o = null;
  const c = x(t).position === "fixed";
  let s = c ? W(t) : t;
  for (; y(s) && !D(s); ) {
    const r = x(s), l = j(s);
    !l && r.position === "fixed" && (o = null), (c ? !l && !o : !l && r.position === "static" && !!o && ["absolute", "fixed"].includes(o.position) || M(s) && !l && Y(t, s)) ? i = i.filter((u) => u !== s) : o = r, s = W(s);
  }
  return e.set(t, i), i;
}
function yt(t) {
  let {
    element: e,
    boundary: n,
    rootBoundary: i,
    strategy: o
  } = t;
  const s = [...n === "clippingAncestors" ? k(e) ? [] : vt(e, this._c) : [].concat(n), i], r = s[0], l = s.reduce((f, u) => {
    const h = U(e, u, o);
    return f.top = T(h.top, f.top), f.right = S(h.right, f.right), f.bottom = S(h.bottom, f.bottom), f.left = T(h.left, f.left), f;
  }, U(e, r, o));
  return {
    width: l.right - l.left,
    height: l.bottom - l.top,
    x: l.left,
    y: l.top
  };
}
function xt(t) {
  const {
    width: e,
    height: n
  } = G(t);
  return {
    width: e,
    height: n
  };
}
function bt(t, e, n) {
  const i = R(e), o = C(e), c = n === "fixed", s = L(t, !0, c, e);
  let r = {
    scrollLeft: 0,
    scrollTop: 0
  };
  const l = b(0);
  if (i || !i && !c)
    if (($(e) !== "body" || M(o)) && (r = N(e)), i) {
      const h = L(e, !0, c, e);
      l.x = h.x + e.clientLeft, l.y = h.y + e.clientTop;
    } else
      o && (l.x = Q(o));
  const f = s.left + r.scrollLeft - l.x, u = s.top + r.scrollTop - l.y;
  return {
    x: f,
    y: u,
    width: s.width,
    height: s.height
  };
}
function I(t) {
  return x(t).position === "static";
}
function K(t, e) {
  return !R(t) || x(t).position === "fixed" ? null : e ? e(t) : t.offsetParent;
}
function Z(t, e) {
  const n = E(t);
  if (k(t))
    return n;
  if (!R(t)) {
    let o = W(t);
    for (; o && !D(o); ) {
      if (y(o) && !I(o))
        return o;
      o = W(o);
    }
    return n;
  }
  let i = K(t, e);
  for (; i && lt(i) && I(i); )
    i = K(i, e);
  return i && D(i) && I(i) && !j(i) ? n : i || ft(t) || n;
}
const Rt = async function(t) {
  const e = this.getOffsetParent || Z, n = this.getDimensions, i = await n(t.floating);
  return {
    reference: bt(t.reference, await e(t.floating), t.strategy),
    floating: {
      x: 0,
      y: 0,
      width: i.width,
      height: i.height
    }
  };
};
function Ct(t) {
  return x(t).direction === "rtl";
}
const Ot = {
  convertOffsetParentRelativeRectToViewportRelativeRect: dt,
  getDocumentElement: C,
  getClippingRect: yt,
  getOffsetParent: Z,
  getElementRects: Rt,
  getClientRects: gt,
  getDimensions: xt,
  getScale: F,
  isElement: y,
  isRTL: Ct
};
function Et(t, e) {
  let n = null, i;
  const o = C(t);
  function c() {
    var r;
    clearTimeout(i), (r = n) == null || r.disconnect(), n = null;
  }
  function s(r, l) {
    r === void 0 && (r = !1), l === void 0 && (l = 1), c();
    const {
      left: f,
      top: u,
      width: h,
      height: w
    } = t.getBoundingClientRect();
    if (r || e(), !h || !w)
      return;
    const g = z(u), v = z(o.clientWidth - (f + h)), a = z(o.clientHeight - (u + w)), p = z(f), m = {
      rootMargin: -g + "px " + -v + "px " + -a + "px " + -p + "px",
      threshold: T(0, S(1, l)) || 1
    };
    let O = !0;
    function B(q) {
      const H = q[0].intersectionRatio;
      if (H !== l) {
        if (!O)
          return s();
        H ? s(!1, H) : i = setTimeout(() => {
          s(!1, 1e-7);
        }, 1e3);
      }
      O = !1;
    }
    try {
      n = new IntersectionObserver(B, {
        ...m,
        // Handle <iframe>s
        root: o.ownerDocument
      });
    } catch {
      n = new IntersectionObserver(B, m);
    }
    n.observe(t);
  }
  return s(!0), c;
}
function Wt(t, e, n, i) {
  i === void 0 && (i = {});
  const {
    ancestorScroll: o = !0,
    ancestorResize: c = !0,
    elementResize: s = typeof ResizeObserver == "function",
    layoutShift: r = typeof IntersectionObserver == "function",
    animationFrame: l = !1
  } = i, f = _(t), u = o || c ? [...f ? V(f) : [], ...V(e)] : [];
  u.forEach((d) => {
    o && d.addEventListener("scroll", n, {
      passive: !0
    }), c && d.addEventListener("resize", n);
  });
  const h = f && r ? Et(f, n) : null;
  let w = -1, g = null;
  s && (g = new ResizeObserver((d) => {
    let [m] = d;
    m && m.target === f && g && (g.unobserve(e), cancelAnimationFrame(w), w = requestAnimationFrame(() => {
      var O;
      (O = g) == null || O.observe(e);
    })), n();
  }), f && !l && g.observe(f), g.observe(e));
  let v, a = l ? L(t) : null;
  l && p();
  function p() {
    const d = L(t);
    a && (d.x !== a.x || d.y !== a.y || d.width !== a.width || d.height !== a.height) && n(), a = d, v = requestAnimationFrame(p);
  }
  return n(), () => {
    var d;
    u.forEach((m) => {
      o && m.removeEventListener("scroll", n), c && m.removeEventListener("resize", n);
    }), h == null || h(), (d = g) == null || d.disconnect(), g = null, l && cancelAnimationFrame(v);
  };
}
const Bt = tt, zt = et, At = it, Dt = nt, Nt = ot, Ht = st, It = ct, St = (t, e, n) => {
  const i = /* @__PURE__ */ new Map(), o = {
    platform: Ot,
    ...n
  }, c = {
    ...o.platform,
    _c: i
  };
  return rt(t, e, {
    ...o,
    platform: c
  });
};
export {
  Ht as arrow,
  Wt as autoUpdate,
  St as computePosition,
  At as flip,
  V as getOverflowAncestors,
  Nt as hide,
  It as limitShift,
  Bt as offset,
  Ot as platform,
  zt as shift,
  Dt as size
};
