import{r as m,j as e}from"./vendor-react-DrAK5R0M.js";import{C as h,bO as S,bN as k,ce as L,ch as q,n as U,o as $,p as A,q as Q,D as R}from"./index-7eqLqzhB.js";import{P as V,a as X,b as B}from"./popover-D2Z9PLgr.js";import{C as F,a as G,b as z,c as H,d as J,e as K}from"./command-B3OEOPpN.js";import{X as M,aX as W,t as Y}from"./vendor-icons-Cu2Ube6F.js";function Z(s,o=300){const[t,r]=m.useState(s);return m.useEffect(()=>{const n=setTimeout(()=>r(s),o);return()=>clearTimeout(n)},[s,o]),t}function f(s,o){if(s==="contact")return[o.first_name,o.last_name].filter(Boolean).join(" ")||o.email||o.id;if(s==="account")return o.name??o.id;if(s==="opportunity"){const t=o.amount?` · ${R(Number(o.amount))}`:"";return(o.name??o.id)+t}return s==="use_case"?o.name??o.id:o.id}function I(s,o){const{data:t}=S(s==="account"?o:""),{data:r}=k(s==="contact"?o:""),{data:n}=L(s==="opportunity"?o:""),{data:a}=q(s==="use_case"?o:"");if(o){if(s==="account"&&(t!=null&&t.account))return f("account",t.account);if(s==="contact"&&(r!=null&&r.contact))return f("contact",r.contact);if(s==="opportunity"&&(n!=null&&n.opportunity))return f("opportunity",n.opportunity);if(s==="use_case"&&(a!=null&&a.use_case))return f("use_case",a.use_case)}}function D(s,o,t){const r=t?{q:o||void 0,limit:10}:void 0,{data:n}=U(s==="account"?r:void 0),{data:a}=$(s==="contact"?r:void 0),{data:u}=A(s==="opportunity"?r:void 0),{data:d}=Q(s==="use_case"?r:void 0);let l=[];return s==="account"?l=(n==null?void 0:n.data)??(n==null?void 0:n.accounts)??[]:s==="contact"?l=(a==null?void 0:a.data)??(a==null?void 0:a.contacts)??[]:s==="opportunity"?l=(u==null?void 0:u.data)??(u==null?void 0:u.opportunities)??[]:s==="use_case"&&(l=(d==null?void 0:d.data)??(d==null?void 0:d.use_cases)??[]),l.map(i=>({id:i.id,label:f(s,i)}))}function rs({entityType:s,value:o,onChange:t,onSelectItem:r,placeholder:n,clearable:a=!0,disabled:u=!1,className:d}){const[l,i]=m.useState(!1),[x,p]=m.useState(""),b=Z(x,300),N=I(s,o),g=D(s,b,l),_=`Select ${s.replace("_"," ")}…`,v=o?N??"…":n??_,C=!!o,w=c=>{i(c),c||p("")},O=c=>{t(c);const j=g.find(P=>P.id===c);j&&(r==null||r(j)),i(!1),p("")},E=c=>{c.stopPropagation(),t("")};return e.jsxs(V,{open:l,onOpenChange:w,children:[e.jsx(X,{asChild:!0,children:e.jsxs("button",{type:"button",disabled:u,className:h("w-full h-10 px-3 rounded-md border border-border bg-background text-sm outline-none","flex items-center justify-between gap-2","hover:border-ring focus:ring-1 focus:ring-ring transition-colors","disabled:opacity-50 disabled:cursor-not-allowed",d),children:[e.jsx("span",{className:h("flex-1 text-left truncate",!C&&"text-muted-foreground"),children:v}),e.jsxs("span",{className:"flex items-center gap-1 shrink-0",children:[a&&C&&e.jsx(M,{className:"w-3.5 h-3.5 text-muted-foreground hover:text-foreground transition-colors",onClick:E}),e.jsx(W,{className:"w-3.5 h-3.5 text-muted-foreground opacity-60"})]})]})}),e.jsx(B,{className:"p-0 w-[--radix-popover-trigger-width] min-w-[200px]",align:"start",sideOffset:4,children:e.jsxs(F,{shouldFilter:!1,children:[e.jsx(G,{placeholder:`Search ${s.replace("_"," ")}s…`,value:x,onValueChange:p}),e.jsxs(z,{children:[e.jsx(H,{className:"py-4 text-center text-sm text-muted-foreground",children:b?"No results found.":"Start typing to search…"}),e.jsx(J,{children:g.map(c=>e.jsxs(K,{value:c.id,onSelect:()=>O(c.id),className:"flex items-center gap-2",children:[e.jsx(Y,{className:h("w-3.5 h-3.5 shrink-0",o===c.id?"opacity-100":"opacity-0")}),e.jsx("span",{className:"truncate",children:c.label})]},c.id))})]})]})})]})}export{rs as E};
