import T from "./index-extends.js";
import H from "./index-objectWithoutPropertiesLoose.js";
import * as c from "react";
import { isFragment as O } from "react-is";
import d from "./index-_virtual16.js";
import { jsx as j } from "react/jsx-runtime";
import _ from "./index-useEnhancedEffect2.js";
import A from "./index-getScrollbarSize.js";
import I from "./index-ownerDocument2.js";
import P from "./index-useForkRef.js";
import V from "./index-List2.js";
const W = ["actions", "autoFocus", "autoFocusItem", "children", "className", "disabledItemsFocusable", "disableListWrap", "onKeyDown", "variant"];
function v(n, t, r) {
  return n === t ? n.firstChild : t && t.nextElementSibling ? t.nextElementSibling : r ? null : n.firstChild;
}
function k(n, t, r) {
  return n === t ? r ? n.firstChild : n.lastChild : t && t.previousElementSibling ? t.previousElementSibling : r ? null : n.lastChild;
}
function C(n, t) {
  if (t === void 0)
    return !0;
  let r = n.innerText;
  return r === void 0 && (r = n.textContent), r = r.trim().toLowerCase(), r.length === 0 ? !1 : t.repeating ? r[0] === t.keys[0] : r.indexOf(t.keys.join("")) === 0;
}
function b(n, t, r, y, h, w) {
  let p = !1, u = h(n, t, t ? r : !1);
  for (; u; ) {
    if (u === n.firstChild) {
      if (p)
        return !1;
      p = !0;
    }
    const m = y ? !1 : u.disabled || u.getAttribute("aria-disabled") === "true";
    if (!u.hasAttribute("tabindex") || !C(u, w) || m)
      u = h(n, u, r);
    else
      return u.focus(), !0;
  }
  return !1;
}
const z = /* @__PURE__ */ c.forwardRef(function(t, r) {
  const {
    // private
    // eslint-disable-next-line react/prop-types
    actions: y,
    autoFocus: h = !1,
    autoFocusItem: w = !1,
    children: p,
    className: u,
    disabledItemsFocusable: m = !1,
    disableListWrap: g = !1,
    onKeyDown: F,
    variant: x = "selectedMenu"
  } = t, M = H(t, W), a = c.useRef(null), S = c.useRef({
    keys: [],
    repeating: !0,
    previousKeyMatched: !0,
    lastTime: null
  });
  _(() => {
    h && a.current.focus();
  }, [h]), c.useImperativeHandle(y, () => ({
    adjustStyleForScrollbar: (e, {
      direction: o
    }) => {
      const l = !a.current.style.width;
      if (e.clientHeight < a.current.clientHeight && l) {
        const f = `${A(I(e))}px`;
        a.current.style[o === "rtl" ? "paddingLeft" : "paddingRight"] = f, a.current.style.width = `calc(100% + ${f})`;
      }
      return a.current;
    }
  }), []);
  const K = (e) => {
    const o = a.current, l = e.key, f = I(o).activeElement;
    if (l === "ArrowDown")
      e.preventDefault(), b(o, f, g, m, v);
    else if (l === "ArrowUp")
      e.preventDefault(), b(o, f, g, m, k);
    else if (l === "Home")
      e.preventDefault(), b(o, null, g, m, v);
    else if (l === "End")
      e.preventDefault(), b(o, null, g, m, k);
    else if (l.length === 1) {
      const s = S.current, D = l.toLowerCase(), E = performance.now();
      s.keys.length > 0 && (E - s.lastTime > 500 ? (s.keys = [], s.repeating = !0, s.previousKeyMatched = !0) : s.repeating && D !== s.keys[0] && (s.repeating = !1)), s.lastTime = E, s.keys.push(D);
      const N = f && !s.repeating && C(f, s);
      s.previousKeyMatched && (N || b(o, f, !1, m, v, s)) ? e.preventDefault() : s.previousKeyMatched = !1;
    }
    F && F(e);
  }, L = P(a, r);
  let i = -1;
  c.Children.forEach(p, (e, o) => {
    if (!/* @__PURE__ */ c.isValidElement(e)) {
      i === o && (i += 1, i >= p.length && (i = -1));
      return;
    }
    process.env.NODE_ENV !== "production" && O(e), e.props.disabled || (x === "selectedMenu" && e.props.selected || i === -1) && (i = o), i === o && (e.props.disabled || e.props.muiSkipListHighlight || e.type.muiSkipListHighlight) && (i += 1, i >= p.length && (i = -1));
  });
  const R = c.Children.map(p, (e, o) => {
    if (o === i) {
      const l = {};
      return w && (l.autoFocus = !0), e.props.tabIndex === void 0 && x === "selectedMenu" && (l.tabIndex = 0), /* @__PURE__ */ c.cloneElement(e, l);
    }
    return e;
  });
  return /* @__PURE__ */ j(V, T({
    role: "menu",
    ref: L,
    className: u,
    onKeyDown: K,
    tabIndex: h ? 0 : -1
  }, M, {
    children: R
  }));
});
process.env.NODE_ENV !== "production" && (z.propTypes = {
  // ┌────────────────────────────── Warning ──────────────────────────────┐
  // │ These PropTypes are generated from the TypeScript type definitions. │
  // │    To update them, edit the d.ts file and run `pnpm proptypes`.     │
  // └─────────────────────────────────────────────────────────────────────┘
  /**
   * If `true`, will focus the `[role="menu"]` container and move into tab order.
   * @default false
   */
  autoFocus: d.bool,
  /**
   * If `true`, will focus the first menuitem if `variant="menu"` or selected item
   * if `variant="selectedMenu"`.
   * @default false
   */
  autoFocusItem: d.bool,
  /**
   * MenuList contents, normally `MenuItem`s.
   */
  children: d.node,
  /**
   * @ignore
   */
  className: d.string,
  /**
   * If `true`, will allow focus on disabled items.
   * @default false
   */
  disabledItemsFocusable: d.bool,
  /**
   * If `true`, the menu items will not wrap focus.
   * @default false
   */
  disableListWrap: d.bool,
  /**
   * @ignore
   */
  onKeyDown: d.func,
  /**
   * The variant to use. Use `menu` to prevent selected items from impacting the initial focus
   * and the vertical alignment relative to the anchor element.
   * @default 'selectedMenu'
   */
  variant: d.oneOf(["menu", "selectedMenu"])
});
export {
  z as default
};
