import{_ as N,y as q,o as s,c as u,t as m,g as k,b as d,e as V,v as D,J as f,w as b,K as R,F as B,s as z,A as H,r as v,x as F}from"./index-Dd5IVJne.js";const J={class:"client-combobox"},U=["for"],j={class:"client-combobox-wrap"},G=["id","placeholder","aria-expanded","disabled","onKeydown"],O={class:"client-combobox-list",role:"listbox"},P={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 y=n,g=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)");q(()=>y.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 y.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 A(){g("update:modelValue",""),i.value=!0,w(!1)}function T(){i.value=!0,w(!0)}function $(t){const e=t.phone||t.email||"—";return`${t.name} — ${e}`}function E(t){g("update:modelValue",t.id),r.value=$(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 K(){a.value>=0&&l.value[a.value]&&E(l.value[a.value])}function L(){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,U)):k("",!0),d("div",j,[V(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:A,onFocus:T,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(K,["prevent"]),["enter"]),e[3]||(e[3]=f(o=>i.value=!1,["escape"]))],onBlur:L},null,40,G),[[D,r.value]]),V(d("ul",O,[c.value?(s(),u("li",P,"Buscando…")):(s(),u(B,{key:1},[h.value?(s(),u("li",Q,m(h.value),1)):k("",!0),(s(!0),u(B,null,z(l.value,(o,M)=>(s(),u("li",{key:o.id,role:"option",class:H(["client-combobox-li",{"is-active":M===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),[[R,i.value&&(c.value||h.value||l.value.length>0)]])])]))}},le=N(Z,[["__scopeId","data-v-75458bcb"]]);export{le as C};
