import Vt from "../../@babel/runtime/helpers/esm/extends.mjs";
import Re from "../../@babel/runtime/helpers/esm/defineProperty.mjs";
import Pe from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import Lt from "../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import l from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import Ft from "../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import Tt from "../../classnames/index.mjs";
import T from "../../rc-overflow/es/Overflow.mjs";
import U from "../../rc-util/es/hooks/useMergedState.mjs";
import Ut from "../../rc-util/es/isEqual.mjs";
import { warningOnce as zt } from "../../rc-util/es/warning.mjs";
import * as n from "react";
import { useImperativeHandle as jt } from "react";
import { r as Wt } from "../../react-dom/index.mjs";
import { IdContext as Yt } from "./context/IdContext.mjs";
import Oe from "./context/MenuContext.mjs";
import { PathUserContext as qt, PathRegisterContext as Ht } from "./context/PathContext.mjs";
import Bt from "./context/PrivateContext.mjs";
import { refreshElements as Gt, getFocusableElements as Jt, useAccessibility as Qt } from "./hooks/useAccessibility.mjs";
import Xt, { OVERFLOW_KEY as Zt } from "./hooks/useKeyRecords.mjs";
import h from "./hooks/useMemoCallback.mjs";
import en from "./hooks/useUUID.mjs";
import tn from "./MenuItem.mjs";
import nn from "./SubMenu/index.mjs";
import { parseItems as rn } from "./utils/nodeUtil.mjs";
import { warnItemProp as on } from "./utils/warnUtil.mjs";
var an = ["prefixCls", "rootClassName", "style", "className", "tabIndex", "items", "children", "direction", "id", "mode", "inlineCollapsed", "disabled", "disabledOverflow", "subMenuOpenDelay", "subMenuCloseDelay", "forceSubMenuRender", "defaultOpenKeys", "openKeys", "activeKey", "defaultActiveFirst", "selectable", "multiple", "defaultSelectedKeys", "selectedKeys", "onSelect", "onDeselect", "inlineIndent", "motion", "defaultMotions", "triggerSubMenuAction", "builtinPlacements", "itemIcon", "expandIcon", "overflowedIndicator", "overflowedIndicatorPopupClassName", "getPopupContainer", "onClick", "onOpenChange", "onKeyDown", "openAnimation", "openTransitionName", "_internalRenderMenuItem", "_internalRenderSubMenuItem"], m = [], An = /* @__PURE__ */ n.forwardRef(function(xe, Ee) {
  var _, g, e = xe, z = e.prefixCls, c = z === void 0 ? "rc-menu" : z, j = e.rootClassName, we = e.style, Ae = e.className, W = e.tabIndex, De = W === void 0 ? 0 : W, Y = e.items, q = e.children, H = e.direction, B = e.id, G = e.mode, y = G === void 0 ? "vertical" : G, K = e.inlineCollapsed, Ne = e.disabled, R = e.disabledOverflow, J = e.subMenuOpenDelay, $e = J === void 0 ? 0.1 : J, Q = e.subMenuCloseDelay, ke = Q === void 0 ? 0.1 : Q, Ve = e.forceSubMenuRender, Le = e.defaultOpenKeys, Fe = e.openKeys, X = e.activeKey, Te = e.defaultActiveFirst, Z = e.selectable, Ue = Z === void 0 ? !0 : Z, ee = e.multiple, te = ee === void 0 ? !1 : ee, ze = e.defaultSelectedKeys, je = e.selectedKeys, P = e.onSelect, O = e.onDeselect, ne = e.inlineIndent, We = ne === void 0 ? 24 : ne, Ye = e.motion, qe = e.defaultMotions, re = e.triggerSubMenuAction, He = re === void 0 ? "hover" : re, Be = e.builtinPlacements, Ge = e.itemIcon, Je = e.expandIcon, ie = e.overflowedIndicator, Qe = ie === void 0 ? "..." : ie, Xe = e.overflowedIndicatorPopupClassName, Ze = e.getPopupContainer, x = e.onClick, E = e.onOpenChange, et = e.onKeyDown, tt = e.openAnimation, nt = e.openTransitionName, oe = e._internalRenderMenuItem, ae = e._internalRenderSubMenuItem, rt = Ft(e, an), s = n.useMemo(function() {
    return rn(q, Y, m);
  }, [q, Y]), it = n.useState(!1), le = l(it, 2), se = le[0], ot = le[1], b = n.useRef(), w = en(B), A = H === "rtl";
  process.env.NODE_ENV !== "production" && zt(!tt && !nt, "`openAnimation` and `openTransitionName` is removed. Please use `motion` or `defaultMotion` instead.");
  var at = U(Le, {
    value: Fe,
    postState: function(t) {
      return t || m;
    }
  }), ue = l(at, 2), u = ue[0], de = ue[1], D = function(t) {
    var r = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : !1;
    function o() {
      de(t), E == null || E(t);
    }
    r ? Wt.flushSync(o) : o();
  }, lt = n.useState(u), ce = l(lt, 2), st = ce[0], ut = ce[1], N = n.useRef(!1), dt = n.useMemo(function() {
    return (y === "inline" || y === "vertical") && K ? ["vertical", K] : [y, !1];
  }, [y, K]), fe = l(dt, 2), I = fe[0], $ = fe[1], me = I === "inline", ct = n.useState(I), pe = l(ct, 2), d = pe[0], ft = pe[1], mt = n.useState($), ve = l(mt, 2), pt = ve[0], vt = ve[1];
  n.useEffect(function() {
    ft(I), vt($), N.current && (me ? de(st) : D(m));
  }, [I, $]);
  var gt = n.useState(0), ge = l(gt, 2), M = ge[0], yt = ge[1], k = M >= s.length - 1 || d !== "horizontal" || R;
  n.useEffect(function() {
    me && ut(u);
  }, [u]), n.useEffect(function() {
    return N.current = !0, function() {
      N.current = !1;
    };
  }, []);
  var f = Xt(), ye = f.registerPath, be = f.unregisterPath, bt = f.refreshOverflowKeys, Ie = f.isSubPathKey, It = f.getKeyPath, Me = f.getKeys, Mt = f.getSubPathKeys, Ct = n.useMemo(function() {
    return {
      registerPath: ye,
      unregisterPath: be
    };
  }, [ye, be]), St = n.useMemo(function() {
    return {
      isSubPathKey: Ie
    };
  }, [Ie]);
  n.useEffect(function() {
    bt(k ? m : s.slice(M + 1).map(function(i) {
      return i.key;
    }));
  }, [M, k]);
  var ht = U(X || Te && ((_ = s[0]) === null || _ === void 0 ? void 0 : _.key), {
    value: X
  }), Ce = l(ht, 2), p = Ce[0], V = Ce[1], _t = h(function(i) {
    V(i);
  }), Kt = h(function() {
    V(void 0);
  });
  jt(Ee, function() {
    return {
      list: b.current,
      focus: function(t) {
        var r, o = Me(), a = Gt(o, w), S = a.elements, L = a.key2element, $t = a.element2key, _e = Jt(b.current, S), Ke = p ?? (_e[0] ? $t.get(_e[0]) : (r = s.find(function(kt) {
          return !kt.props.disabled;
        })) === null || r === void 0 ? void 0 : r.key), v = L.get(Ke);
        if (Ke && v) {
          var F;
          v == null || (F = v.focus) === null || F === void 0 || F.call(v, t);
        }
      }
    };
  });
  var Rt = U(ze || [], {
    value: je,
    // Legacy convert key to array
    postState: function(t) {
      return Array.isArray(t) ? t : t == null ? m : [t];
    }
  }), Se = l(Rt, 2), C = Se[0], Pt = Se[1], Ot = function(t) {
    if (Ue) {
      var r = t.key, o = C.includes(r), a;
      te ? o ? a = C.filter(function(L) {
        return L !== r;
      }) : a = [].concat(Lt(C), [r]) : a = [r], Pt(a);
      var S = Pe(Pe({}, t), {}, {
        selectedKeys: a
      });
      o ? O == null || O(S) : P == null || P(S);
    }
    !te && u.length && d !== "inline" && D(m);
  }, xt = h(function(i) {
    x == null || x(on(i)), Ot(i);
  }), he = h(function(i, t) {
    var r = u.filter(function(a) {
      return a !== i;
    });
    if (t)
      r.push(i);
    else if (d !== "inline") {
      var o = Mt(i);
      r = r.filter(function(a) {
        return !o.has(a);
      });
    }
    Ut(u, r, !0) || D(r, !0);
  }), Et = function(t, r) {
    var o = r ?? !u.includes(t);
    he(t, o);
  }, wt = Qt(d, p, A, w, b, Me, It, V, Et, et);
  n.useEffect(function() {
    ot(!0);
  }, []);
  var At = n.useMemo(function() {
    return {
      _internalRenderMenuItem: oe,
      _internalRenderSubMenuItem: ae
    };
  }, [oe, ae]), Dt = d !== "horizontal" || R ? s : (
    // Need wrap for overflow dropdown that do not response for open
    s.map(function(i, t) {
      return (
        // Always wrap provider to avoid sub node re-mount
        /* @__PURE__ */ n.createElement(Oe, {
          key: i.key,
          overflowDisabled: t > M
        }, i)
      );
    })
  ), Nt = /* @__PURE__ */ n.createElement(T, Vt({
    id: B,
    ref: b,
    prefixCls: "".concat(c, "-overflow"),
    component: "ul",
    itemComponent: tn,
    className: Tt(c, "".concat(c, "-root"), "".concat(c, "-").concat(d), Ae, (g = {}, Re(g, "".concat(c, "-inline-collapsed"), pt), Re(g, "".concat(c, "-rtl"), A), g), j),
    dir: H,
    style: we,
    role: "menu",
    tabIndex: De,
    data: Dt,
    renderRawItem: function(t) {
      return t;
    },
    renderRawRest: function(t) {
      var r = t.length, o = r ? s.slice(-r) : null;
      return /* @__PURE__ */ n.createElement(nn, {
        eventKey: Zt,
        title: Qe,
        disabled: k,
        internalPopupClose: r === 0,
        popupClassName: Xe
      }, o);
    },
    maxCount: d !== "horizontal" || R ? T.INVALIDATE : T.RESPONSIVE,
    ssr: "full",
    "data-menu-list": !0,
    onVisibleChange: function(t) {
      yt(t);
    },
    onKeyDown: wt
  }, rt));
  return /* @__PURE__ */ n.createElement(Bt.Provider, {
    value: At
  }, /* @__PURE__ */ n.createElement(Yt.Provider, {
    value: w
  }, /* @__PURE__ */ n.createElement(Oe, {
    prefixCls: c,
    rootClassName: j,
    mode: d,
    openKeys: u,
    rtl: A,
    disabled: Ne,
    motion: se ? Ye : null,
    defaultMotions: se ? qe : null,
    activeKey: p,
    onActive: _t,
    onInactive: Kt,
    selectedKeys: C,
    inlineIndent: We,
    subMenuOpenDelay: $e,
    subMenuCloseDelay: ke,
    forceSubMenuRender: Ve,
    builtinPlacements: Be,
    triggerSubMenuAction: He,
    getPopupContainer: Ze,
    itemIcon: Ge,
    expandIcon: Je,
    onItemClick: xt,
    onOpenChange: he
  }, /* @__PURE__ */ n.createElement(qt.Provider, {
    value: St
  }, Nt), /* @__PURE__ */ n.createElement("div", {
    style: {
      display: "none"
    },
    "aria-hidden": !0
  }, /* @__PURE__ */ n.createElement(Ht.Provider, {
    value: Ct
  }, s)))));
});
export {
  An as default
};
