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