import { jsx as o, jsxs as h } from "react/jsx-runtime";
import { t as b } from "../../classix-DG18itHa.js";
import { useRef as x, useCallback as D, useEffect as _ } from "react";
import { Button as y } from "../button/index.js";
import { Icon as m } from "../icon/index.js";
import { List as k } from "../list/index.js";
import { Popover as w } from "../popover/index.js";
import '../../assets/index21.css';const A = "_list_1x03m_1", B = "_item_1x03m_13", C = "_danger_1x03m_41", f = {
  list: A,
  item: B,
  danger: C
};
function E(s) {
  return s.filter((t) => !!(t && !t.disabled));
}
const I = ({
  close: s,
  initialFocus: t,
  items: u,
  menuAccessibleLabel: c,
  onItemSelect: a
}) => {
  const l = x([]), r = D((e) => {
    const n = E(l.current);
    if (n.length === 0)
      return;
    if (e === "first") {
      n[0].focus();
      return;
    }
    if (e === "last") {
      n[n.length - 1].focus();
      return;
    }
    const i = n.indexOf(document.activeElement), g = e === "next" ? (i + 1) % n.length : (i - 1 + n.length) % n.length;
    n[g].focus();
  }, []);
  _(() => {
    r(t ?? "first");
  }, [r, t]);
  const d = (e) => {
    if (e.key === "ArrowDown") {
      e.preventDefault(), r("next");
      return;
    }
    if (e.key === "ArrowUp") {
      e.preventDefault(), r("previous");
      return;
    }
    if (e.key === "Home") {
      e.preventDefault(), r("first");
      return;
    }
    e.key === "End" && (e.preventDefault(), r("last"));
  }, p = (e) => {
    var n;
    (n = e.onSelect) == null || n.call(e), a == null || a(e), s();
  };
  return /* @__PURE__ */ o(k, { "aria-label": c, className: f.list, onKeyDown: d, role: "menu", "data-component": "menupanel", children: u.map((e, n) => /* @__PURE__ */ o("li", { role: "none", children: /* @__PURE__ */ h(y, { "aria-label": e.accessibleLabel, className: b(f.item, e.isDanger && f.danger), disabled: e.disabled, onClick: () => p(e), ref: (i) => {
    l.current[n] = i;
  }, role: "menuitem", variant: "minimal", children: [
    e.icon && /* @__PURE__ */ o(m, { gap: "right", name: e.icon, size: "small", variant: e.isDanger ? "danger" : "default" }),
    /* @__PURE__ */ o("span", { children: e.label })
  ] }) }, e.id ?? n)) });
}, L = ({
  items: s,
  menuAccessibleLabel: t = "Menu",
  onItemSelect: u,
  position: c = "left",
  triggerAccessibleLabel: a = "Open menu"
}) => /* @__PURE__ */ o(w, { position: c, triggerAccessibleLabel: a, triggerAriaHasPopup: "menu", triggerButtonContent: /* @__PURE__ */ o(m, { name: "dots", size: "medium" }), "data-component": "menu", children: ({
  close: l,
  initialFocus: r
}) => /* @__PURE__ */ o(I, { close: l, initialFocus: r, items: s, menuAccessibleLabel: t, onItemSelect: u }) });
export {
  L as Menu
};
//# sourceMappingURL=index.js.map
