import{useState as e,useEffect as t}from"react";const r=(r,n,a,c,s)=>{const[o,u]=e(-1);t((()=>{u((()=>{if(a||u(-1),!s)return-1;const e=n.indexOf(s);return-1===e?-1:e})())}),[s,a]);const f=((e,t)=>{if(!(e<0))return t[e]})(o,n);return[f,e=>{if(a)switch(e.key){case"ArrowUp":e.preventDefault(),u((()=>{const e=o-1;return Math.max(e,0)})());break;case"ArrowDown":e.preventDefault(),u((()=>{const e=o+1;return Math.min(e,n.length-1)})());break;case"Enter":e.preventDefault(),f&&r(f);break;case"Escape":e.preventDefault(),c(!1),u(-1)}}]};export{r as default};
