import{Rr as e,kr as t}from"./KXnyWfWg.js";function n(n){let{isOpen:r,triggerEl:i,menuEl:a,close:o}=n,s=n.itemSelector??`[role="menuitem"]`,c=typeof document<`u`,l=()=>a.value?Array.from(a.value.querySelectorAll(s)):[],u=e=>{let t=l();t.length&&t[(e%t.length+t.length)%t.length]?.focus()},d=()=>{let e=l();if(!e.length)return;let t=e.findIndex(e=>e.getAttribute(`aria-current`)===`true`||e.dataset.current===`true`);e[t>=0?t:0]?.focus()},f=()=>c?l().indexOf(document.activeElement):-1;return e(r,(e,n)=>{if(c){if(e)t(d);else if(n){let e=document.activeElement;(!e||e===document.body||a.value?.contains(e))&&i.value?.focus()}}}),{onKeydown:e=>{switch(e.key){case`Escape`:e.preventDefault(),o();break;case`ArrowDown`:e.preventDefault(),u(f()+1);break;case`ArrowUp`:e.preventDefault(),u(f()-1);break;case`Home`:e.preventDefault(),u(0);break;case`End`:e.preventDefault(),u(l().length-1);break}},focusActiveOrFirst:d}}export{n as t};