import { evaluate as E, placements as D, getAlignmentSides as _, getSide as O, getAlignment as R, getSideAxis as k, getOppositeAlignmentPlacement as F, getPaddingObject as M, rectToClientRect as L, getAlignmentAxis as j, getAxisLength as X } from "../../../../../@floating-ui_utils@0.2.9/node_modules/@floating-ui/utils/dist/floating-ui.utils.js";
function $(n, l, o) {
  let {
    reference: t,
    floating: e
  } = n;
  const a = k(l), s = j(l), r = X(s), m = O(l), g = a === "y", u = t.x + t.width / 2 - e.width / 2, c = t.y + t.height / 2 - e.height / 2, f = t[r] / 2 - e[r] / 2;
  let i;
  switch (m) {
    case "top":
      i = {
        x: u,
        y: t.y - e.height
      };
      break;
    case "bottom":
      i = {
        x: u,
        y: t.y + t.height
      };
      break;
    case "right":
      i = {
        x: t.x + t.width,
        y: c
      };
      break;
    case "left":
      i = {
        x: t.x - e.width,
        y: c
      };
      break;
    default:
      i = {
        x: t.x,
        y: t.y
      };
  }
  switch (R(l)) {
    case "start":
      i[s] -= f * (o && g ? -1 : 1);
      break;
    case "end":
      i[s] += f * (o && g ? -1 : 1);
      break;
  }
  return i;
}
const H = async (n, l, o) => {
  const {
    placement: t = "bottom",
    strategy: e = "absolute",
    middleware: a = [],
    platform: s
  } = o, r = a.filter(Boolean), m = await (s.isRTL == null ? void 0 : s.isRTL(l));
  let g = await s.getElementRects({
    reference: n,
    floating: l,
    strategy: e
  }), {
    x: u,
    y: c
  } = $(g, t, m), f = t, i = {}, x = 0;
  for (let h = 0; h < r.length; h++) {
    const {
      name: v,
      fn: w
    } = r[h], {
      x: p,
      y: A,
      data: P,
      reset: d
    } = await w({
      x: u,
      y: c,
      initialPlacement: t,
      placement: f,
      strategy: e,
      middlewareData: i,
      rects: g,
      platform: s,
      elements: {
        reference: n,
        floating: l
      }
    });
    u = p ?? u, c = A ?? c, i = {
      ...i,
      [v]: {
        ...i[v],
        ...P
      }
    }, d && x <= 50 && (x++, typeof d == "object" && (d.placement && (f = d.placement), d.rects && (g = d.rects === !0 ? await s.getElementRects({
      reference: n,
      floating: l,
      strategy: e
    }) : d.rects), {
      x: u,
      y: c
    } = $(g, f, m)), h = -1);
  }
  return {
    x: u,
    y: c,
    placement: f,
    strategy: e,
    middlewareData: i
  };
};
async function Y(n, l) {
  var o;
  l === void 0 && (l = {});
  const {
    x: t,
    y: e,
    platform: a,
    rects: s,
    elements: r,
    strategy: m
  } = n, {
    boundary: g = "clippingAncestors",
    rootBoundary: u = "viewport",
    elementContext: c = "floating",
    altBoundary: f = !1,
    padding: i = 0
  } = E(l, n), x = M(i), v = r[f ? c === "floating" ? "reference" : "floating" : c], w = L(await a.getClippingRect({
    element: (o = await (a.isElement == null ? void 0 : a.isElement(v))) == null || o ? v : v.contextElement || await (a.getDocumentElement == null ? void 0 : a.getDocumentElement(r.floating)),
    boundary: g,
    rootBoundary: u,
    strategy: m
  })), p = c === "floating" ? {
    x: t,
    y: e,
    width: s.floating.width,
    height: s.floating.height
  } : s.reference, A = await (a.getOffsetParent == null ? void 0 : a.getOffsetParent(r.floating)), P = await (a.isElement == null ? void 0 : a.isElement(A)) ? await (a.getScale == null ? void 0 : a.getScale(A)) || {
    x: 1,
    y: 1
  } : {
    x: 1,
    y: 1
  }, d = L(a.convertOffsetParentRelativeRectToViewportRelativeRect ? await a.convertOffsetParentRelativeRectToViewportRelativeRect({
    elements: r,
    rect: p,
    offsetParent: A,
    strategy: m
  }) : p);
  return {
    top: (w.top - d.top + x.top) / P.y,
    bottom: (d.bottom - w.bottom + x.bottom) / P.y,
    left: (w.left - d.left + x.left) / P.x,
    right: (d.right - w.right + x.right) / P.x
  };
}
function I(n, l, o) {
  return (n ? [...o.filter((e) => R(e) === n), ...o.filter((e) => R(e) !== n)] : o.filter((e) => O(e) === e)).filter((e) => n ? R(e) === n || (l ? F(e) !== e : !1) : !0);
}
const J = function(n) {
  return n === void 0 && (n = {}), {
    name: "autoPlacement",
    options: n,
    async fn(l) {
      var o, t, e;
      const {
        rects: a,
        middlewareData: s,
        placement: r,
        platform: m,
        elements: g
      } = l, {
        crossAxis: u = !1,
        alignment: c,
        allowedPlacements: f = D,
        autoAlignment: i = !0,
        ...x
      } = E(n, l), h = c !== void 0 || f === D ? I(c || null, i, f) : f, v = await Y(l, x), w = ((o = s.autoPlacement) == null ? void 0 : o.index) || 0, p = h[w];
      if (p == null)
        return {};
      const A = _(p, a, await (m.isRTL == null ? void 0 : m.isRTL(g.floating)));
      if (r !== p)
        return {
          reset: {
            placement: h[0]
          }
        };
      const P = [v[O(p)], v[A[0]], v[A[1]]], d = [...((t = s.autoPlacement) == null ? void 0 : t.overflows) || [], {
        placement: p,
        overflows: P
      }], S = h[w + 1];
      if (S)
        return {
          data: {
            index: w + 1,
            overflows: d
          },
          reset: {
            placement: S
          }
        };
      const T = d.map((y) => {
        const b = R(y.placement);
        return [y.placement, b && u ? (
          // Check along the mainAxis and main crossAxis side.
          y.overflows.slice(0, 2).reduce((B, V) => B + V, 0)
        ) : (
          // Check only the mainAxis.
          y.overflows[0]
        ), y.overflows];
      }).sort((y, b) => y[1] - b[1]), C = ((e = T.filter((y) => y[2].slice(
        0,
        // Aligned placements should not check their opposite crossAxis
        // side.
        R(y[0]) ? 2 : 3
      ).every((b) => b <= 0))[0]) == null ? void 0 : e[0]) || T[0][0];
      return C !== r ? {
        data: {
          index: w + 1,
          overflows: d
        },
        reset: {
          placement: C
        }
      } : {};
    }
  };
};
async function q(n, l) {
  const {
    placement: o,
    platform: t,
    elements: e
  } = n, a = await (t.isRTL == null ? void 0 : t.isRTL(e.floating)), s = O(o), r = R(o), m = k(o) === "y", g = ["left", "top"].includes(s) ? -1 : 1, u = a && m ? -1 : 1, c = E(l, n);
  let {
    mainAxis: f,
    crossAxis: i,
    alignmentAxis: x
  } = typeof c == "number" ? {
    mainAxis: c,
    crossAxis: 0,
    alignmentAxis: null
  } : {
    mainAxis: c.mainAxis || 0,
    crossAxis: c.crossAxis || 0,
    alignmentAxis: c.alignmentAxis
  };
  return r && typeof x == "number" && (i = r === "end" ? x * -1 : x), m ? {
    x: i * u,
    y: f * g
  } : {
    x: f * g,
    y: i * u
  };
}
const K = function(n) {
  return n === void 0 && (n = 0), {
    name: "offset",
    options: n,
    async fn(l) {
      var o, t;
      const {
        x: e,
        y: a,
        placement: s,
        middlewareData: r
      } = l, m = await q(l, n);
      return s === ((o = r.offset) == null ? void 0 : o.placement) && (t = r.arrow) != null && t.alignmentOffset ? {} : {
        x: e + m.x,
        y: a + m.y,
        data: {
          ...m,
          placement: s
        }
      };
    }
  };
};
export {
  J as autoPlacement,
  H as computePosition,
  Y as detectOverflow,
  K as offset,
  L as rectToClientRect
};
//# sourceMappingURL=floating-ui.core.js.map
