import{_ as q,z as D,o as s,c as u,t as m,g as k,b as d,e as B,v as K,J as f,w as b,P as z,F as V,C as R,B as H,r as v,x as F}from"./index-FrXTkRdH.js";const J={class:"client-combobox"},P=["for"],U={class:"client-combobox-wrap"},j=["id","placeholder","aria-expanded","disabled","onKeydown"],G={class:"client-combobox-list",role:"listbox"},O={key:0,class:"client-combobox-li muted"},Q={key:0,class:"client-combobox-li muted"},W=["onMousedown"],X={class:"client-combobox-name"},Y={class:"client-combobox-meta"},Z={__name:"ClientSearchCombobox",props:{modelValue:{type:[String,Number],default:""},fetchClients:{type:Function,required:!0},label:{type:String,default:"Cliente"},placeholder:{type:String,default:"Escribí nombre, teléfono o correo y elegí de la lista"},inputId:{type:String,default:"client-combobox"},disabled:{type:Boolean,default:!1}},emits:["update:modelValue"],setup(n,{emit:I}){const g=n,y=I,r=v(""),i=v(!1),l=v([]),c=v(!1),a=v(-1);let x=null,p=null;const C=F(()=>r.value.trim()),h=F(()=>c.value||l.value.length>0?"":C.value.length>0?"Sin coincidencias":"Escribí para filtrar (o elegí de la lista al cargar)");D(()=>g.modelValue,t=>{(t===""||t==null)&&(r.value="")});function w(t){if(clearTimeout(x),t){_();return}x=setTimeout(()=>_(),320)}async function _(){p?.abort(),p=new AbortController;const t=p.signal;c.value=!0;try{const e=await g.fetchClients(C.value,t);l.value=Array.isArray(e)?e:[],a.value=l.value.length>0?0:-1}catch(e){if(e?.code==="ERR_CANCELED"||e?.name==="CanceledError"||e?.name==="AbortError")return;l.value=[],a.value=-1}finally{c.value=!1}}function T(){y("update:modelValue",""),i.value=!0,w(!1)}function $(){i.value=!0,w(!0)}function A(t){const e=t.phone||t.email||"—";return`${t.name} — ${e}`}function E(t){y("update:modelValue",t.id),r.value=A(t),i.value=!1,l.value=[],a.value=-1}function S(t){if(!l.value.length)return;let e=a.value+t;e<0&&(e=l.value.length-1),e>=l.value.length&&(e=0),a.value=e}function L(){a.value>=0&&l.value[a.value]&&E(l.value[a.value])}function M(){setTimeout(()=>{i.value=!1},180)}return(t,e)=>(s(),u("div",J,[n.label?(s(),u("label",{key:0,for:n.inputId},m(n.label),9,P)):k("",!0),d("div",U,[B(d("input",{id:n.inputId,"onUpdate:modelValue":e[0]||(e[0]=o=>r.value=o),type:"text",class:"input-control",placeholder:n.placeholder,autocomplete:"off",spellcheck:"false",role:"combobox","aria-expanded":i.value,"aria-autocomplete":"list",disabled:n.disabled,onInput:T,onFocus:$,onKeydown:[e[1]||(e[1]=f(b(o=>S(1),["prevent"]),["down"])),e[2]||(e[2]=f(b(o=>S(-1),["prevent"]),["up"])),f(b(L,["prevent"]),["enter"]),e[3]||(e[3]=f(o=>i.value=!1,["escape"]))],onBlur:M},null,40,j),[[K,r.value]]),B(d("ul",G,[c.value?(s(),u("li",O,"Buscando…")):(s(),u(V,{key:1},[h.value?(s(),u("li",Q,m(h.value),1)):k("",!0),(s(!0),u(V,null,R(l.value,(o,N)=>(s(),u("li",{key:o.id,role:"option",class:H(["client-combobox-li",{"is-active":N===a.value}]),onMousedown:b(ee=>E(o),["prevent"])},[d("span",X,m(o.name),1),d("span",Y,m(o.phone||o.email||"—"),1)],42,W))),128))],64))],512),[[z,i.value&&(c.value||h.value||l.value.length>0)]])])]))}},le=q(Z,[["__scopeId","data-v-75458bcb"]]);export{le as C};
