import{n as e}from"./rolldown-runtime-C0FnF6B9.js";import{t}from"./react-C21x__mS.js";import{n,t as r}from"./check-DCksZOfl.js";import{n as i}from"./iframe-GrlyTB2d.js";import{a,i as o,n as s,r as c,s as l,t as u}from"./utils-DUM89eIj.js";import{n as d,t as f}from"./ui.module-CiLQZm_L.js";function p(e,t,n){if(n){if(Array.isArray(t)&&t.length>0){let n=e.findIndex(e=>t.includes(e.value)&&!e.disabled);if(n>=0)return n}return c(e,_)}if(t!==void 0){let n=e.findIndex(e=>e.value===t&&!e.disabled);if(n>=0)return n}return c(e,_)}function m(e){let{value:t,defaultValue:n,multiple:i,disabled:o,focusOnMount:f,renderItem:m,getKey:v,options:y,onChange:b,id:x,...S}=e,C=t!==void 0,w=(0,h.useId)(),T=x??`${w}-listbox`,[E,D]=(0,h.useState)(n),O=(0,h.useRef)(null),[k,A]=(0,h.useState)(()=>p(y,t??n,i)),j=(0,h.useRef)(!1),M=(0,h.useMemo)(()=>(C?t:E)??(i?[]:null),[E,C,i,t]),N=k,P=t=>{if(o||t.disabled)return;let n;if(e.multiple){let r=Array.isArray(M)?M:[];n=r.includes(t.value)?r.filter(e=>e!==t.value):[...r,t.value],e.onChange?.(n,t)}else n=t.value,e.onChange?.(n,t);C||D(n)};return(0,h.useEffect)(()=>{if(!j.current)return;j.current=!1;let e=O.current?.querySelectorAll(`[role="option"]`)[N];e&&e.scrollIntoView({block:`nearest`})},[N]),(0,h.useLayoutEffect)(()=>{f&&O.current?.focus()},[f]),(0,g.jsx)(`ul`,{ref:O,id:T,role:`listbox`,"aria-multiselectable":i||void 0,"aria-activedescendant":N>=0?`${T}-option-${N}`:void 0,onKeyDown:e=>{if(!o){if(e.key===`ArrowDown`)e.preventDefault(),j.current=!0,A(e=>u(y,e,1,_));else if(e.key===`ArrowUp`)e.preventDefault(),j.current=!0,A(e=>u(y,e,-1,_));else if(e.key===`Home`)e.preventDefault(),j.current=!0,A(c(y,_));else if(e.key===`End`)e.preventDefault(),j.current=!0,A(l(y,_));else if(s(e,()=>{let e=y[N];e&&P(e)}))return}},...S,"aria-disabled":o||void 0,tabIndex:o?-1:0,className:a(d.uiPanel,`scrollable`,o&&d.disabled),children:y.map((e,t)=>{let n=i?Array.isArray(M)&&M.includes(e.value):M===e.value,s=t===N;return(0,g.jsxs)(`li`,{id:`${T}-option-${t}`,role:`option`,"aria-selected":n,"aria-disabled":o||e.disabled||void 0,onMouseDown:n=>{n.preventDefault(),!(o||e.disabled)&&(A(t),P(e))},tabIndex:-1,className:a(d.uiPopupOption,(o||e.disabled)&&d.disabled,s&&d.uiPopupOptionActive,n&&d.selected),children:[n?(0,g.jsx)(r,{className:d.uiPopupOptionIcon}):(0,g.jsx)(`span`,{className:d.uiPopupOptionIcon}),(0,g.jsx)(`div`,{children:m?m(e,n,s):e.label??String(e.value)})]},v?v(e,t):t)})})}var h,g,_;function v(){return(v=e((()=>{h=t(),o(),n(),f(),g=i(),_=e=>e.disabled===!0,m.__docgenInfo={description:``,methods:[],displayName:`Listbox`}})))()}export{v as n,m as t};