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