import{slicedToArray as e,defineProperty as o}from"../../_virtual/_rollupPluginBabelHelpers.js";import{jsx as i,jsxs as n}from"react/jsx-runtime";import r,{forwardRef as t,useRef as s,useState as a,useCallback as d,useMemo as l,useLayoutEffect as g}from"react";import c from"classnames";import p from"../Dialog/Dialog.js";import u from"../DialogContentContainer/DialogContentContainer.js";import m from"../Tooltip/Tooltip.js";import{backwardCompatibilityForProperties as h}from"../../helpers/backwardCompatibilityForProperties.js";import f from"../../hooks/useMergeRefs.js";import{BUTTON_ICON_SIZE as T}from"../Button/ButtonConstants.js";import{MenuButtonSize as C,MenuButtonComponentPosition as E}from"./MenuButtonConstants.js";import{HideShowEvent as v}from"../Dialog/consts/dialog-show-hide-event.js";import{NOOP as S}from"../../utils/function-utils.js";import"./MenuButton.scss.js";import w from"../Icon/Icons/components/Menu.js";import{AnimationType as b}from"../../constants/dialog.js";function j(e){return"menu-button--wrapper--".concat(e)}var O=[p.hideShowTriggers.MOUSE_ENTER],M=[v.CLICK,v.ENTER],P=[],A={main:0,secondary:-6},D=t((function(t,E){var v,N=t.id,y=t.className,I=t.componentClassName,R=t.openDialogComponentClassName,_=t.children,z=t.component,L=void 0===z?w:z,B=t.componentPosition,x=void 0===B?D.componentPositions.START:B,H=t.size,K=void 0===H?C.SMALL:H,U=t.open,k=void 0!==U&&U,F=t.onClick,W=void 0===F?S:F,Y=t.zIndex,X=void 0===Y?null:Y,V=t.ariaLabel,q=void 0===V?"Menu":V,G=t.closeDialogOnContentClick,J=void 0!==G&&G,Q=t.dialogOffset,Z=void 0===Q?A:Q,$=t.dialogPosition,ee=void 0===$?p.positions.BOTTOM_START:$,oe=t.dialogClassName,ie=t.dialogPaddingSize,ne=void 0===ie?u.sizes.MEDIUM:ie,re=t.dialogShowTriggerIgnoreClass,te=t.dialogHideTriggerIgnoreClass,se=t.onMenuHide,ae=void 0===se?S:se,de=t.onMenuShow,le=void 0===de?S:de,ge=t.disabled,ce=void 0!==ge&&ge,pe=t.text,ue=t.tooltipContent,me=t.disabledReason,he=t.tooltipTriggers,fe=void 0===he?[D.hideTriggers.MOUSE_LEAVE]:he,Te=t.tooltipPosition,Ce=void 0===Te?D.dialogPositions.RIGHT:Te,Ee=t.startingEdge,ve=void 0===Ee?"bottom":Ee,Se=t.removeTabCloseTrigger,we=void 0!==Se&&Se,be=t.tooltipReferenceClassName,je=t.hideWhenReferenceHidden,Oe=void 0!==je&&je,Me=t.dialogContainerSelector,Pe=s(null),Ae=f({refs:[E,Pe]}),De=a(k),Ne=e(De,2),ye=Ne[0],Ie=Ne[1],Re=d((function(e){if(e&&"Escape"===e.key){Ie(!1);var o=Pe.current;window.requestAnimationFrame((function(){o.focus()}))}}),[Pe,Ie]),_e=d((function(e,o){Ie(!1),ae();var i=Pe.current;window.requestAnimationFrame((function(){i&&o===p.hideShowTriggers.ESCAPE_KEY&&i.focus()}))}),[Ie,ae,Pe]),ze=d((function(){Ie(!0),le()}),[Ie,le]),Le=l((function(){var e=new Set([p.hideShowTriggers.CLICK_OUTSIDE,p.hideShowTriggers.TAB_KEY,p.hideShowTriggers.ESCAPE_KEY]);return J&&(e.add(p.hideShowTriggers.CONTENT_CLICK),e.add(p.hideShowTriggers.ENTER)),we&&e.delete(p.hideShowTriggers.TAB_KEY),[r.Children.toArray(_).map((function(o){if(!r.isValidElement(o))return null;var i={};return o.type&&o.type.supportFocusOnMount&&(i.focusOnMount=!0,e.delete(p.hideShowTriggers.ESCAPE_KEY)),o.type&&o.type.isMenu&&(i.onClose=Re),r.cloneElement(o,i)})),Array.from(e)]}),[_,Re,J,we]),Be=e(Le,2),xe=Be[0],He=Be[1],Ke=l((function(){return 0===xe.length?null:i(u,Object.assign({size:ne,type:u.types.POPOVER},{children:xe}))}),[xe,ne]),Ue=l((function(){return Object.assign(Object.assign({},A),Z)}),[Z]),ke=L,Fe=l((function(){switch(K){case C.XXS:case C.XS:return 16;case C.SMALL:case C.MEDIUM:case C.LARGE:return T;default:return 24}}),[K]),We=ke?i(ke,{size:""+Fe,role:"img","aria-hidden":"true"}):null;g((function(){Ie(k)}),[k,Ie]);var Ye=h([ue,me]),Xe=h([y,I]);return i(m,Object.assign({content:Ye,position:Ce,showTrigger:O,hideTrigger:fe,referenceWrapperClassName:be,hideWhenReferenceHidden:Oe},{children:i(p,Object.assign({wrapperClassName:oe,position:ee,containerSelector:Me,startingEdge:ve,animationType:b.EXPAND,content:Ke,moveBy:Ue,showTrigger:ce?P:M,hideTrigger:He,showTriggerIgnoreClass:re,hideTriggerIgnoreClass:te,useDerivedStateFromProps:!0,onDialogDidShow:ze,onDialogDidHide:_e,referenceWrapperClassName:j("reference-icon"),zIndex:X,isOpen:ye,hideWhenReferenceHidden:Oe},{children:n("button",Object.assign({id:N,ref:Ae,type:"button",className:c("menu-button--wrapper",Xe,j("size-".concat(K)),(v={},o(v,j("open"),ye),o(v,R,ye&&R),o(v,j("disabled"),ce),o(v,j("text"),pe),v)),"aria-haspopup":"true","aria-expanded":ye,"aria-label":!pe&&q,onMouseUp:function(e){ce?e.currentTarget.blur():W(e)},"aria-disabled":ce},{children:[x===D.componentPositions.START&&We,pe&&i("span",Object.assign({className:j("inner-text")},{children:pe})),x===D.componentPositions.END&&We]}))}))}))}));Object.assign(D,{sizes:C,paddingSizes:u.sizes,dialogPositions:p.positions,hideTriggers:p.hideShowTriggers,componentPositions:E});var N=D;export{N as default};
