import{i as e}from"./rolldown-runtime-Dd_uD5pT.js";import{t}from"./react-BatatxyT.js";import{s as n,t as r}from"./utils-BO1RPuol.js";import{t as i}from"./chevron-down-Bbo_Qa5h.js";import{ko as a,vo as o}from"./index-CsuVxiRQ.js";var s=e(t(),1),c=n(),l=new Map,u=3e5,d=`all-items`;function f(){l.delete(d)}function p(e){let t=l.get(d);t&&(t.options=[...t.options,e],l.set(d,t))}function m({value:e,onValueChange:t,fetchAll:n,placeholder:f=`Select...`,disabled:p=!1,className:m=``}){let[h,g]=(0,s.useState)(!1),[_,v]=(0,s.useState)([]),[y,b]=(0,s.useState)([]),[x,S]=(0,s.useState)(!1),[C,w]=(0,s.useState)(``),T=(0,s.useRef)(void 0),E=(0,s.useRef)(null),D=(0,s.useRef)(!1),O=_.find(t=>t.value===e),k=O?.label||f,A=(0,s.useCallback)(async()=>{if(D.current)return;let e=Date.now(),t=l.get(d);if(t&&e-t.timestamp<u){v(t.options),b(t.options);return}D.current=!0,S(!0);try{let t=await n();l.set(d,{options:t,timestamp:e}),v(t),b(t)}catch(e){console.error(`Failed to load options:`,e)}finally{S(!1),D.current=!1}},[n]);(0,s.useEffect)(()=>{h&&_.length===0&&A()},[h,_.length,A]),(0,s.useEffect)(()=>{if(e&&_.length===0){let e=l.get(d);e&&Date.now()-e.timestamp<u&&(v(e.options),b(e.options))}},[e,_.length]),(0,s.useEffect)(()=>(T.current&&clearTimeout(T.current),T.current=setTimeout(()=>{if(!C.trim())b(_);else{let e=C.toLowerCase();b(_.filter(t=>t.label.toLowerCase().includes(e)))}},300),()=>{T.current&&clearTimeout(T.current)}),[C,_]),(0,s.useEffect)(()=>{if(!h)return;let e=e=>{E.current&&!E.current.contains(e.target)&&g(!1)};return document.addEventListener(`mousedown`,e),()=>document.removeEventListener(`mousedown`,e)},[h]);let j=e=>{t(e),g(!1),w(``)};return(0,c.jsxs)(`div`,{className:r(`relative`,m),ref:E,children:[(0,c.jsxs)(`button`,{type:`button`,onClick:()=>!p&&g(!h),disabled:p,className:r(`flex w-full items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background`,`hover:bg-accent hover:text-accent-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2`,`disabled:cursor-not-allowed disabled:opacity-50`,!O&&`text-muted-foreground`),children:[(0,c.jsx)(`span`,{className:`truncate`,children:k}),(0,c.jsx)(i,{className:r(`ml-2 h-4 w-4 shrink-0 opacity-50 transition-transform`,h&&`rotate-180`)})]}),h&&(0,c.jsxs)(`div`,{className:`absolute z-50 mt-1 w-full rounded-md border bg-popover shadow-md`,children:[(0,c.jsxs)(`div`,{className:`flex items-center border-b px-3 py-2`,children:[(0,c.jsx)(o,{className:`mr-2 h-4 w-4 shrink-0 opacity-50`}),(0,c.jsx)(`input`,{type:`text`,placeholder:`Search...`,value:C,onChange:e=>w(e.target.value),className:`flex-1 bg-transparent text-sm outline-none placeholder:text-muted-foreground`,autoFocus:!0})]}),(0,c.jsxs)(`div`,{className:`max-h-75 overflow-y-auto p-1`,children:[x&&y.length===0&&(0,c.jsxs)(`div`,{className:`flex items-center justify-center py-6`,children:[(0,c.jsx)(a,{className:`h-5 w-5 animate-spin text-muted-foreground`}),(0,c.jsx)(`span`,{className:`ml-2 text-sm text-muted-foreground`,children:`Loading items...`})]}),!x&&y.length===0&&(0,c.jsx)(`div`,{className:`py-6 text-center text-sm text-muted-foreground`,children:`No results found`}),y.map(t=>(0,c.jsx)(`button`,{type:`button`,onClick:()=>j(t.value),className:r(`w-full rounded-sm px-2 py-1.5 text-left text-sm outline-none cursor-pointer`,`hover:bg-accent hover:text-accent-foreground`,t.value===e&&`bg-accent text-accent-foreground`),children:t.label},t.value))]})]})]})}export{p as n,f as r,m as t};