function submitRowIndex(e){return e.length}function filterOptions(e,t){let n=t.trim().toLowerCase();return n===``?[...e]:e.filter(e=>e.label.toLowerCase().includes(n)||e.value.toLowerCase().includes(n)||(e.hint?.toLowerCase().includes(n)??!1))}function isNavigable(e){return!e.disabled&&!e.locked}function firstNavigableIndex(e,t){let n=e.findIndex(isNavigable);return n>=0?n:t?submitRowIndex(e):0}function stepCursor(e,t,n,r){let i=e.length+ +!!r;if(i===0)return t;let a=t;for(let t=0;t<i;t+=1){if(a=(a+n+i)%i,r&&a===submitRowIndex(e))return a;let t=e[a];if(t&&isNavigable(t))return a}return t}function reduceSelect(e,t,n){let r=n.submitRow===!0;switch(t.type){case`char`:{let i=e.filter+t.char;return{...e,filter:i,cursor:firstNavigableIndex(filterOptions(n.options,i),r)}}case`backspace`:{if(e.filter.length===0)return e;let t=e.filter.slice(0,-1);return{...e,filter:t,cursor:firstNavigableIndex(filterOptions(n.options,t),r)}}case`up`:case`down`:{let i=filterOptions(n.options,e.filter),a=t.type===`up`?-1:1,o=stepCursor(i,e.cursor,a,r);return o===e.cursor?e:{...e,cursor:o}}case`toggle`:{let t=filterOptions(n.options,e.filter)[e.cursor];if(t===void 0||!isNavigable(t))return e;let r=new Set(e.selected);return r.has(t.value)?r.delete(t.value):r.add(t.value),{...e,selected:r}}}}function initialSelectState(e){let t=e.filter??``,n=filterOptions(e.options,t),r=firstNavigableIndex(n,e.submitRow===!0);if(e.defaultValue!==void 0){let t=n.findIndex(t=>isNavigable(t)&&t.value===e.defaultValue);t>=0&&(r=t)}let i=e.options.filter(e=>e.locked).map(e=>e.value);return{filter:t,cursor:r,selected:new Set([...e.initialValues??[],...i])}}function selectValueAtCursor(e,t){let n=e[t];return n&&isNavigable(n)?n.value:void 0}function orderedSelection(e,t){return e.filter(e=>t.has(e.value)).map(e=>e.value)}export{filterOptions,initialSelectState,orderedSelection,reduceSelect,selectValueAtCursor,submitRowIndex};