import{r as s,j as t}from"./vendor-react.js";import{c as p}from"./button.js";import{I as j}from"./input.js";import{C}from"./vendor-icons.js";function D({value:a,onChange:d,options:f,placeholder:x,loading:v,loadingText:h="Loading...",className:g}){const[r,n]=s.useState(!1),[c,u]=s.useState(""),l=s.useRef(null),i=s.useRef(null);s.useEffect(()=>{r||u(a)},[r,a]),s.useEffect(()=>{if(!r)return;const e=o=>{i.current&&!i.current.contains(o.target)&&n(!1)};return document.addEventListener("pointerdown",e),()=>document.removeEventListener("pointerdown",e)},[r]);const m=c?f.filter(e=>e.label.toLowerCase().includes(c.toLowerCase())||e.value.toLowerCase().includes(c.toLowerCase())):f,w=e=>{const o=e.target.value;u(o),d(o),r||n(!0)},b=e=>{d(e),u(e),n(!1)};return t.jsxs("div",{ref:i,className:p("relative",g),children:[t.jsx(j,{ref:l,value:r?c:a,onChange:w,placeholder:x,onFocus:()=>n(!0),onKeyDown:e=>{e.key==="Escape"&&(n(!1),l.current?.blur())},className:"pr-8"}),t.jsx(C,{className:"absolute right-2.5 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground cursor-pointer",onClick:()=>{n(e=>!e),l.current?.focus()}}),r&&t.jsx("div",{className:"absolute z-50 mt-1 w-full rounded-md border bg-popover p-1 text-popover-foreground shadow-md animate-in fade-in-0 zoom-in-95 slide-in-from-top-2",children:t.jsx("div",{className:"max-h-[200px] overflow-y-auto",children:v?t.jsx("div",{className:"px-3 py-2 text-sm text-muted-foreground",children:h}):m.length===0?t.jsx("div",{className:"px-3 py-2 text-sm text-muted-foreground",children:"—"}):m.map(e=>t.jsx("div",{className:p("flex items-center rounded-sm px-3 py-1.5 text-sm cursor-pointer hover:bg-accent hover:text-accent-foreground",e.value===a&&"bg-accent text-accent-foreground"),onMouseDown:o=>{o.preventDefault(),b(e.value)},children:e.label},e.value))})})]})}export{D as C};
