import M from "../../../@babel/runtime/helpers/esm/extends.js";
import { createElement as r, forwardRef as h, useRef as v, useEffect as k, useState as K, useCallback as w, Fragment as Ce } from "react";
import { composeEventHandlers as m } from "../../primitive/dist/index.js";
import { createCollection as he } from "../../react-collection/dist/index.js";
import { useComposedRefs as N } from "../../react-compose-refs/dist/index.js";
import { createContextScope as Pe } from "../../react-context/dist/index.js";
import { useDirection as Ee } from "../../react-direction/dist/index.js";
import { DismissableLayer as ge } from "../../react-dismissable-layer/dist/index.js";
import { useFocusGuards as Se } from "../../react-focus-guards/dist/index.js";
import { FocusScope as Ie } from "../../react-focus-scope/dist/index.js";
import { createPopperScope as re, Content as Re, Root as we, Anchor as De } from "../../react-popper/dist/index.js";
import { Portal as ye } from "../../react-portal/dist/index.js";
import { Presence as ae } from "../../react-presence/dist/index.js";
import { Primitive as W, dispatchDiscreteCustomEvent as Oe } from "../../react-primitive/dist/index.js";
import { createRovingFocusGroupScope as se, Root as Te, Item as Fe } from "../../react-roving-focus/dist/index.js";
import { Slot as Ae } from "../../react-slot/dist/index.js";
import { useCallbackRef as Le } from "../../react-use-callback-ref/dist/index.js";
import { hideOthers as Ke } from "../../../aria-hidden/dist/es2015/index.js";
import ke from "../../../react-remove-scroll/dist/es2015/Combination.js";
const Ne = [
  "Enter",
  " "
], Ge = [
  "ArrowDown",
  "PageUp",
  "Home"
], ie = [
  "ArrowUp",
  "PageDown",
  "End"
], Ue = [
  ...Ge,
  ...ie
], G = "Menu", [B, Ve, Xe] = he(G), [I, Tt] = Pe(G, [
  Xe,
  re,
  se
]), z = re(), ue = se(), [Ye, O] = I(G), [Be, Z] = I(G), je = (e) => {
  const { __scopeMenu: t, open: n = !1, children: c, dir: a, onOpenChange: s, modal: u = !0 } = e, x = z(t), [p, l] = K(null), b = v(!1), i = Le(s), d = Ee(a);
  return k(() => {
    const P = () => {
      b.current = !0, document.addEventListener("pointerdown", $, {
        capture: !0,
        once: !0
      }), document.addEventListener("pointermove", $, {
        capture: !0,
        once: !0
      });
    }, $ = () => b.current = !1;
    return document.addEventListener("keydown", P, {
      capture: !0
    }), () => {
      document.removeEventListener("keydown", P, {
        capture: !0
      }), document.removeEventListener("pointerdown", $, {
        capture: !0
      }), document.removeEventListener("pointermove", $, {
        capture: !0
      });
    };
  }, []), /* @__PURE__ */ r(we, x, /* @__PURE__ */ r(Ye, {
    scope: t,
    open: n,
    onOpenChange: i,
    content: p,
    onContentChange: l
  }, /* @__PURE__ */ r(Be, {
    scope: t,
    onClose: w(
      () => i(!1),
      [
        i
      ]
    ),
    isUsingKeyboardRef: b,
    dir: d,
    modal: u
  }, c)));
}, He = /* @__PURE__ */ h((e, t) => {
  const { __scopeMenu: n, ...c } = e, a = z(n);
  return /* @__PURE__ */ r(De, M({}, a, c, {
    ref: t
  }));
}), de = "MenuPortal", [We, ze] = I(de, {
  forceMount: void 0
}), Ze = (e) => {
  const { __scopeMenu: t, forceMount: n, children: c, container: a } = e, s = O(de, t);
  return /* @__PURE__ */ r(We, {
    scope: t,
    forceMount: n
  }, /* @__PURE__ */ r(ae, {
    present: n || s.open
  }, /* @__PURE__ */ r(ye, {
    asChild: !0,
    container: a
  }, c)));
}, g = "MenuContent", [qe, fe] = I(g), Je = /* @__PURE__ */ h((e, t) => {
  const n = ze(g, e.__scopeMenu), { forceMount: c = n.forceMount, ...a } = e, s = O(g, e.__scopeMenu), u = Z(g, e.__scopeMenu);
  return /* @__PURE__ */ r(B.Provider, {
    scope: e.__scopeMenu
  }, /* @__PURE__ */ r(ae, {
    present: c || s.open
  }, /* @__PURE__ */ r(B.Slot, {
    scope: e.__scopeMenu
  }, u.modal ? /* @__PURE__ */ r(Qe, M({}, a, {
    ref: t
  })) : /* @__PURE__ */ r(et, M({}, a, {
    ref: t
  })))));
}), Qe = /* @__PURE__ */ h((e, t) => {
  const n = O(g, e.__scopeMenu), c = v(null), a = N(t, c);
  return k(() => {
    const s = c.current;
    if (s)
      return Ke(s);
  }, []), /* @__PURE__ */ r(le, M({}, e, {
    ref: a,
    trapFocus: n.open,
    disableOutsidePointerEvents: n.open,
    disableOutsideScroll: !0,
    onFocusOutside: m(
      e.onFocusOutside,
      (s) => s.preventDefault(),
      {
        checkForDefaultPrevented: !1
      }
    ),
    onDismiss: () => n.onOpenChange(!1)
  }));
}), et = /* @__PURE__ */ h((e, t) => {
  const n = O(g, e.__scopeMenu);
  return /* @__PURE__ */ r(le, M({}, e, {
    ref: t,
    trapFocus: !1,
    disableOutsidePointerEvents: !1,
    disableOutsideScroll: !1,
    onDismiss: () => n.onOpenChange(!1)
  }));
}), le = /* @__PURE__ */ h((e, t) => {
  const { __scopeMenu: n, loop: c = !1, trapFocus: a, onOpenAutoFocus: s, onCloseAutoFocus: u, disableOutsidePointerEvents: x, onEntryFocus: p, onEscapeKeyDown: l, onPointerDownOutside: b, onFocusOutside: i, onInteractOutside: d, onDismiss: P, disableOutsideScroll: $, ...E } = e, q = O(g, n), U = Z(g, n), $e = z(n), pe = ue(n), J = Ve(n), [me, Q] = K(null), T = v(null), be = N(t, T, q.onContentChange), F = v(0), A = v(""), ve = v(0), V = v(null), ee = v("right"), X = v(0), Me = $ ? ke : Ce, xe = $ ? {
    as: Ae,
    allowPinchZoom: !0
  } : void 0, _e = (o) => {
    var f, _;
    const S = A.current + o, D = J().filter(
      (C) => !C.disabled
    ), Y = document.activeElement, te = (f = D.find(
      (C) => C.ref.current === Y
    )) === null || f === void 0 ? void 0 : f.textValue, L = D.map(
      (C) => C.textValue
    ), y = ft(L, S, te), ne = (_ = D.find(
      (C) => C.textValue === y
    )) === null || _ === void 0 ? void 0 : _.ref.current;
    (function C(oe) {
      A.current = oe, window.clearTimeout(F.current), oe !== "" && (F.current = window.setTimeout(
        () => C(""),
        1e3
      ));
    })(S), ne && setTimeout(
      () => ne.focus()
    );
  };
  k(() => () => window.clearTimeout(F.current), []), Se();
  const R = w((o) => {
    var f, _;
    return ee.current === ((f = V.current) === null || f === void 0 ? void 0 : f.side) && $t(o, (_ = V.current) === null || _ === void 0 ? void 0 : _.area);
  }, []);
  return /* @__PURE__ */ r(qe, {
    scope: n,
    searchRef: A,
    onItemEnter: w((o) => {
      R(o) && o.preventDefault();
    }, [
      R
    ]),
    onItemLeave: w((o) => {
      var f;
      R(o) || ((f = T.current) === null || f === void 0 || f.focus(), Q(null));
    }, [
      R
    ]),
    onTriggerLeave: w((o) => {
      R(o) && o.preventDefault();
    }, [
      R
    ]),
    pointerGraceTimerRef: ve,
    onPointerGraceIntentChange: w((o) => {
      V.current = o;
    }, [])
  }, /* @__PURE__ */ r(Me, xe, /* @__PURE__ */ r(Ie, {
    asChild: !0,
    trapped: a,
    onMountAutoFocus: m(s, (o) => {
      var f;
      o.preventDefault(), (f = T.current) === null || f === void 0 || f.focus();
    }),
    onUnmountAutoFocus: u
  }, /* @__PURE__ */ r(ge, {
    asChild: !0,
    disableOutsidePointerEvents: x,
    onEscapeKeyDown: l,
    onPointerDownOutside: b,
    onFocusOutside: i,
    onInteractOutside: d,
    onDismiss: P
  }, /* @__PURE__ */ r(Te, M({
    asChild: !0
  }, pe, {
    dir: U.dir,
    orientation: "vertical",
    loop: c,
    currentTabStopId: me,
    onCurrentTabStopIdChange: Q,
    onEntryFocus: m(p, (o) => {
      U.isUsingKeyboardRef.current || o.preventDefault();
    })
  }), /* @__PURE__ */ r(Re, M({
    role: "menu",
    "aria-orientation": "vertical",
    "data-state": it(q.open),
    "data-radix-menu-content": "",
    dir: U.dir
  }, $e, E, {
    ref: be,
    style: {
      outline: "none",
      ...E.style
    },
    onKeyDown: m(E.onKeyDown, (o) => {
      const _ = o.target.closest("[data-radix-menu-content]") === o.currentTarget, S = o.ctrlKey || o.altKey || o.metaKey, D = o.key.length === 1;
      _ && (o.key === "Tab" && o.preventDefault(), !S && D && _e(o.key));
      const Y = T.current;
      if (o.target !== Y || !Ue.includes(o.key))
        return;
      o.preventDefault();
      const L = J().filter(
        (y) => !y.disabled
      ).map(
        (y) => y.ref.current
      );
      ie.includes(o.key) && L.reverse(), ut(L);
    }),
    onBlur: m(e.onBlur, (o) => {
      o.currentTarget.contains(o.target) || (window.clearTimeout(F.current), A.current = "");
    }),
    onPointerMove: m(e.onPointerMove, H((o) => {
      const f = o.target, _ = X.current !== o.clientX;
      if (o.currentTarget.contains(f) && _) {
        const S = o.clientX > X.current ? "right" : "left";
        ee.current = S, X.current = o.clientX;
      }
    }))
  })))))));
}), tt = /* @__PURE__ */ h((e, t) => {
  const { __scopeMenu: n, ...c } = e;
  return /* @__PURE__ */ r(W.div, M({}, c, {
    ref: t
  }));
}), j = "MenuItem", ce = "menu.itemSelect", nt = /* @__PURE__ */ h((e, t) => {
  const { disabled: n = !1, onSelect: c, ...a } = e, s = v(null), u = Z(j, e.__scopeMenu), x = fe(j, e.__scopeMenu), p = N(t, s), l = v(!1), b = () => {
    const i = s.current;
    if (!n && i) {
      const d = new CustomEvent(ce, {
        bubbles: !0,
        cancelable: !0
      });
      i.addEventListener(
        ce,
        (P) => c == null ? void 0 : c(P),
        {
          once: !0
        }
      ), Oe(i, d), d.defaultPrevented ? l.current = !1 : u.onClose();
    }
  };
  return /* @__PURE__ */ r(ot, M({}, a, {
    ref: p,
    disabled: n,
    onClick: m(e.onClick, b),
    onPointerDown: (i) => {
      var d;
      (d = e.onPointerDown) === null || d === void 0 || d.call(e, i), l.current = !0;
    },
    onPointerUp: m(e.onPointerUp, (i) => {
      var d;
      l.current || (d = i.currentTarget) === null || d === void 0 || d.click();
    }),
    onKeyDown: m(e.onKeyDown, (i) => {
      const d = x.searchRef.current !== "";
      n || d && i.key === " " || Ne.includes(i.key) && (i.currentTarget.click(), i.preventDefault());
    })
  }));
}), ot = /* @__PURE__ */ h((e, t) => {
  const { __scopeMenu: n, disabled: c = !1, textValue: a, ...s } = e, u = fe(j, n), x = ue(n), p = v(null), l = N(t, p), [b, i] = K(!1), [d, P] = K("");
  return k(() => {
    const $ = p.current;
    if ($) {
      var E;
      P(((E = $.textContent) !== null && E !== void 0 ? E : "").trim());
    }
  }, [
    s.children
  ]), /* @__PURE__ */ r(B.ItemSlot, {
    scope: n,
    disabled: c,
    textValue: a ?? d
  }, /* @__PURE__ */ r(Fe, M({
    asChild: !0
  }, x, {
    focusable: !c
  }), /* @__PURE__ */ r(W.div, M({
    role: "menuitem",
    "data-highlighted": b ? "" : void 0,
    "aria-disabled": c || void 0,
    "data-disabled": c ? "" : void 0
  }, s, {
    ref: l,
    onPointerMove: m(e.onPointerMove, H(($) => {
      c ? u.onItemLeave($) : (u.onItemEnter($), $.defaultPrevented || $.currentTarget.focus());
    })),
    onPointerLeave: m(e.onPointerLeave, H(
      ($) => u.onItemLeave($)
    )),
    onFocus: m(
      e.onFocus,
      () => i(!0)
    ),
    onBlur: m(
      e.onBlur,
      () => i(!1)
    )
  }))));
}), ct = "MenuRadioGroup";
I(ct, {
  value: void 0,
  onValueChange: () => {
  }
});
const rt = "MenuItemIndicator";
I(rt, {
  checked: !1
});
const at = /* @__PURE__ */ h((e, t) => {
  const { __scopeMenu: n, ...c } = e;
  return /* @__PURE__ */ r(W.div, M({
    role: "separator",
    "aria-orientation": "horizontal"
  }, c, {
    ref: t
  }));
}), st = "MenuSub";
I(st);
function it(e) {
  return e ? "open" : "closed";
}
function ut(e) {
  const t = document.activeElement;
  for (const n of e)
    if (n === t || (n.focus(), document.activeElement !== t))
      return;
}
function dt(e, t) {
  return e.map(
    (n, c) => e[(t + c) % e.length]
  );
}
function ft(e, t, n) {
  const a = t.length > 1 && Array.from(t).every(
    (l) => l === t[0]
  ) ? t[0] : t, s = n ? e.indexOf(n) : -1;
  let u = dt(e, Math.max(s, 0));
  a.length === 1 && (u = u.filter(
    (l) => l !== n
  ));
  const p = u.find(
    (l) => l.toLowerCase().startsWith(a.toLowerCase())
  );
  return p !== n ? p : void 0;
}
function lt(e, t) {
  const { x: n, y: c } = e;
  let a = !1;
  for (let s = 0, u = t.length - 1; s < t.length; u = s++) {
    const x = t[s].x, p = t[s].y, l = t[u].x, b = t[u].y;
    p > c != b > c && n < (l - x) * (c - p) / (b - p) + x && (a = !a);
  }
  return a;
}
function $t(e, t) {
  if (!t)
    return !1;
  const n = {
    x: e.clientX,
    y: e.clientY
  };
  return lt(n, t);
}
function H(e) {
  return (t) => t.pointerType === "mouse" ? e(t) : void 0;
}
const Ft = je, At = He, Lt = Ze, Kt = Je, kt = tt, Nt = nt, Gt = at;
export {
  At as Anchor,
  Kt as Content,
  Nt as Item,
  kt as Label,
  je as Menu,
  He as MenuAnchor,
  Je as MenuContent,
  nt as MenuItem,
  tt as MenuLabel,
  Ze as MenuPortal,
  at as MenuSeparator,
  Lt as Portal,
  Ft as Root,
  Gt as Separator,
  Tt as createMenuScope
};
