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