import H from "../../../@babel/runtime/helpers/esm/extends.js";
import { forwardRef as N, createElement as m, useRef as he, useEffect as ue, useState as C } from "react";
import { useFloating as ge, arrow as ve } from "../../../@floating-ui/react-dom/dist/floating-ui.react-dom.js";
import { Root as xe } from "../../react-arrow/dist/index.js";
import { useComposedRefs as B } from "../../react-compose-refs/dist/index.js";
import { createContextScope as Pe } from "../../react-context/dist/index.js";
import { Primitive as F } from "../../react-primitive/dist/index.js";
import { useCallbackRef as be } from "../../react-use-callback-ref/dist/index.js";
import { useLayoutEffect as k } from "../../react-use-layout-effect/dist/index.js";
import { useSize as ye } from "../../react-use-size/dist/index.js";
import { autoUpdate as Ae, offset as _e, shift as Ce, flip as Se, size as Oe, hide as De, limitShift as Re } from "../../../@floating-ui/dom/dist/floating-ui.dom.js";
const T = "Popper", [j, Ve] = Pe(T), [Ee, Z] = j(T), He = (o) => {
  const { __scopePopper: i, children: r } = o, [t, a] = C(null);
  return /* @__PURE__ */ m(Ee, {
    scope: i,
    anchor: t,
    onAnchorChange: a
  }, r);
}, Ne = "PopperAnchor", We = /* @__PURE__ */ N((o, i) => {
  const { __scopePopper: r, virtualRef: t, ...a } = o, e = Z(Ne, r), n = he(null), s = B(i, n);
  return ue(() => {
    e.onAnchorChange((t == null ? void 0 : t.current) || n.current);
  }), t ? null : /* @__PURE__ */ m(F.div, H({}, a, {
    ref: s
  }));
}), L = "PopperContent", [ze, Ye] = j(L), Ie = /* @__PURE__ */ N((o, i) => {
  var r, t, a, e, n, s, w, l;
  const { __scopePopper: S, side: d = "bottom", sideOffset: O = 0, align: c = "center", alignOffset: h = 0, arrowPadding: D = 0, avoidCollisions: p = !0, collisionBoundary: u = [], collisionPadding: g = 0, sticky: f = "partial", hideWhenDetached: $ = !1, updatePositionStrategy: q = "optimized", onPlaced: G, ...W } = o, J = Z(L, S), [R, K] = C(null), Q = B(
    i,
    (P) => K(P)
  ), [E, V] = C(null), v = ye(E), ee = (r = v == null ? void 0 : v.width) !== null && r !== void 0 ? r : 0, z = (t = v == null ? void 0 : v.height) !== null && t !== void 0 ? t : 0, te = d + (c !== "center" ? "-" + c : ""), oe = typeof g == "number" ? g : {
    top: 0,
    right: 0,
    bottom: 0,
    left: 0,
    ...g
  }, Y = Array.isArray(u) ? u : [
    u
  ], re = Y.length > 0, b = {
    padding: oe,
    boundary: Y.filter(ke),
    // with `strategy: 'fixed'`, this is the only way to get it to respect boundaries
    altBoundary: re
  }, { refs: ae, floatingStyles: I, placement: ne, isPositioned: y, middlewareData: x } = ge({
    // default to `fixed` strategy so users don't have to pick and we also avoid focus scroll issues
    strategy: "fixed",
    placement: te,
    whileElementsMounted: (...P) => Ae(...P, {
      animationFrame: q === "always"
    }),
    elements: {
      reference: J.anchor
    },
    middleware: [
      _e({
        mainAxis: O + z,
        alignmentAxis: h
      }),
      p && Ce({
        mainAxis: !0,
        crossAxis: !1,
        limiter: f === "partial" ? Re() : void 0,
        ...b
      }),
      p && Se({
        ...b
      }),
      Oe({
        ...b,
        apply: ({ elements: P, rects: M, availableWidth: fe, availableHeight: $e }) => {
          const { width: me, height: we } = M.reference, _ = P.floating.style;
          _.setProperty("--radix-popper-available-width", `${fe}px`), _.setProperty("--radix-popper-available-height", `${$e}px`), _.setProperty("--radix-popper-anchor-width", `${me}px`), _.setProperty("--radix-popper-anchor-height", `${we}px`);
        }
      }),
      E && ve({
        element: E,
        padding: D
      }),
      Be({
        arrowWidth: ee,
        arrowHeight: z
      }),
      $ && De({
        strategy: "referenceHidden",
        ...b
      })
    ]
  }), [X, ie] = U(ne), A = be(G);
  k(() => {
    y && (A == null || A());
  }, [
    y,
    A
  ]);
  const de = (a = x.arrow) === null || a === void 0 ? void 0 : a.x, ce = (e = x.arrow) === null || e === void 0 ? void 0 : e.y, se = ((n = x.arrow) === null || n === void 0 ? void 0 : n.centerOffset) !== 0, [le, pe] = C();
  return k(() => {
    R && pe(window.getComputedStyle(R).zIndex);
  }, [
    R
  ]), /* @__PURE__ */ m("div", {
    ref: ae.setFloating,
    "data-radix-popper-content-wrapper": "",
    style: {
      ...I,
      transform: y ? I.transform : "translate(0, -200%)",
      // keep off the page when measuring
      minWidth: "max-content",
      zIndex: le,
      "--radix-popper-transform-origin": [
        (s = x.transformOrigin) === null || s === void 0 ? void 0 : s.x,
        (w = x.transformOrigin) === null || w === void 0 ? void 0 : w.y
      ].join(" ")
    },
    dir: o.dir
  }, /* @__PURE__ */ m(ze, {
    scope: S,
    placedSide: X,
    onArrowChange: V,
    arrowX: de,
    arrowY: ce,
    shouldHideArrow: se
  }, /* @__PURE__ */ m(F.div, H({
    "data-side": X,
    "data-align": ie
  }, W, {
    ref: Q,
    style: {
      ...W.style,
      // if the PopperContent hasn't been placed yet (not all measurements done)
      // we prevent animations so that users's animation don't kick in too early referring wrong sides
      animation: y ? void 0 : "none",
      // hide the content if using the hide middleware and should be hidden
      opacity: (l = x.hide) !== null && l !== void 0 && l.referenceHidden ? 0 : void 0
    }
  }))));
}), Xe = "PopperArrow", Me = {
  top: "bottom",
  right: "left",
  bottom: "top",
  left: "right"
}, et = /* @__PURE__ */ N(function(i, r) {
  const { __scopePopper: t, ...a } = i, e = Ye(Xe, t), n = Me[e.placedSide];
  return (
    // we have to use an extra wrapper because `ResizeObserver` (used by `useSize`)
    // doesn't report size as we'd expect on SVG elements.
    // it reports their bounding box which is effectively the largest path inside the SVG.
    /* @__PURE__ */ m("span", {
      ref: e.onArrowChange,
      style: {
        position: "absolute",
        left: e.arrowX,
        top: e.arrowY,
        [n]: 0,
        transformOrigin: {
          top: "",
          right: "0 0",
          bottom: "center 0",
          left: "100% 0"
        }[e.placedSide],
        transform: {
          top: "translateY(100%)",
          right: "translateY(50%) rotate(90deg) translateX(-50%)",
          bottom: "rotate(180deg)",
          left: "translateY(50%) rotate(-90deg) translateX(50%)"
        }[e.placedSide],
        visibility: e.shouldHideArrow ? "hidden" : void 0
      }
    }, /* @__PURE__ */ m(xe, H({}, a, {
      ref: r,
      style: {
        ...a.style,
        // ensures the element can be measured correctly (mostly for if SVG)
        display: "block"
      }
    })))
  );
});
function ke(o) {
  return o !== null;
}
const Be = (o) => ({
  name: "transformOrigin",
  options: o,
  fn(i) {
    var r, t, a, e, n;
    const { placement: s, rects: w, middlewareData: l } = i, d = ((r = l.arrow) === null || r === void 0 ? void 0 : r.centerOffset) !== 0, O = d ? 0 : o.arrowWidth, c = d ? 0 : o.arrowHeight, [h, D] = U(s), p = {
      start: "0%",
      center: "50%",
      end: "100%"
    }[D], u = ((t = (a = l.arrow) === null || a === void 0 ? void 0 : a.x) !== null && t !== void 0 ? t : 0) + O / 2, g = ((e = (n = l.arrow) === null || n === void 0 ? void 0 : n.y) !== null && e !== void 0 ? e : 0) + c / 2;
    let f = "", $ = "";
    return h === "bottom" ? (f = d ? p : `${u}px`, $ = `${-c}px`) : h === "top" ? (f = d ? p : `${u}px`, $ = `${w.floating.height + c}px`) : h === "right" ? (f = `${-c}px`, $ = d ? p : `${g}px`) : h === "left" && (f = `${w.floating.width + c}px`, $ = d ? p : `${g}px`), {
      data: {
        x: f,
        y: $
      }
    };
  }
});
function U(o) {
  const [i, r = "center"] = o.split("-");
  return [
    i,
    r
  ];
}
const tt = He, ot = We, rt = Ie;
export {
  ot as Anchor,
  rt as Content,
  He as Popper,
  We as PopperAnchor,
  et as PopperArrow,
  Ie as PopperContent,
  tt as Root,
  Ve as createPopperScope
};
