"use strict";const F=require("./tslib.es6-C4EgNkz1.js"),c=require("react"),V=require("classnames"),G=require("./use-roving-index-DoAVBgsp.js"),H=require("./index-Bsb1LHpi.js"),J=require("./forwardRef-DBQKIlPy.js");require("./array.polyfill.flat-5BAolFdk.js");const Q=require("./badge-BWhBKWCu.js"),U=require("./icons/ebay-icon-tick-16/index.js"),B=o=>{var{className:k,checked:r,type:f,focused:m=!1,tabIndex:w,disabled:p,badgeNumber:y,badgeAriaLabel:C,children:v,baseClass:_="menu"}=o,g=F.__rest(o,["className","checked","type","focused","tabIndex","disabled","badgeNumber","badgeAriaLabel","children","baseClass"]);const i=c.useRef(null),b=y!==void 0;c.useEffect(()=>{i.current&&m&&i.current.focus()},[i,m]);const R=["radio","checkbox"];return c.createElement("div",Object.assign({"aria-label":C},g,{ref:i,className:V(k,`${_}__item`,b&&`${_}__item--badged`),role:X(f),"aria-checked":R.includes(f)?r:void 0,"aria-disabled":p,tabIndex:m?0:w}),c.createElement("span",{"aria-hidden":b},v,b&&c.createElement(Q.EbayBadge,{type:"menu",number:y})),c.createElement(U.EbayIconTick16,null))};function X(o){return{radio:"menuitemradio",checkbox:"menuitemcheckbox"}[o]||"menuitem"}const Y=o=>{var{baseEl:k="span",type:r,checked:f,className:m,autofocus:w,classPrefix:p,reverse:y,fixWidth:C,fixed:v,onClick:_=()=>{},onKeyDown:g=()=>{},onChange:i=()=>{},onSelect:b=()=>{},forwardedRef:R,menuRef:S,children:K}=o,T=F.__rest(o,["baseEl","type","checked","className","autofocus","classPrefix","reverse","fixWidth","fixed","onClick","onKeyDown","onChange","onSelect","forwardedRef","menuRef","children"]);const h=c.Children.toArray(K),[D,j]=G.useRovingIndex(K,B,w===!0?0:void 0),[u,O]=c.useState(h.map(()=>!1)),L=n=>h.reduce((e,s,a)=>n[a]?[...e,s.props.value]:e,[]),A=n=>n.reduce((e,s,a)=>s?[...e,a]:e,[]),N=(n,e)=>({index:n,checked:A(e)}),M=(n,e)=>Object.assign(Object.assign({},N(n,e)),{indexes:A(e),checkedValues:L(e)}),P=(n,e,s=!1)=>{let a=!1;const I=u.map((E,l)=>{const q=s?!1:E;return n===l?(E!==e&&(a=!0),e):q});if(a)return O(I),I},x=n=>{switch(r){case"radio":return P(n,!0,!0);case"checkbox":return P(n,!u[n],!1);default:return u.map((e,s)=>s===n)}};c.useEffect(()=>{if(r==="radio")if(f===void 0){const n=h.findIndex(e=>e.props.checked);n>-1&&x(n)}else x(f);else r==="checkbox"&&O(h.map(n=>!!n.props.checked))},[]);const $=(n,e,s)=>{switch(r){case"radio":case"checkbox":return i(n,M(e,s));default:return b(n,N(e,s))}},W=(n,e)=>{let s;switch(H.isActionKey(n.key)&&(s=x(e),s&&$(n,e,s)),r){case"radio":case"checkbox":return g(n,M(e,s||u));default:return g(n,N(e,s||u))}},z=(n,e)=>{j(e);const s=x(e);s&&$(n,e,s)},d=p||"menu";return c.createElement(k,Object.assign({},T,{className:V(m,p?`${d}__menu`:"menu",y&&`${d}__menu--reverse`,v&&`${d}__menu--fixed`,C&&`${d}__menu--fix-width`),ref:R}),c.createElement("div",{className:`${d}__items`,role:"menu",ref:S},h.map((n,e)=>{const s=n.props,{onClick:a=()=>{},onFocus:I=()=>{},onKeyDown:E=()=>{},disabled:l}=s,q=F.__rest(s,["onClick","onFocus","onKeyDown","disabled"]);return c.cloneElement(n,Object.assign(Object.assign({},q),{disabled:l,type:r,baseClass:d,focused:e===D,tabIndex:D===void 0?0:-1,checked:u[e],onFocus:t=>{l||(j(e),I(t))},onClick:t=>{if(l){t.stopPropagation();return}z(t,e),a(t),_(t)},onKeyDown:t=>{l||(E(t),W(t,e))}}))})))},Z=J.withForwardRef(Y);exports.EbayMenu=Z;exports.EbayMenuItem=B;
