import { evaluate as L, getSideAxis as T, getSide as j, clamp as $, getOppositePlacement as Z, getExpandedPlacements as ee, getOppositeAxisPlacements as te, getAlignmentSides as ne, getAlignment as z, min as Y, max as W, getOppositeAxis as G, getPaddingObject as J, getAlignmentAxis as K, sides as ie, rectToClientRect as X, getAxisLength as N } from "../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.es.js";
function I(t, e, d) {
  let {
    reference: a,
    floating: m
  } = t;
  const n = T(e), s = K(e), l = N(s), g = j(e), p = n === "y", r = a.x + a.width / 2 - m.width / 2, o = a.y + a.height / 2 - m.height / 2, f = a[l] / 2 - m[l] / 2;
  let i;
  switch (g) {
    case "top":
      i = {
        x: r,
        y: a.y - m.height
      };
      break;
    case "bottom":
      i = {
        x: r,
        y: a.y + a.height
      };
      break;
    case "right":
      i = {
        x: a.x + a.width,
        y: o
      };
      break;
    case "left":
      i = {
        x: a.x - m.width,
        y: o
      };
      break;
    default:
      i = {
        x: a.x,
        y: a.y
      };
  }
  switch (z(e)) {
    case "start":
      i[s] -= f * (d && p ? -1 : 1);
      break;
    case "end":
      i[s] += f * (d && p ? -1 : 1);
      break;
  }
  return i;
}
async function se(t, e) {
  var d;
  e === void 0 && (e = {});
  const {
    x: a,
    y: m,
    platform: n,
    rects: s,
    elements: l,
    strategy: g
  } = t, {
    boundary: p = "clippingAncestors",
    rootBoundary: r = "viewport",
    elementContext: o = "floating",
    altBoundary: f = !1,
    padding: i = 0
  } = L(e, t), c = J(i), x = l[f ? o === "floating" ? "reference" : "floating" : o], v = X(await n.getClippingRect({
    element: (d = await (n.isElement == null ? void 0 : n.isElement(x))) == null || d ? x : x.contextElement || await (n.getDocumentElement == null ? void 0 : n.getDocumentElement(l.floating)),
    boundary: p,
    rootBoundary: r,
    strategy: g
  })), y = o === "floating" ? {
    x: a,
    y: m,
    width: s.floating.width,
    height: s.floating.height
  } : s.reference, w = await (n.getOffsetParent == null ? void 0 : n.getOffsetParent(l.floating)), h = await (n.isElement == null ? void 0 : n.isElement(w)) ? await (n.getScale == null ? void 0 : n.getScale(w)) || {
    x: 1,
    y: 1
  } : {
    x: 1,
    y: 1
  }, A = X(n.convertOffsetParentRelativeRectToViewportRelativeRect ? await n.convertOffsetParentRelativeRectToViewportRelativeRect({
    elements: l,
    rect: y,
    offsetParent: w,
    strategy: g
  }) : y);
  return {
    top: (v.top - A.top + c.top) / h.y,
    bottom: (A.bottom - v.bottom + c.bottom) / h.y,
    left: (v.left - A.left + c.left) / h.x,
    right: (A.right - v.right + c.right) / h.x
  };
}
const le = async (t, e, d) => {
  const {
    placement: a = "bottom",
    strategy: m = "absolute",
    middleware: n = [],
    platform: s
  } = d, l = n.filter(Boolean), g = await (s.isRTL == null ? void 0 : s.isRTL(e));
  let p = await s.getElementRects({
    reference: t,
    floating: e,
    strategy: m
  }), {
    x: r,
    y: o
  } = I(p, a, g), f = a, i = {}, c = 0;
  for (let x = 0; x < l.length; x++) {
    var u;
    const {
      name: v,
      fn: y
    } = l[x], {
      x: w,
      y: h,
      data: A,
      reset: b
    } = await y({
      x: r,
      y: o,
      initialPlacement: a,
      placement: f,
      strategy: m,
      middlewareData: i,
      rects: p,
      platform: {
        ...s,
        detectOverflow: (u = s.detectOverflow) != null ? u : se
      },
      elements: {
        reference: t,
        floating: e
      }
    });
    r = w ?? r, o = h ?? o, i = {
      ...i,
      [v]: {
        ...i[v],
        ...A
      }
    }, b && c <= 50 && (c++, typeof b == "object" && (b.placement && (f = b.placement), b.rects && (p = b.rects === !0 ? await s.getElementRects({
      reference: t,
      floating: e,
      strategy: m
    }) : b.rects), {
      x: r,
      y: o
    } = I(p, f, g)), x = -1);
  }
  return {
    x: r,
    y: o,
    placement: f,
    strategy: m,
    middlewareData: i
  };
}, ce = (t) => ({
  name: "arrow",
  options: t,
  async fn(e) {
    const {
      x: d,
      y: a,
      placement: m,
      rects: n,
      platform: s,
      elements: l,
      middlewareData: g
    } = e, {
      element: p,
      padding: r = 0
    } = L(t, e) || {};
    if (p == null)
      return {};
    const o = J(r), f = {
      x: d,
      y: a
    }, i = K(m), c = N(i), u = await s.getDimensions(p), x = i === "y", v = x ? "top" : "left", y = x ? "bottom" : "right", w = x ? "clientHeight" : "clientWidth", h = n.reference[c] + n.reference[i] - f[i] - n.floating[c], A = f[i] - n.reference[i], b = await (s.getOffsetParent == null ? void 0 : s.getOffsetParent(p));
    let C = b ? b[w] : 0;
    (!C || !await (s.isElement == null ? void 0 : s.isElement(b))) && (C = l.floating[w] || n.floating[c]);
    const M = h / 2 - A / 2, k = C / 2 - u[c] / 2 - 1, P = Y(o[v], k), H = Y(o[y], k), D = P, F = C - u[c] - H, O = C / 2 - u[c] / 2 + M, B = $(D, O, F), E = !g.arrow && z(m) != null && O !== B && n.reference[c] / 2 - (O < D ? P : H) - u[c] / 2 < 0, S = E ? O < D ? O - D : O - F : 0;
    return {
      [i]: f[i] + S,
      data: {
        [i]: B,
        centerOffset: O - B - S,
        ...E && {
          alignmentOffset: S
        }
      },
      reset: E
    };
  }
}), re = function(t) {
  return t === void 0 && (t = {}), {
    name: "flip",
    options: t,
    async fn(e) {
      var d, a;
      const {
        placement: m,
        middlewareData: n,
        rects: s,
        initialPlacement: l,
        platform: g,
        elements: p
      } = e, {
        mainAxis: r = !0,
        crossAxis: o = !0,
        fallbackPlacements: f,
        fallbackStrategy: i = "bestFit",
        fallbackAxisSideDirection: c = "none",
        flipAlignment: u = !0,
        ...x
      } = L(t, e);
      if ((d = n.arrow) != null && d.alignmentOffset)
        return {};
      const v = j(m), y = T(l), w = j(l) === l, h = await (g.isRTL == null ? void 0 : g.isRTL(p.floating)), A = f || (w || !u ? [Z(l)] : ee(l)), b = c !== "none";
      !f && b && A.push(...te(l, u, c, h));
      const C = [l, ...A], M = await g.detectOverflow(e, x), k = [];
      let P = ((a = n.flip) == null ? void 0 : a.overflows) || [];
      if (r && k.push(M[v]), o) {
        const O = ne(m, s, h);
        k.push(M[O[0]], M[O[1]]);
      }
      if (P = [...P, {
        placement: m,
        overflows: k
      }], !k.every((O) => O <= 0)) {
        var H, D;
        const O = (((H = n.flip) == null ? void 0 : H.index) || 0) + 1, B = C[O];
        if (B && (!(o === "alignment" ? y !== T(B) : !1) || // We leave the current main axis only if every placement on that axis
        // overflows the main axis.
        P.every((R) => T(R.placement) === y ? R.overflows[0] > 0 : !0)))
          return {
            data: {
              index: O,
              overflows: P
            },
            reset: {
              placement: B
            }
          };
        let E = (D = P.filter((S) => S.overflows[0] <= 0).sort((S, R) => S.overflows[1] - R.overflows[1])[0]) == null ? void 0 : D.placement;
        if (!E)
          switch (i) {
            case "bestFit": {
              var F;
              const S = (F = P.filter((R) => {
                if (b) {
                  const V = T(R.placement);
                  return V === y || // Create a bias to the `y` side axis due to horizontal
                  // reading directions favoring greater width.
                  V === "y";
                }
                return !0;
              }).map((R) => [R.placement, R.overflows.filter((V) => V > 0).reduce((V, U) => V + U, 0)]).sort((R, V) => R[1] - V[1])[0]) == null ? void 0 : F[0];
              S && (E = S);
              break;
            }
            case "initialPlacement":
              E = l;
              break;
          }
        if (m !== E)
          return {
            reset: {
              placement: E
            }
          };
      }
      return {};
    }
  };
};
function _(t, e) {
  return {
    top: t.top - e.height,
    right: t.right - e.width,
    bottom: t.bottom - e.height,
    left: t.left - e.width
  };
}
function q(t) {
  return ie.some((e) => t[e] >= 0);
}
const fe = function(t) {
  return t === void 0 && (t = {}), {
    name: "hide",
    options: t,
    async fn(e) {
      const {
        rects: d,
        platform: a
      } = e, {
        strategy: m = "referenceHidden",
        ...n
      } = L(t, e);
      switch (m) {
        case "referenceHidden": {
          const s = await a.detectOverflow(e, {
            ...n,
            elementContext: "reference"
          }), l = _(s, d.reference);
          return {
            data: {
              referenceHiddenOffsets: l,
              referenceHidden: q(l)
            }
          };
        }
        case "escaped": {
          const s = await a.detectOverflow(e, {
            ...n,
            altBoundary: !0
          }), l = _(s, d.floating);
          return {
            data: {
              escapedOffsets: l,
              escaped: q(l)
            }
          };
        }
        default:
          return {};
      }
    }
  };
}, Q = /* @__PURE__ */ new Set(["left", "top"]);
async function oe(t, e) {
  const {
    placement: d,
    platform: a,
    elements: m
  } = t, n = await (a.isRTL == null ? void 0 : a.isRTL(m.floating)), s = j(d), l = z(d), g = T(d) === "y", p = Q.has(s) ? -1 : 1, r = n && g ? -1 : 1, o = L(e, t);
  let {
    mainAxis: f,
    crossAxis: i,
    alignmentAxis: c
  } = typeof o == "number" ? {
    mainAxis: o,
    crossAxis: 0,
    alignmentAxis: null
  } : {
    mainAxis: o.mainAxis || 0,
    crossAxis: o.crossAxis || 0,
    alignmentAxis: o.alignmentAxis
  };
  return l && typeof c == "number" && (i = l === "end" ? c * -1 : c), g ? {
    x: i * r,
    y: f * p
  } : {
    x: f * p,
    y: i * r
  };
}
const me = function(t) {
  return t === void 0 && (t = 0), {
    name: "offset",
    options: t,
    async fn(e) {
      var d, a;
      const {
        x: m,
        y: n,
        placement: s,
        middlewareData: l
      } = e, g = await oe(e, t);
      return s === ((d = l.offset) == null ? void 0 : d.placement) && (a = l.arrow) != null && a.alignmentOffset ? {} : {
        x: m + g.x,
        y: n + g.y,
        data: {
          ...g,
          placement: s
        }
      };
    }
  };
}, de = function(t) {
  return t === void 0 && (t = {}), {
    name: "shift",
    options: t,
    async fn(e) {
      const {
        x: d,
        y: a,
        placement: m,
        platform: n
      } = e, {
        mainAxis: s = !0,
        crossAxis: l = !1,
        limiter: g = {
          fn: (v) => {
            let {
              x: y,
              y: w
            } = v;
            return {
              x: y,
              y: w
            };
          }
        },
        ...p
      } = L(t, e), r = {
        x: d,
        y: a
      }, o = await n.detectOverflow(e, p), f = T(j(m)), i = G(f);
      let c = r[i], u = r[f];
      if (s) {
        const v = i === "y" ? "top" : "left", y = i === "y" ? "bottom" : "right", w = c + o[v], h = c - o[y];
        c = $(w, c, h);
      }
      if (l) {
        const v = f === "y" ? "top" : "left", y = f === "y" ? "bottom" : "right", w = u + o[v], h = u - o[y];
        u = $(w, u, h);
      }
      const x = g.fn({
        ...e,
        [i]: c,
        [f]: u
      });
      return {
        ...x,
        data: {
          x: x.x - d,
          y: x.y - a,
          enabled: {
            [i]: s,
            [f]: l
          }
        }
      };
    }
  };
}, xe = function(t) {
  return t === void 0 && (t = {}), {
    options: t,
    fn(e) {
      const {
        x: d,
        y: a,
        placement: m,
        rects: n,
        middlewareData: s
      } = e, {
        offset: l = 0,
        mainAxis: g = !0,
        crossAxis: p = !0
      } = L(t, e), r = {
        x: d,
        y: a
      }, o = T(m), f = G(o);
      let i = r[f], c = r[o];
      const u = L(l, e), x = typeof u == "number" ? {
        mainAxis: u,
        crossAxis: 0
      } : {
        mainAxis: 0,
        crossAxis: 0,
        ...u
      };
      if (g) {
        const w = f === "y" ? "height" : "width", h = n.reference[f] - n.floating[w] + x.mainAxis, A = n.reference[f] + n.reference[w] - x.mainAxis;
        i < h ? i = h : i > A && (i = A);
      }
      if (p) {
        var v, y;
        const w = f === "y" ? "width" : "height", h = Q.has(j(m)), A = n.reference[o] - n.floating[w] + (h && ((v = s.offset) == null ? void 0 : v[o]) || 0) + (h ? 0 : x.crossAxis), b = n.reference[o] + n.reference[w] + (h ? 0 : ((y = s.offset) == null ? void 0 : y[o]) || 0) - (h ? x.crossAxis : 0);
        c < A ? c = A : c > b && (c = b);
      }
      return {
        [f]: i,
        [o]: c
      };
    }
  };
}, ge = function(t) {
  return t === void 0 && (t = {}), {
    name: "size",
    options: t,
    async fn(e) {
      var d, a;
      const {
        placement: m,
        rects: n,
        platform: s,
        elements: l
      } = e, {
        apply: g = () => {
        },
        ...p
      } = L(t, e), r = await s.detectOverflow(e, p), o = j(m), f = z(m), i = T(m) === "y", {
        width: c,
        height: u
      } = n.floating;
      let x, v;
      o === "top" || o === "bottom" ? (x = o, v = f === (await (s.isRTL == null ? void 0 : s.isRTL(l.floating)) ? "start" : "end") ? "left" : "right") : (v = o, x = f === "end" ? "top" : "bottom");
      const y = u - r.top - r.bottom, w = c - r.left - r.right, h = Y(u - r[x], y), A = Y(c - r[v], w), b = !e.middlewareData.shift;
      let C = h, M = A;
      if ((d = e.middlewareData.shift) != null && d.enabled.x && (M = w), (a = e.middlewareData.shift) != null && a.enabled.y && (C = y), b && !f) {
        const P = W(r.left, 0), H = W(r.right, 0), D = W(r.top, 0), F = W(r.bottom, 0);
        i ? M = c - 2 * (P !== 0 || H !== 0 ? P + H : W(r.left, r.right)) : C = u - 2 * (D !== 0 || F !== 0 ? D + F : W(r.top, r.bottom));
      }
      await g({
        ...e,
        availableWidth: M,
        availableHeight: C
      });
      const k = await s.getDimensions(l.floating);
      return c !== k.width || u !== k.height ? {
        reset: {
          rects: !0
        }
      } : {};
    }
  };
};
export {
  ce as arrow,
  le as computePosition,
  se as detectOverflow,
  re as flip,
  fe as hide,
  xe as limitShift,
  me as offset,
  X as rectToClientRect,
  de as shift,
  ge as size
};
//# sourceMappingURL=floating-ui.core.es.js.map
