import{r as s,j as e,P as O,y as ee,B as se,w as V,z as te,E as re}from"./index-C-gKvEDq.js";import{C as ae,e as ne,f as ce,g as le,h as oe,i as ue}from"./command-CyAetjD5.js";import{C as de}from"./chevrons-up-down-BLR0y9k5.js";import{L as N}from"./loader-circle-MiR2w4nF.js";function he({value:i,onValueChange:H,placeholder:B="Select option...",searchPlaceholder:q="Search...",emptyMessage:z="No results found.",disabled:D=!1,className:G,options:U,onSearch:o,onLoadMore:u,hasMore:p=!1,debounceMs:C=300,minSearchLength:E=0,initialOptions:r=[],isLoading:J=!1}){const[a,L]=s.useState(!1),[c,S]=s.useState(""),[m,l]=s.useState(r),[j,R]=s.useState(!1),[_,I]=s.useState(r.length>0),[y,w]=s.useState(!1),[K,b]=s.useState(!1),d=s.useRef(null),x=s.useRef(!1),k=s.useRef(p),T=s.useRef(c),v=s.useRef(u),A=s.useRef(null);s.useEffect(()=>{k.current=p},[p]),s.useEffect(()=>{T.current=c},[c]),s.useEffect(()=>{v.current=u},[u]),s.useEffect(()=>{if(!a||!u)return;const t=()=>{const g=A.current;if(!g)return;const P=()=>{const{scrollTop:X,scrollHeight:Y,clientHeight:Z}=g;Y-X-Z<80&&k.current&&!x.current&&v.current&&(x.current=!0,b(!0),v.current(T.current).then($=>{l(M=>[...M,...$])}).catch(()=>{}).finally(()=>{x.current=!1,b(!1)}))};return g.addEventListener("scroll",P),()=>g.removeEventListener("scroll",P)};let h;const W=requestAnimationFrame(()=>{h=t()});return()=>{cancelAnimationFrame(W),h?.()}},[a,u]);const f=U||m,n=!!o,Q=f.find(t=>t.value===i);s.useEffect(()=>{r.length>0&&(l(r),I(!0))},[r]),s.useEffect(()=>{if(!(!n||!a)){if(d.current&&clearTimeout(d.current),c.length<E){l(r.length>0?r:m);return}return d.current=setTimeout(async()=>{R(!0),x.current=!1,b(!1);try{const t=await o(c);l(t)}catch(t){console.error("Search error:",t)}finally{R(!1)}},C),()=>{d.current&&clearTimeout(d.current)}}},[c,n,o,a,C,E]),s.useEffect(()=>{a||(S(""),n&&r.length>0&&l(r))},[a,n,r]),s.useEffect(()=>{a&&n&&!_&&!j&&!y&&(m.length===0&&r.length===0&&w(!0),I(!0),o("").then(h=>{l(h),w(!1)}).catch(()=>{w(!1)}))},[a,n,_,j,y,o,m.length,r.length]);const F=J||j||y;return e.jsxs(O,{open:a,onOpenChange:L,children:[e.jsx(ee,{asChild:!0,children:e.jsxs(se,{variant:"outline",role:"combobox","aria-expanded":a,disabled:D,className:V("w-full justify-between font-normal","h-11 rounded-[30px] bg-background dark:bg-card px-4 py-2.5 text-base text-foreground","shadow-[0px_4px_20px_rgba(0,0,0,0.08)] dark:shadow-[0px_4px_20px_rgba(0,0,0,0.3)]","focus:outline-none focus:shadow-[0px_4px_20px_rgba(0,0,0,0.16)] dark:focus:shadow-[0px_4px_20px_rgba(0,0,0,0.5)]","transition-shadow duration-200","disabled:cursor-not-allowed disabled:opacity-50",!i&&"text-muted-foreground",G),children:[Q?.label||B,e.jsx(de,{className:"ml-2 h-4 w-4 shrink-0 opacity-50"})]})}),e.jsx(te,{className:"w-[--radix-popover-trigger-width] p-0",align:"start",children:e.jsxs(ae,{shouldFilter:!n,children:[e.jsx(ne,{placeholder:q,value:c,onValueChange:S,className:"h-10"}),e.jsx(ce,{ref:A,children:F&&f.length===0?e.jsxs("div",{className:"flex items-center justify-center py-6",children:[e.jsx(N,{className:"h-4 w-4 animate-spin text-muted-foreground"}),e.jsx("span",{className:"ml-2 text-sm text-muted-foreground",children:"Loading..."})]}):e.jsxs(e.Fragment,{children:[f.length===0?e.jsx(le,{children:z}):e.jsx(oe,{children:f.map(t=>e.jsxs(ue,{value:n?t.value:t.label,onSelect:()=>{H(t.value===i?"":t.value),L(!1)},children:[e.jsx(re,{className:V("mr-2 h-4 w-4",i===t.value?"opacity-100":"opacity-0")}),e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{children:t.label}),t.description&&e.jsx("span",{className:"text-xs text-muted-foreground",children:t.description})]})]},t.value))}),F&&f.length>0&&e.jsxs("div",{className:"flex items-center justify-center py-2 border-t",children:[e.jsx(N,{className:"h-3 w-3 animate-spin text-muted-foreground"}),e.jsx("span",{className:"ml-2 text-xs text-muted-foreground",children:"Searching..."})]}),K&&e.jsxs("div",{className:"flex items-center justify-center py-2 border-t",children:[e.jsx(N,{className:"h-3 w-3 animate-spin text-muted-foreground"}),e.jsx("span",{className:"ml-2 text-xs text-muted-foreground",children:"Loading more..."})]})]})})]})})]})}export{he as S};
