import M from "../../../@babel/runtime/helpers/esm/extends.js";
import { createElement as s, forwardRef as g, useRef as h, useEffect as w, useState as D, useCallback as R, Fragment as Ge } from "react";
import { composeEventHandlers as v } from "../../primitive/dist/index.js";
import { createCollection as Ne } from "../../react-collection/dist/index.js";
import { useComposedRefs as G, composeRefs as Ue } from "../../react-compose-refs/dist/index.js";
import { createContextScope as Ve } from "../../react-context/dist/index.js";
import { useDirection as Be } from "../../react-direction/dist/index.js";
import { DismissableLayer as Ye } from "../../react-dismissable-layer/dist/index.js";
import { useFocusGuards as Xe } from "../../react-focus-guards/dist/index.js";
import { FocusScope as je } from "../../react-focus-scope/dist/index.js";
import { useId as me } from "../../react-id/dist/index.js";
import { createPopperScope as ve, Content as He, Root as Me, Anchor as We } from "../../react-popper/dist/index.js";
import { Portal as ze } from "../../react-portal/dist/index.js";
import { Presence as Z } from "../../react-presence/dist/index.js";
import { Primitive as N, dispatchDiscreteCustomEvent as Ze } from "../../react-primitive/dist/index.js";
import { createRovingFocusGroupScope as Ce, Root as qe, Item as Je } from "../../react-roving-focus/dist/index.js";
import { Slot as Qe } from "../../react-slot/dist/index.js";
import { useCallbackRef as ce } from "../../react-use-callback-ref/dist/index.js";
import { hideOthers as en } from "../../../aria-hidden/dist/es2015/index.js";
import nn from "../../../react-remove-scroll/dist/es2015/Combination.js";
const te = [
  "Enter",
  " "
], tn = [
  "ArrowDown",
  "PageUp",
  "Home"
], ge = [
  "ArrowUp",
  "PageDown",
  "End"
], on = [
  ...tn,
  ...ge
], cn = {
  ltr: [
    ...te,
    "ArrowRight"
  ],
  rtl: [
    ...te,
    "ArrowLeft"
  ]
}, rn = {
  ltr: [
    "ArrowLeft"
  ],
  rtl: [
    "ArrowRight"
  ]
}, q = "Menu", [L, an, sn] = Ne(q), [T, ct] = Ve(q, [
  sn,
  ve,
  Ce
]), J = ve(), he = Ce(), [_e, S] = T(q), [un, U] = T(q), dn = (e) => {
  const { __scopeMenu: t, open: n = !1, children: o, dir: a, onOpenChange: c, modal: i = !0 } = e, $ = J(t), [p, m] = D(null), d = h(!1), r = ce(c), l = Be(a);
  return w(() => {
    const f = () => {
      d.current = !0, document.addEventListener("pointerdown", b, {
        capture: !0,
        once: !0
      }), document.addEventListener("pointermove", b, {
        capture: !0,
        once: !0
      });
    }, b = () => d.current = !1;
    return document.addEventListener("keydown", f, {
      capture: !0
    }), () => {
      document.removeEventListener("keydown", f, {
        capture: !0
      }), document.removeEventListener("pointerdown", b, {
        capture: !0
      }), document.removeEventListener("pointermove", b, {
        capture: !0
      });
    };
  }, []), /* @__PURE__ */ s(Me, $, /* @__PURE__ */ s(_e, {
    scope: t,
    open: n,
    onOpenChange: r,
    content: p,
    onContentChange: m
  }, /* @__PURE__ */ s(un, {
    scope: t,
    onClose: R(
      () => r(!1),
      [
        r
      ]
    ),
    isUsingKeyboardRef: d,
    dir: l,
    modal: i
  }, o)));
}, xe = /* @__PURE__ */ g((e, t) => {
  const { __scopeMenu: n, ...o } = e, a = J(n);
  return /* @__PURE__ */ s(We, M({}, a, o, {
    ref: t
  }));
}), Pe = "MenuPortal", [ln, Ie] = T(Pe, {
  forceMount: void 0
}), fn = (e) => {
  const { __scopeMenu: t, forceMount: n, children: o, container: a } = e, c = S(Pe, t);
  return /* @__PURE__ */ s(ln, {
    scope: t,
    forceMount: n
  }, /* @__PURE__ */ s(Z, {
    present: n || c.open
  }, /* @__PURE__ */ s(ze, {
    asChild: !0,
    container: a
  }, o)));
}, P = "MenuContent", [$n, re] = T(P), pn = /* @__PURE__ */ g((e, t) => {
  const n = Ie(P, e.__scopeMenu), { forceMount: o = n.forceMount, ...a } = e, c = S(P, e.__scopeMenu), i = U(P, e.__scopeMenu);
  return /* @__PURE__ */ s(L.Provider, {
    scope: e.__scopeMenu
  }, /* @__PURE__ */ s(Z, {
    present: o || c.open
  }, /* @__PURE__ */ s(L.Slot, {
    scope: e.__scopeMenu
  }, i.modal ? /* @__PURE__ */ s(mn, M({}, a, {
    ref: t
  })) : /* @__PURE__ */ s(bn, M({}, a, {
    ref: t
  })))));
}), mn = /* @__PURE__ */ g((e, t) => {
  const n = S(P, e.__scopeMenu), o = h(null), a = G(t, o);
  return w(() => {
    const c = o.current;
    if (c)
      return en(c);
  }, []), /* @__PURE__ */ s(ae, M({}, e, {
    ref: a,
    trapFocus: n.open,
    disableOutsidePointerEvents: n.open,
    disableOutsideScroll: !0,
    onFocusOutside: v(
      e.onFocusOutside,
      (c) => c.preventDefault(),
      {
        checkForDefaultPrevented: !1
      }
    ),
    onDismiss: () => n.onOpenChange(!1)
  }));
}), bn = /* @__PURE__ */ g((e, t) => {
  const n = S(P, e.__scopeMenu);
  return /* @__PURE__ */ s(ae, M({}, e, {
    ref: t,
    trapFocus: !1,
    disableOutsidePointerEvents: !1,
    disableOutsideScroll: !1,
    onDismiss: () => n.onOpenChange(!1)
  }));
}), ae = /* @__PURE__ */ g((e, t) => {
  const { __scopeMenu: n, loop: o = !1, trapFocus: a, onOpenAutoFocus: c, onCloseAutoFocus: i, disableOutsidePointerEvents: $, onEntryFocus: p, onEscapeKeyDown: m, onPointerDownOutside: d, onFocusOutside: r, onInteractOutside: l, onDismiss: f, disableOutsideScroll: b, ..._ } = e, y = S(P, n), k = U(P, n), V = J(n), B = he(n), ie = an(n), [De, de] = D(null), Y = h(null), ke = G(t, Y, y.onContentChange), X = h(0), j = h(""), Fe = h(0), Q = h(null), le = h("right"), ee = h(0), Ae = b ? nn : Ge, Le = b ? {
    as: Qe,
    allowPinchZoom: !0
  } : void 0, Ke = (u) => {
    var C, x;
    const E = j.current + u, F = ie().filter(
      (I) => !I.disabled
    ), ne = document.activeElement, fe = (C = F.find(
      (I) => I.ref.current === ne
    )) === null || C === void 0 ? void 0 : C.textValue, H = F.map(
      (I) => I.textValue
    ), A = Fn(H, E, fe), $e = (x = F.find(
      (I) => I.textValue === A
    )) === null || x === void 0 ? void 0 : x.ref.current;
    (function I(pe) {
      j.current = pe, window.clearTimeout(X.current), pe !== "" && (X.current = window.setTimeout(
        () => I(""),
        1e3
      ));
    })(E), $e && setTimeout(
      () => $e.focus()
    );
  };
  w(() => () => window.clearTimeout(X.current), []), Xe();
  const O = R((u) => {
    var C, x;
    return le.current === ((C = Q.current) === null || C === void 0 ? void 0 : C.side) && Ln(u, (x = Q.current) === null || x === void 0 ? void 0 : x.area);
  }, []);
  return /* @__PURE__ */ s($n, {
    scope: n,
    searchRef: j,
    onItemEnter: R((u) => {
      O(u) && u.preventDefault();
    }, [
      O
    ]),
    onItemLeave: R((u) => {
      var C;
      O(u) || ((C = Y.current) === null || C === void 0 || C.focus(), de(null));
    }, [
      O
    ]),
    onTriggerLeave: R((u) => {
      O(u) && u.preventDefault();
    }, [
      O
    ]),
    pointerGraceTimerRef: Fe,
    onPointerGraceIntentChange: R((u) => {
      Q.current = u;
    }, [])
  }, /* @__PURE__ */ s(Ae, Le, /* @__PURE__ */ s(je, {
    asChild: !0,
    trapped: a,
    onMountAutoFocus: v(c, (u) => {
      var C;
      u.preventDefault(), (C = Y.current) === null || C === void 0 || C.focus();
    }),
    onUnmountAutoFocus: i
  }, /* @__PURE__ */ s(Ye, {
    asChild: !0,
    disableOutsidePointerEvents: $,
    onEscapeKeyDown: m,
    onPointerDownOutside: d,
    onFocusOutside: r,
    onInteractOutside: l,
    onDismiss: f
  }, /* @__PURE__ */ s(qe, M({
    asChild: !0
  }, B, {
    dir: k.dir,
    orientation: "vertical",
    loop: o,
    currentTabStopId: De,
    onCurrentTabStopIdChange: de,
    onEntryFocus: v(p, (u) => {
      k.isUsingKeyboardRef.current || u.preventDefault();
    })
  }), /* @__PURE__ */ s(He, M({
    role: "menu",
    "aria-orientation": "vertical",
    "data-state": Oe(y.open),
    "data-radix-menu-content": "",
    dir: k.dir
  }, V, _, {
    ref: ke,
    style: {
      outline: "none",
      ..._.style
    },
    onKeyDown: v(_.onKeyDown, (u) => {
      const x = u.target.closest("[data-radix-menu-content]") === u.currentTarget, E = u.ctrlKey || u.altKey || u.metaKey, F = u.key.length === 1;
      x && (u.key === "Tab" && u.preventDefault(), !E && F && Ke(u.key));
      const ne = Y.current;
      if (u.target !== ne || !on.includes(u.key))
        return;
      u.preventDefault();
      const H = ie().filter(
        (A) => !A.disabled
      ).map(
        (A) => A.ref.current
      );
      ge.includes(u.key) && H.reverse(), Dn(H);
    }),
    onBlur: v(e.onBlur, (u) => {
      u.currentTarget.contains(u.target) || (window.clearTimeout(X.current), j.current = "");
    }),
    onPointerMove: v(e.onPointerMove, K((u) => {
      const C = u.target, x = ee.current !== u.clientX;
      if (u.currentTarget.contains(C) && x) {
        const E = u.clientX > ee.current ? "right" : "left";
        le.current = E, ee.current = u.clientX;
      }
    }))
  })))))));
}), Se = /* @__PURE__ */ g((e, t) => {
  const { __scopeMenu: n, ...o } = e;
  return /* @__PURE__ */ s(N.div, M({
    role: "group"
  }, o, {
    ref: t
  }));
}), vn = /* @__PURE__ */ g((e, t) => {
  const { __scopeMenu: n, ...o } = e;
  return /* @__PURE__ */ s(N.div, M({}, o, {
    ref: t
  }));
}), oe = "MenuItem", be = "menu.itemSelect", se = /* @__PURE__ */ g((e, t) => {
  const { disabled: n = !1, onSelect: o, ...a } = e, c = h(null), i = U(oe, e.__scopeMenu), $ = re(oe, e.__scopeMenu), p = G(t, c), m = h(!1), d = () => {
    const r = c.current;
    if (!n && r) {
      const l = new CustomEvent(be, {
        bubbles: !0,
        cancelable: !0
      });
      r.addEventListener(
        be,
        (f) => o == null ? void 0 : o(f),
        {
          once: !0
        }
      ), Ze(r, l), l.defaultPrevented ? m.current = !1 : i.onClose();
    }
  };
  return /* @__PURE__ */ s(Ee, M({}, a, {
    ref: p,
    disabled: n,
    onClick: v(e.onClick, d),
    onPointerDown: (r) => {
      var l;
      (l = e.onPointerDown) === null || l === void 0 || l.call(e, r), m.current = !0;
    },
    onPointerUp: v(e.onPointerUp, (r) => {
      var l;
      m.current || (l = r.currentTarget) === null || l === void 0 || l.click();
    }),
    onKeyDown: v(e.onKeyDown, (r) => {
      const l = $.searchRef.current !== "";
      n || l && r.key === " " || te.includes(r.key) && (r.currentTarget.click(), r.preventDefault());
    })
  }));
}), Ee = /* @__PURE__ */ g((e, t) => {
  const { __scopeMenu: n, disabled: o = !1, textValue: a, ...c } = e, i = re(oe, n), $ = he(n), p = h(null), m = G(t, p), [d, r] = D(!1), [l, f] = D("");
  return w(() => {
    const b = p.current;
    if (b) {
      var _;
      f(((_ = b.textContent) !== null && _ !== void 0 ? _ : "").trim());
    }
  }, [
    c.children
  ]), /* @__PURE__ */ s(L.ItemSlot, {
    scope: n,
    disabled: o,
    textValue: a ?? l
  }, /* @__PURE__ */ s(Je, M({
    asChild: !0
  }, $, {
    focusable: !o
  }), /* @__PURE__ */ s(N.div, M({
    role: "menuitem",
    "data-highlighted": d ? "" : void 0,
    "aria-disabled": o || void 0,
    "data-disabled": o ? "" : void 0
  }, c, {
    ref: m,
    onPointerMove: v(e.onPointerMove, K((b) => {
      o ? i.onItemLeave(b) : (i.onItemEnter(b), b.defaultPrevented || b.currentTarget.focus());
    })),
    onPointerLeave: v(e.onPointerLeave, K(
      (b) => i.onItemLeave(b)
    )),
    onFocus: v(
      e.onFocus,
      () => r(!0)
    ),
    onBlur: v(
      e.onBlur,
      () => r(!1)
    )
  }))));
}), Mn = /* @__PURE__ */ g((e, t) => {
  const { checked: n = !1, onCheckedChange: o, ...a } = e;
  return /* @__PURE__ */ s(we, {
    scope: e.__scopeMenu,
    checked: n
  }, /* @__PURE__ */ s(se, M({
    role: "menuitemcheckbox",
    "aria-checked": z(n) ? "mixed" : n
  }, a, {
    ref: t,
    "data-state": ue(n),
    onSelect: v(
      a.onSelect,
      () => o == null ? void 0 : o(z(n) ? !0 : !n),
      {
        checkForDefaultPrevented: !1
      }
    )
  })));
}), Cn = "MenuRadioGroup", [gn, hn] = T(Cn, {
  value: void 0,
  onValueChange: () => {
  }
}), _n = /* @__PURE__ */ g((e, t) => {
  const { value: n, onValueChange: o, ...a } = e, c = ce(o);
  return /* @__PURE__ */ s(gn, {
    scope: e.__scopeMenu,
    value: n,
    onValueChange: c
  }, /* @__PURE__ */ s(Se, M({}, a, {
    ref: t
  })));
}), xn = "MenuRadioItem", Pn = /* @__PURE__ */ g((e, t) => {
  const { value: n, ...o } = e, a = hn(xn, e.__scopeMenu), c = n === a.value;
  return /* @__PURE__ */ s(we, {
    scope: e.__scopeMenu,
    checked: c
  }, /* @__PURE__ */ s(se, M({
    role: "menuitemradio",
    "aria-checked": c
  }, o, {
    ref: t,
    "data-state": ue(c),
    onSelect: v(o.onSelect, () => {
      var i;
      return (i = a.onValueChange) === null || i === void 0 ? void 0 : i.call(a, n);
    }, {
      checkForDefaultPrevented: !1
    })
  })));
}), Re = "MenuItemIndicator", [we, In] = T(Re, {
  checked: !1
}), Sn = /* @__PURE__ */ g((e, t) => {
  const { __scopeMenu: n, forceMount: o, ...a } = e, c = In(Re, n);
  return /* @__PURE__ */ s(Z, {
    present: o || z(c.checked) || c.checked === !0
  }, /* @__PURE__ */ s(N.span, M({}, a, {
    ref: t,
    "data-state": ue(c.checked)
  })));
}), En = /* @__PURE__ */ g((e, t) => {
  const { __scopeMenu: n, ...o } = e;
  return /* @__PURE__ */ s(N.div, M({
    role: "separator",
    "aria-orientation": "horizontal"
  }, o, {
    ref: t
  }));
}), Te = "MenuSub", [Rn, ye] = T(Te), wn = (e) => {
  const { __scopeMenu: t, children: n, open: o = !1, onOpenChange: a } = e, c = S(Te, t), i = J(t), [$, p] = D(null), [m, d] = D(null), r = ce(a);
  return w(() => (c.open === !1 && r(!1), () => r(!1)), [
    c.open,
    r
  ]), /* @__PURE__ */ s(Me, i, /* @__PURE__ */ s(_e, {
    scope: t,
    open: o,
    onOpenChange: r,
    content: m,
    onContentChange: d
  }, /* @__PURE__ */ s(Rn, {
    scope: t,
    contentId: me(),
    triggerId: me(),
    trigger: $,
    onTriggerChange: p
  }, n)));
}, W = "MenuSubTrigger", Tn = /* @__PURE__ */ g((e, t) => {
  const n = S(W, e.__scopeMenu), o = U(W, e.__scopeMenu), a = ye(W, e.__scopeMenu), c = re(W, e.__scopeMenu), i = h(null), { pointerGraceTimerRef: $, onPointerGraceIntentChange: p } = c, m = {
    __scopeMenu: e.__scopeMenu
  }, d = R(() => {
    i.current && window.clearTimeout(i.current), i.current = null;
  }, []);
  return w(
    () => d,
    [
      d
    ]
  ), w(() => {
    const r = $.current;
    return () => {
      window.clearTimeout(r), p(null);
    };
  }, [
    $,
    p
  ]), /* @__PURE__ */ s(xe, M({
    asChild: !0
  }, m), /* @__PURE__ */ s(Ee, M({
    id: a.triggerId,
    "aria-haspopup": "menu",
    "aria-expanded": n.open,
    "aria-controls": a.contentId,
    "data-state": Oe(n.open)
  }, e, {
    ref: Ue(t, a.onTriggerChange),
    onClick: (r) => {
      var l;
      (l = e.onClick) === null || l === void 0 || l.call(e, r), !(e.disabled || r.defaultPrevented) && (r.currentTarget.focus(), n.open || n.onOpenChange(!0));
    },
    onPointerMove: v(e.onPointerMove, K((r) => {
      c.onItemEnter(r), !r.defaultPrevented && !e.disabled && !n.open && !i.current && (c.onPointerGraceIntentChange(null), i.current = window.setTimeout(() => {
        n.onOpenChange(!0), d();
      }, 100));
    })),
    onPointerLeave: v(e.onPointerLeave, K((r) => {
      var l;
      d();
      const f = (l = n.content) === null || l === void 0 ? void 0 : l.getBoundingClientRect();
      if (f) {
        var b;
        const _ = (b = n.content) === null || b === void 0 ? void 0 : b.dataset.side, y = _ === "right", k = y ? -5 : 5, V = f[y ? "left" : "right"], B = f[y ? "right" : "left"];
        c.onPointerGraceIntentChange({
          area: [
            // consistently within polygon bounds
            {
              x: r.clientX + k,
              y: r.clientY
            },
            {
              x: V,
              y: f.top
            },
            {
              x: B,
              y: f.top
            },
            {
              x: B,
              y: f.bottom
            },
            {
              x: V,
              y: f.bottom
            }
          ],
          side: _
        }), window.clearTimeout($.current), $.current = window.setTimeout(
          () => c.onPointerGraceIntentChange(null),
          300
        );
      } else {
        if (c.onTriggerLeave(r), r.defaultPrevented)
          return;
        c.onPointerGraceIntentChange(null);
      }
    })),
    onKeyDown: v(e.onKeyDown, (r) => {
      const l = c.searchRef.current !== "";
      if (!(e.disabled || l && r.key === " ") && cn[o.dir].includes(r.key)) {
        var f;
        n.onOpenChange(!0), (f = n.content) === null || f === void 0 || f.focus(), r.preventDefault();
      }
    })
  })));
}), yn = "MenuSubContent", On = /* @__PURE__ */ g((e, t) => {
  const n = Ie(P, e.__scopeMenu), { forceMount: o = n.forceMount, ...a } = e, c = S(P, e.__scopeMenu), i = U(P, e.__scopeMenu), $ = ye(yn, e.__scopeMenu), p = h(null), m = G(t, p);
  return /* @__PURE__ */ s(L.Provider, {
    scope: e.__scopeMenu
  }, /* @__PURE__ */ s(Z, {
    present: o || c.open
  }, /* @__PURE__ */ s(L.Slot, {
    scope: e.__scopeMenu
  }, /* @__PURE__ */ s(ae, M({
    id: $.contentId,
    "aria-labelledby": $.triggerId
  }, a, {
    ref: m,
    align: "start",
    side: i.dir === "rtl" ? "left" : "right",
    disableOutsidePointerEvents: !1,
    disableOutsideScroll: !1,
    trapFocus: !1,
    onOpenAutoFocus: (d) => {
      var r;
      i.isUsingKeyboardRef.current && ((r = p.current) === null || r === void 0 || r.focus()), d.preventDefault();
    },
    onCloseAutoFocus: (d) => d.preventDefault(),
    onFocusOutside: v(e.onFocusOutside, (d) => {
      d.target !== $.trigger && c.onOpenChange(!1);
    }),
    onEscapeKeyDown: v(e.onEscapeKeyDown, (d) => {
      i.onClose(), d.preventDefault();
    }),
    onKeyDown: v(e.onKeyDown, (d) => {
      const r = d.currentTarget.contains(d.target), l = rn[i.dir].includes(d.key);
      if (r && l) {
        var f;
        c.onOpenChange(!1), (f = $.trigger) === null || f === void 0 || f.focus(), d.preventDefault();
      }
    })
  })))));
});
function Oe(e) {
  return e ? "open" : "closed";
}
function z(e) {
  return e === "indeterminate";
}
function ue(e) {
  return z(e) ? "indeterminate" : e ? "checked" : "unchecked";
}
function Dn(e) {
  const t = document.activeElement;
  for (const n of e)
    if (n === t || (n.focus(), document.activeElement !== t))
      return;
}
function kn(e, t) {
  return e.map(
    (n, o) => e[(t + o) % e.length]
  );
}
function Fn(e, t, n) {
  const a = t.length > 1 && Array.from(t).every(
    (m) => m === t[0]
  ) ? t[0] : t, c = n ? e.indexOf(n) : -1;
  let i = kn(e, Math.max(c, 0));
  a.length === 1 && (i = i.filter(
    (m) => m !== n
  ));
  const p = i.find(
    (m) => m.toLowerCase().startsWith(a.toLowerCase())
  );
  return p !== n ? p : void 0;
}
function An(e, t) {
  const { x: n, y: o } = e;
  let a = !1;
  for (let c = 0, i = t.length - 1; c < t.length; i = c++) {
    const $ = t[c].x, p = t[c].y, m = t[i].x, d = t[i].y;
    p > o != d > o && n < (m - $) * (o - p) / (d - p) + $ && (a = !a);
  }
  return a;
}
function Ln(e, t) {
  if (!t)
    return !1;
  const n = {
    x: e.clientX,
    y: e.clientY
  };
  return An(n, t);
}
function K(e) {
  return (t) => t.pointerType === "mouse" ? e(t) : void 0;
}
const rt = dn, at = xe, st = fn, ut = pn, it = Se, dt = vn, lt = se, ft = Mn, $t = _n, pt = Pn, mt = Sn, bt = En, vt = wn, Mt = Tn, Ct = On;
export {
  at as Anchor,
  ft as CheckboxItem,
  ut as Content,
  it as Group,
  lt as Item,
  mt as ItemIndicator,
  dt as Label,
  dn as Menu,
  xe as MenuAnchor,
  Mn as MenuCheckboxItem,
  pn as MenuContent,
  Se as MenuGroup,
  se as MenuItem,
  Sn as MenuItemIndicator,
  vn as MenuLabel,
  fn as MenuPortal,
  _n as MenuRadioGroup,
  Pn as MenuRadioItem,
  En as MenuSeparator,
  wn as MenuSub,
  On as MenuSubContent,
  Tn as MenuSubTrigger,
  st as Portal,
  $t as RadioGroup,
  pt as RadioItem,
  rt as Root,
  bt as Separator,
  vt as Sub,
  Ct as SubContent,
  Mt as SubTrigger,
  ct as createMenuScope
};
