import{slicedToArray as e}from"../../../../_virtual/_rollupPluginBabelHelpers.js";import n from"classnames";import{SIZES as t}from"../../../constants/sizes.js";import s,{forwardRef as o,useRef as u,useState as i,useMemo as r,useCallback as l,useEffect as d}from"react";import a from"../../../hooks/useMergeRef.js";import c from"../../../hooks/ssr/useIsomorphicLayoutEffect.js";import m from"./hooks/useSubMenuIndex.js";import p from"./hooks/useOnCloseMenu.js";import v from"./hooks/useCloseMenuOnKeyEvent.js";import f from"./hooks/useMenuKeyboardNavigation.js";import I from"./hooks/useMouseLeave.js";import{useAdjacentSelectableMenuIndex as M}from"./hooks/useAdjacentSelectableMenuIndex.js";import{useFocusWithin as b}from"../../../hooks/useFocusWithin.js";import h from"../../../hooks/usePrevious/index.js";import{withStaticProps as x}from"../../../types/withStaticProps.js";import"../../../utils/colors-vars-map.js";import{getStyle as S}from"../../../helpers/typesciptCssModulesHelper.js";import{getTestId as j}from"../../../tests/testIds.js";import{ComponentDefaultTestId as O,ComponentVibeId as k}from"../../../tests/constants.js";import{useFocusOnMount as g}from"./hooks/useFocusOnMount.js";import{useMenuId as C}from"./hooks/useMenuId.js";import{generateMenuItemId as E}from"./utils/utils.js";import y from"./Menu.module.scss.js";import A from"../../../../hooks/dist/useClickOutside/index.js";var N=o((function(o,x){var N=o.id,B=o.className,F=o.size,P=void 0===F?t.MEDIUM:F,D=o.tabIndex,L=void 0===D?0:D,V=o.ariaLabel,U=void 0===V?"Menu":V,z=o.ariaDescribedBy,R=o.children,T=o.isVisible,W=void 0===T||T,H=o.onClose,K=o.onItemFocus,_=o.focusOnMount,q=void 0!==_&&_,w=o.focusItemIndex,G=void 0===w?-1:w,J=o.focusItemIndexOnMount,Q=void 0===J?-1:J,X=o.isSubMenu,Y=void 0!==X&&X,Z=o.useDocumentEventListeners,$=void 0!==Z&&Z,ee=o.shouldScrollMenu,ne=void 0!==ee&&ee,te=o["data-testid"],se=u(null),oe=a(se,x),ue=C(N),ie=u(null),re=i(G),le=e(re,2),de=le[0],ae=le[1],ce=i(!1),me=e(ce,2),pe=me[0],ve=me[1],fe=r((function(){return s.Children.toArray(R).filter((function(e){return!!e.type.isMenuChild||(console.error("Menu child must be a menuChild item (such as MenuItem, MenuDivider, MenuTitle, etc). This child is not supported: ",e),!1)}))}),[R]),Ie=l((function(e){var n,t,o;ae(e);var u=fe[e],i=s.isValidElement(u)?(null===(n=null==u?void 0:u.props)||void 0===n?void 0:n.id)||"".concat(ue,"-item-").concat(e):void 0;i?null===(t=null==se?void 0:se.current)||void 0===t||t.setAttribute("aria-activedescendant",i):null===(o=null==se?void 0:se.current)||void 0===o||o.removeAttribute("aria-activedescendant")}),[fe,ue]),Me=l((function(e){K&&K(e),Ie(e),ve(!1)}),[Ie,K]),be=m(),he=be.setSubMenuIsOpenByIndex,xe=be.hasOpenSubMenu,Se=be.openSubMenuIndex,je=be.resetOpenSubMenuIndex,Oe=p({setActiveItemIndex:Me,setOpenSubMenuIndex:be.setOpenSubMenuIndex,onClose:H});A({ref:se,callback:function(){return Oe()}}),v({hasOpenSubMenu:xe,onCloseMenu:Oe,ref:se,onClose:H,isSubMenu:Y,useDocumentEventListeners:$});var ke=M({children:fe}),ge=ke.getNextSelectableIndex,Ce=ke.getPreviousSelectableIndex;f({hasOpenSubMenu:xe,getNextSelectableIndex:ge,getPreviousSelectableIndex:Ce,activeItemIndex:de,setActiveItemIndex:Me,isVisible:W,ref:se,useDocumentEventListeners:$}),I({resetOpenSubMenuIndex:je,hasOpenSubMenu:xe,ref:se,setActiveItemIndex:Me}),g({focusItemIndexOnMount:Q,focusChildOnMount:fe[Q],getNextSelectableIndex:ge,updateActiveItemIndex:Ie,setIsInitialFocusSet:ve});var Ee=l((function(){ve(!0)}),[ve]),ye=h(xe);d((function(){var e;xe||$||de>-1&&ye&&(null===(e=null==se?void 0:se.current)||void 0===e||e.focus())}),[de,xe,ye,$]),c((function(){q&&!$&&requestAnimationFrame((function(){se&&se.current&&se.current.focus()}))}),[se,q,$]);var Ae=b({onBlurWithin:function(){Oe&&Oe()}}).focusWithinProps;return s.createElement("ul",{onFocus:null==Ae?void 0:Ae.onFocus,onBlur:null==Ae?void 0:Ae.onBlur,id:ue,"data-testid":te||j(O.MENU,N),"data-vibe":k.MENU,className:n(y.menu,S(y,P),B),ref:oe,tabIndex:L,"aria-label":U,role:"menu","aria-describedby":z,onMouseOver:Ee},fe&&s.Children.map(fe,(function(e,n){return s.isValidElement(e)?s.cloneElement(e,Object.assign(Object.assign({},null==e?void 0:e.props),{activeItemIndex:de,index:n,setActiveItemIndex:Ie,menuRef:se,resetOpenSubMenuIndex:je,isParentMenuVisible:W,setSubMenuIsOpenByIndex:he,hasOpenSubMenu:n===Se,closeMenu:Oe,id:E(ue,e,n),useDocumentEventListeners:$,isInitialSelectedState:pe,shouldScrollMenu:ne,getNextSelectableIndex:ge,getPreviousSelectableIndex:Ce,isUnderSubMenu:Y,splitMenuItemIconButtonRef:ie})):null})))}));Object.assign(N,{isMenu:!0,supportFocusOnMount:!0});var B=x(N,{sizes:t});export{B as default};
//# sourceMappingURL=Menu.js.map
