import{_ as B,p as M,a as h,V as T,O as $,o as s,c as r,e as y,w as F,v as L,f as D,b as k,j as w,F as E,h as I,x as N,t as S,r as c}from"../index.js";const j={class:"crm-contact-picker__input-wrap"},A=["placeholder"],O=["aria-label"],P={key:0,class:"crm-contact-picker__dropdown"},R=["onMousedown"],U={__name:"ContactPicker",props:{modelValue:{type:Number,default:null},placeholder:{type:String,default:()=>k("Search contacts…","pressio-crm")}},emits:["update:modelValue"],setup(f,{emit:V}){const i=f,d=V,t=c(""),u=c(!1),a=c([]),n=c(null),m=c(null);let p=null;function v(e){const l=[e.first_name,e.last_name].filter(Boolean).join(" ");return e.email?`${l} — ${e.email}`:l}async function b(){if(!t.value.trim()){a.value=[];return}try{const e=await h("contacts",{params:{search:t.value.trim(),per_page:10,page:1}});a.value=Array.isArray(e)?e:(e==null?void 0:e.items)||[]}catch{a.value=[]}}function g(){clearTimeout(p),p=setTimeout(b,300),u.value=!0,n.value=null,d("update:modelValue",null)}function C(e){n.value=e,t.value=v(e),u.value=!1,a.value=[],d("update:modelValue",e.id)}function x(){n.value=null,t.value="",a.value=[],d("update:modelValue",null)}function _(e){m.value&&!m.value.contains(e.target)&&(u.value=!1)}return M(async()=>{if(document.addEventListener("mousedown",_),i.modelValue)try{const e=await h(`contacts/${i.modelValue}`);e!=null&&e.id&&(n.value=e,t.value=v(e))}catch{}}),T(()=>{document.removeEventListener("mousedown",_),clearTimeout(p)}),$(()=>i.modelValue,e=>{e||(n.value=null,t.value="",a.value=[])}),(e,l)=>(s(),r("div",{ref_key:"containerRef",ref:m,class:"crm-contact-picker"},[y("div",j,[F(y("input",{"onUpdate:modelValue":l[0]||(l[0]=o=>t.value=o),type:"text",class:"crm-input",placeholder:f.placeholder,onInput:g,onFocus:l[1]||(l[1]=o=>u.value=!0)},null,40,A),[[L,t.value]]),n.value?(s(),r("button",{key:0,type:"button",class:"crm-contact-picker__clear","aria-label":D(k)("Clear","pressio-crm"),onClick:x}," × ",8,O)):w("",!0)]),u.value&&a.value.length>0?(s(),r("ul",P,[(s(!0),r(E,null,I(a.value,o=>(s(),r("li",{key:o.id,class:"crm-contact-picker__option",onMousedown:N(q=>C(o),["prevent"])},S(v(o)),41,R))),128))])):w("",!0)],512))}},G=B(U,[["__scopeId","data-v-b74fd16c"]]);export{G as C};
