import{slicedToArray as e}from"../../../_virtual/_rollupPluginBabelHelpers.js";import{jsx as n}from"react/jsx-runtime";import{SIZES as s}from"../../../constants/sizes.js";import o,{forwardRef as t,useRef as u,useState as i,useCallback as r,useEffect as l,useMemo as c,useLayoutEffect as a}from"react";import d from"classnames";import m from"../../../hooks/useMergeRefs.js";import p from"../../../hooks/useClickOutside/index.js";import{backwardCompatibilityForProperties as v}from"../../../helpers/backwardCompatibilityForProperties.js";import b from"./hooks/useSubMenuIndex.js";import f from"./hooks/useOnCloseMenu.js";import M from"./hooks/useCloseMenuOnKeyEvent.js";import I from"./hooks/useMenuKeyboardNavigation.js";import x from"./hooks/useMouseLeave.js";import{useAdjacentSelectableMenuIndex as h}from"./hooks/useAdjacentSelectableMenuIndex.js";import{useFocusWithin as S}from"../../../hooks/useFocusWithin.js";import O from"../../../hooks/usePrevious/index.js";import"./Menu.scss.js";var j=t((function(s,t){var g=s.id,k=s.className,y=s.classname,C=s.size,A=void 0===C?j.sizes.MEDIUM:C,E=s.tabIndex,P=void 0===E?0:E,B=s.ariaLabel,D=void 0===B?"Menu":B,F=s.ariaDescribedBy,L=s.children,N=s.isVisible,z=void 0===N||N,V=s.onClose,T=s.focusOnMount,W=void 0!==T&&T,K=s.focusItemIndex,R=void 0===K?-1:K,U=s.focusItemIndexOnMount,_=void 0===U?-1:U,q=s.isSubMenu,w=void 0!==q&&q,H=s.useDocumentEventListeners,G=void 0!==H&&H,J=s.shouldScrollMenu,Q=void 0!==J&&J,X=s.collapsed,Y=void 0!==X&&X,Z=u(null),$=v([k,y]),ee=i(R),ne=e(ee,2),se=ne[0],oe=ne[1],te=i(!1),ue=e(te,2),ie=ue[0],re=ue[1],le=r((function(e){oe(e),re(!1)}),[oe,re]);l((function(){-1!==_&&(oe(_),re(!0))}),[_,re]);var ce=c((function(){return o.Children.toArray(L).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)}))}),[L]),ae=b(),de=ae.setSubMenuIsOpenByIndex,me=ae.hasOpenSubMenu,pe=ae.openSubMenuIndex,ve=ae.resetOpenSubMenuIndex,be=f({setActiveItemIndex:le,setOpenSubMenuIndex:ae.setOpenSubMenuIndex,onClose:V});p({ref:Z,callback:function(){return be()}}),M({hasOpenSubMenu:me,onCloseMenu:be,ref:Z,onClose:V,isSubMenu:w,useDocumentEventListeners:G});var fe=h({children:ce}),Me=fe.getNextSelectableIndex,Ie=fe.getPreviousSelectableIndex;I({hasOpenSubMenu:me,getNextSelectableIndex:Me,getPreviousSelectableIndex:Ie,activeItemIndex:se,setActiveItemIndex:le,isVisible:z,ref:Z,useDocumentEventListeners:G}),x({resetOpenSubMenuIndex:ve,hasOpenSubMenu:me,ref:Z,setActiveItemIndex:le});var xe=r((function(){re(!0)}),[re]),he=O(me);l((function(){var e;me||G||se>-1&&he&&(null===(e=null==Z?void 0:Z.current)||void 0===e||e.focus())}),[se,me,he,G]),a((function(){W&&!G&&requestAnimationFrame((function(){Z&&Z.current&&Z.current.focus()}))}),[Z,W,G]);var Se=m({refs:[Z,t]}),Oe=S({onBlurWithin:function(){be&&be()}}).focusWithinProps;return n("ul",Object.assign({onFocus:null==Oe?void 0:Oe.onFocus,onBlur:null==Oe?void 0:Oe.onBlur,id:g,className:d("l3-style-menu",$,"l3-style-menu--".concat(A),"".concat(Y&&"l3-menu--collapsed")),ref:Se,tabIndex:P,"aria-label":D,role:"menu","aria-activedescendant":"".concat(g,"-").concat(se),"aria-describedby":F,onMouseOver:xe},{children:ce&&o.Children.map(ce,(function(e,n){return o.isValidElement(e)?o.cloneElement(e,Object.assign(Object.assign({},null==e?void 0:e.props),{activeItemIndex:se,index:n,setActiveItemIndex:oe,menuRef:Z,resetOpenSubMenuIndex:ve,isParentMenuVisible:z,setSubMenuIsOpenByIndex:de,hasOpenSubMenu:n===pe,closeMenu:be,menuId:g,useDocumentEventListeners:G,isInitialSelectedState:ie,shouldScrollMenu:Q,getNextSelectableIndex:Me,getPreviousSelectableIndex:Ie,isUnderSubMenu:w})):null}))}))}));Object.assign(j,{isMenu:!0,supportFocusOnMount:!0,sizes:s});var g=j;export{g as default};
