import { evaluate as V, getPaddingObject as X, getAlignmentAxis as q, clamp as W, getAlignment as M, placements as Y, getAlignmentSides as G, getSide as B, getOppositePlacement as N, getExpandedPlacements as Q, getOppositeAxisPlacements as U, getSideAxis as j, max as $, rectToClientRect as z, getAxisLength as J, min as _, getOppositeAlignmentPlacement as Z, getOppositeAxis as ee } from "../../../../../@floating-ui_utils@0.2.1/node_modules/@floating-ui/utils/dist/floating-ui.utils.js";
function I(e, i, c) {
  let {
    reference: o,
    floating: n
  } = e;
  const s = j(i), m = q(i), d = J(m), l = B(i), g = s === "y", u = o.x + o.width / 2 - n.width / 2, r = o.y + o.height / 2 - n.height / 2, f = o[d] / 2 - n[d] / 2;
  let t;
  switch (l) {
    case "top":
      t = {
        x: u,
        y: o.y - n.height
      };
      break;
    case "bottom":
      t = {
        x: u,
        y: o.y + o.height
      };
      break;
    case "right":
      t = {
        x: o.x + o.width,
        y: r
      };
      break;
    case "left":
      t = {
        x: o.x - n.width,
        y: r
      };
      break;
    default:
      t = {
        x: o.x,
        y: o.y
      };
  }
  switch (M(i)) {
    case "start":
      t[m] -= f * (c && g ? -1 : 1);
      break;
    case "end":
      t[m] += f * (c && g ? -1 : 1);
      break;
  }
  return t;
}
const oe = async (e, i, c) => {
  const {
    placement: o = "bottom",
    strategy: n = "absolute",
    middleware: s = [],
    platform: m
  } = c, d = s.filter(Boolean), l = await (m.isRTL == null ? void 0 : m.isRTL(i));
  let g = await m.getElementRects({
    reference: e,
    floating: i,
    strategy: n
  }), {
    x: u,
    y: r
  } = I(g, o, l), f = o, t = {}, a = 0;
  for (let x = 0; x < d.length; x++) {
    const {
      name: w,
      fn: v
    } = d[x], {
      x: p,
      y: h,
      data: P,
      reset: y
    } = await v({
      x: u,
      y: r,
      initialPlacement: o,
      placement: f,
      strategy: n,
      middlewareData: t,
      rects: g,
      platform: m,
      elements: {
        reference: e,
        floating: i
      }
    });
    if (u = p ?? u, r = h ?? r, t = {
      ...t,
      [w]: {
        ...t[w],
        ...P
      }
    }, y && a <= 50) {
      a++, typeof y == "object" && (y.placement && (f = y.placement), y.rects && (g = y.rects === !0 ? await m.getElementRects({
        reference: e,
        floating: i,
        strategy: n
      }) : y.rects), {
        x: u,
        y: r
      } = I(g, f, l)), x = -1;
      continue;
    }
  }
  return {
    x: u,
    y: r,
    placement: f,
    strategy: n,
    middlewareData: t
  };
};
async function H(e, i) {
  var c;
  i === void 0 && (i = {});
  const {
    x: o,
    y: n,
    platform: s,
    rects: m,
    elements: d,
    strategy: l
  } = e, {
    boundary: g = "clippingAncestors",
    rootBoundary: u = "viewport",
    elementContext: r = "floating",
    altBoundary: f = !1,
    padding: t = 0
  } = V(i, e), a = X(t), w = d[f ? r === "floating" ? "reference" : "floating" : r], v = z(await s.getClippingRect({
    element: (c = await (s.isElement == null ? void 0 : s.isElement(w))) == null || c ? w : w.contextElement || await (s.getDocumentElement == null ? void 0 : s.getDocumentElement(d.floating)),
    boundary: g,
    rootBoundary: u,
    strategy: l
  })), p = r === "floating" ? {
    ...m.floating,
    x: o,
    y: n
  } : m.reference, h = await (s.getOffsetParent == null ? void 0 : s.getOffsetParent(d.floating)), P = await (s.isElement == null ? void 0 : s.isElement(h)) ? await (s.getScale == null ? void 0 : s.getScale(h)) || {
    x: 1,
    y: 1
  } : {
    x: 1,
    y: 1
  }, y = z(s.convertOffsetParentRelativeRectToViewportRelativeRect ? await s.convertOffsetParentRelativeRectToViewportRelativeRect({
    rect: p,
    offsetParent: h,
    strategy: l
  }) : p);
  return {
    top: (v.top - y.top + a.top) / P.y,
    bottom: (y.bottom - v.bottom + a.bottom) / P.y,
    left: (v.left - y.left + a.left) / P.x,
    right: (y.right - v.right + a.right) / P.x
  };
}
const se = (e) => ({
  name: "arrow",
  options: e,
  async fn(i) {
    const {
      x: c,
      y: o,
      placement: n,
      rects: s,
      platform: m,
      elements: d,
      middlewareData: l
    } = i, {
      element: g,
      padding: u = 0
    } = V(e, i) || {};
    if (g == null)
      return {};
    const r = X(u), f = {
      x: c,
      y: o
    }, t = q(n), a = J(t), x = await m.getDimensions(g), w = t === "y", v = w ? "top" : "left", p = w ? "bottom" : "right", h = w ? "clientHeight" : "clientWidth", P = s.reference[a] + s.reference[t] - f[t] - s.floating[a], y = f[t] - s.reference[t], A = await (m.getOffsetParent == null ? void 0 : m.getOffsetParent(g));
    let b = A ? A[h] : 0;
    (!b || !await (m.isElement == null ? void 0 : m.isElement(A))) && (b = d.floating[h] || s.floating[a]);
    const D = P / 2 - y / 2, C = b / 2 - x[a] / 2 - 1, O = _(r[v], C), T = _(r[p], C), R = O, F = b - x[a] - T, S = b / 2 - x[a] / 2 + D, k = W(R, S, F), E = !l.arrow && M(n) != null && S != k && s.reference[a] / 2 - (S < R ? O : T) - x[a] / 2 < 0, L = E ? S < R ? S - R : S - F : 0;
    return {
      [t]: f[t] + L,
      data: {
        [t]: k,
        centerOffset: S - k - L,
        ...E && {
          alignmentOffset: L
        }
      },
      reset: E
    };
  }
});
function te(e, i, c) {
  return (e ? [...c.filter((n) => M(n) === e), ...c.filter((n) => M(n) !== e)] : c.filter((n) => B(n) === n)).filter((n) => e ? M(n) === e || (i ? Z(n) !== n : !1) : !0);
}
const le = function(e) {
  return e === void 0 && (e = {}), {
    name: "autoPlacement",
    options: e,
    async fn(i) {
      var c, o, n;
      const {
        rects: s,
        middlewareData: m,
        placement: d,
        platform: l,
        elements: g
      } = i, {
        crossAxis: u = !1,
        alignment: r,
        allowedPlacements: f = Y,
        autoAlignment: t = !0,
        ...a
      } = V(e, i), x = r !== void 0 || f === Y ? te(r || null, t, f) : f, w = await H(i, a), v = ((c = m.autoPlacement) == null ? void 0 : c.index) || 0, p = x[v];
      if (p == null)
        return {};
      const h = G(p, s, await (l.isRTL == null ? void 0 : l.isRTL(g.floating)));
      if (d !== p)
        return {
          reset: {
            placement: x[0]
          }
        };
      const P = [w[B(p)], w[h[0]], w[h[1]]], y = [...((o = m.autoPlacement) == null ? void 0 : o.overflows) || [], {
        placement: p,
        overflows: P
      }], A = x[v + 1];
      if (A)
        return {
          data: {
            index: v + 1,
            overflows: y
          },
          reset: {
            placement: A
          }
        };
      const b = y.map((O) => {
        const T = M(O.placement);
        return [O.placement, T && u ? (
          // Check along the mainAxis and main crossAxis side.
          O.overflows.slice(0, 2).reduce((R, F) => R + F, 0)
        ) : (
          // Check only the mainAxis.
          O.overflows[0]
        ), O.overflows];
      }).sort((O, T) => O[1] - T[1]), C = ((n = b.filter((O) => O[2].slice(
        0,
        // Aligned placements should not check their opposite crossAxis
        // side.
        M(O[0]) ? 2 : 3
      ).every((T) => T <= 0))[0]) == null ? void 0 : n[0]) || b[0][0];
      return C !== d ? {
        data: {
          index: v + 1,
          overflows: y
        },
        reset: {
          placement: C
        }
      } : {};
    }
  };
}, ae = function(e) {
  return e === void 0 && (e = {}), {
    name: "flip",
    options: e,
    async fn(i) {
      var c, o;
      const {
        placement: n,
        middlewareData: s,
        rects: m,
        initialPlacement: d,
        platform: l,
        elements: g
      } = i, {
        mainAxis: u = !0,
        crossAxis: r = !0,
        fallbackPlacements: f,
        fallbackStrategy: t = "bestFit",
        fallbackAxisSideDirection: a = "none",
        flipAlignment: x = !0,
        ...w
      } = V(e, i);
      if ((c = s.arrow) != null && c.alignmentOffset)
        return {};
      const v = B(n), p = B(d) === d, h = await (l.isRTL == null ? void 0 : l.isRTL(g.floating)), P = f || (p || !x ? [N(d)] : Q(d));
      !f && a !== "none" && P.push(...U(d, x, a, h));
      const y = [d, ...P], A = await H(i, w), b = [];
      let D = ((o = s.flip) == null ? void 0 : o.overflows) || [];
      if (u && b.push(A[v]), r) {
        const R = G(n, m, h);
        b.push(A[R[0]], A[R[1]]);
      }
      if (D = [...D, {
        placement: n,
        overflows: b
      }], !b.every((R) => R <= 0)) {
        var C, O;
        const R = (((C = s.flip) == null ? void 0 : C.index) || 0) + 1, F = y[R];
        if (F)
          return {
            data: {
              index: R,
              overflows: D
            },
            reset: {
              placement: F
            }
          };
        let S = (O = D.filter((k) => k.overflows[0] <= 0).sort((k, E) => k.overflows[1] - E.overflows[1])[0]) == null ? void 0 : O.placement;
        if (!S)
          switch (t) {
            case "bestFit": {
              var T;
              const k = (T = D.map((E) => [E.placement, E.overflows.filter((L) => L > 0).reduce((L, K) => L + K, 0)]).sort((E, L) => E[1] - L[1])[0]) == null ? void 0 : T[0];
              k && (S = k);
              break;
            }
            case "initialPlacement":
              S = d;
              break;
          }
        if (n !== S)
          return {
            reset: {
              placement: S
            }
          };
      }
      return {};
    }
  };
};
async function ne(e, i) {
  const {
    placement: c,
    platform: o,
    elements: n
  } = e, s = await (o.isRTL == null ? void 0 : o.isRTL(n.floating)), m = B(c), d = M(c), l = j(c) === "y", g = ["left", "top"].includes(m) ? -1 : 1, u = s && l ? -1 : 1, r = V(i, e);
  let {
    mainAxis: f,
    crossAxis: t,
    alignmentAxis: a
  } = typeof r == "number" ? {
    mainAxis: r,
    crossAxis: 0,
    alignmentAxis: null
  } : {
    mainAxis: 0,
    crossAxis: 0,
    alignmentAxis: null,
    ...r
  };
  return d && typeof a == "number" && (t = d === "end" ? a * -1 : a), l ? {
    x: t * u,
    y: f * g
  } : {
    x: f * g,
    y: t * u
  };
}
const ce = function(e) {
  return e === void 0 && (e = 0), {
    name: "offset",
    options: e,
    async fn(i) {
      var c, o;
      const {
        x: n,
        y: s,
        placement: m,
        middlewareData: d
      } = i, l = await ne(i, e);
      return m === ((c = d.offset) == null ? void 0 : c.placement) && (o = d.arrow) != null && o.alignmentOffset ? {} : {
        x: n + l.x,
        y: s + l.y,
        data: {
          ...l,
          placement: m
        }
      };
    }
  };
}, re = function(e) {
  return e === void 0 && (e = {}), {
    name: "shift",
    options: e,
    async fn(i) {
      const {
        x: c,
        y: o,
        placement: n
      } = i, {
        mainAxis: s = !0,
        crossAxis: m = !1,
        limiter: d = {
          fn: (w) => {
            let {
              x: v,
              y: p
            } = w;
            return {
              x: v,
              y: p
            };
          }
        },
        ...l
      } = V(e, i), g = {
        x: c,
        y: o
      }, u = await H(i, l), r = j(B(n)), f = ee(r);
      let t = g[f], a = g[r];
      if (s) {
        const w = f === "y" ? "top" : "left", v = f === "y" ? "bottom" : "right", p = t + u[w], h = t - u[v];
        t = W(p, t, h);
      }
      if (m) {
        const w = r === "y" ? "top" : "left", v = r === "y" ? "bottom" : "right", p = a + u[w], h = a - u[v];
        a = W(p, a, h);
      }
      const x = d.fn({
        ...i,
        [f]: t,
        [r]: a
      });
      return {
        ...x,
        data: {
          x: x.x - c,
          y: x.y - o
        }
      };
    }
  };
}, fe = function(e) {
  return e === void 0 && (e = {}), {
    name: "size",
    options: e,
    async fn(i) {
      const {
        placement: c,
        rects: o,
        platform: n,
        elements: s
      } = i, {
        apply: m = () => {
        },
        ...d
      } = V(e, i), l = await H(i, d), g = B(c), u = M(c), r = j(c) === "y", {
        width: f,
        height: t
      } = o.floating;
      let a, x;
      g === "top" || g === "bottom" ? (a = g, x = u === (await (n.isRTL == null ? void 0 : n.isRTL(s.floating)) ? "start" : "end") ? "left" : "right") : (x = g, a = u === "end" ? "top" : "bottom");
      const w = t - l[a], v = f - l[x], p = !i.middlewareData.shift;
      let h = w, P = v;
      if (r) {
        const A = f - l.left - l.right;
        P = u || p ? _(v, A) : A;
      } else {
        const A = t - l.top - l.bottom;
        h = u || p ? _(w, A) : A;
      }
      if (p && !u) {
        const A = $(l.left, 0), b = $(l.right, 0), D = $(l.top, 0), C = $(l.bottom, 0);
        r ? P = f - 2 * (A !== 0 || b !== 0 ? A + b : $(l.left, l.right)) : h = t - 2 * (D !== 0 || C !== 0 ? D + C : $(l.top, l.bottom));
      }
      await m({
        ...i,
        availableWidth: P,
        availableHeight: h
      });
      const y = await n.getDimensions(s.floating);
      return f !== y.width || t !== y.height ? {
        reset: {
          rects: !0
        }
      } : {};
    }
  };
};
export {
  se as arrow,
  le as autoPlacement,
  oe as computePosition,
  H as detectOverflow,
  ae as flip,
  ce as offset,
  z as rectToClientRect,
  re as shift,
  fe as size
};
