import{ad as D}from"./primevue-CfIk0_DY.js";import{b as R,s as C}from"./index-BxIwIJfy.js";import{w as b,o as z,b as g,k as K,l as L,c as _,t as x,a7 as U,a as u,y as p,n as G}from"./vue-core-Di3IuVqe.js";const H={key:0,style:{color:"#9ca3af"}},J={key:1,class:"truncate",style:{display:"inline-block","max-width":"100%","vertical-align":"bottom"}},Y={__name:"ArbreMultiSelect",props:{modelValue:{type:Array,default:()=>[]},source:{type:String,required:!0},cle:{type:String,required:!0},libelle:{type:String,default:"libelle"},parent:{type:String,default:""},unite:{type:String,default:"élément"},disabled:{type:Boolean,default:!1},placeholder:{type:String,default:"Tous"},restrictChamp:{type:String,default:""},restrictIds:{type:Array,default:()=>[]}},emits:["change","update:modelValue"],setup(v,{emit:I}){const y=I,s=v,S=u(null),f=u([]),i=u([]),o=u({}),V=()=>{G(()=>S.value?.hide?.())},m=u(new Map),h=u(!1),w=p(()=>s.placeholder),j=p(()=>Object.values(o.value||{}).filter(Boolean).length),A=p(()=>{const e=new Map;for(const t of i.value)e.set(String(Number(t[s.cle])),t[s.libelle]||`#${Number(t[s.cle])}`);return e}),$=p(()=>{const e=[];for(const[t,r]of Object.entries(o.value||{}))r&&e.push(A.value.get(t)||`#${t}`);return e.join(", ")}),B=e=>{const t=new Map;for(const n of e){const a=Number(n[s.cle]);t.set(a,{key:String(a),label:n[s.libelle]||`#${a}`,children:[]})}const r=[];for(const n of e){const a=t.get(Number(n[s.cle])),c=s.parent&&Number(n[s.parent])||0;c&&t.has(c)?t.get(c).children.push(a):r.push(a)}const l=n=>{n.children&&n.children.length?n.children.forEach(l):delete n.children};return r.forEach(l),r},E=e=>{const t=new Map;if(!s.parent)return t;const r=new Map;for(const l of e){const n=Number(l[s.parent])||0;n&&(r.has(n)||r.set(n,[]),r.get(n).push(Number(l[s.cle])))}for(const l of e){const n=Number(l[s.cle]),a=[],c=[...r.get(n)||[]];let P=0;for(;c.length&&P++<1e5;){const N=c.pop();a.push(N);const M=r.get(N);M&&c.push(...M)}t.set(n,a)}return t},O=()=>{const e=(s.restrictIds||[]).map(Number);if(!s.restrictChamp||e.length===0)return i.value;const t=new Set(e);return i.value.filter(r=>t.has(Number(r[s.restrictChamp])))},k=()=>{const e=O();f.value=B(e),m.value=E(e);const t=new Set(e.map(n=>String(Number(n[s.cle])))),r=o.value||{};let l=!1;for(const n of Object.keys(r))r[n]&&!t.has(n)&&(delete r[n],l=!0);l&&(o.value={...r})},T=async()=>{h.value=!0;try{const e=await R.post(`${C.lienApi}/${s.source}/${C.db}`,{condition:""},{headers:{"Content-Type":"application/json"}});i.value=e.data||[],k()}catch{i.value=[],f.value=[],m.value=new Map}finally{h.value=!1}},q=()=>{const e=new Set;for(const[t,r]of Object.entries(o.value||{})){if(!r)continue;const l=parseInt(t,10);if(Number.isFinite(l)){e.add(l);for(const n of m.value.get(l)||[])e.add(n)}}return[...e]},F=(e,t)=>e.length===t.length&&e.every(r=>t.includes(r));let d=[];return b(o,()=>{d=q(),y("update:modelValue",d),y("change",d)}),b(()=>s.modelValue,e=>{const t=(e||[]).map(Number);if(F(t,d))return;const r={};for(const l of t)r[String(l)]=!0;o.value=r},{immediate:!0}),b(()=>s.restrictIds,()=>{i.value.length&&k()},{deep:!0}),z(T),(e,t)=>(g(),K(U(D),{ref_key:"treeRef",ref:S,modelValue:o.value,"onUpdate:modelValue":t[0]||(t[0]=r=>o.value=r),options:f.value,size:"small",selectionMode:"multiple",metaKeySelection:!1,filter:!0,filterMode:"lenient",placeholder:w.value,showClear:!0,disabled:v.disabled,loading:h.value,class:"w-full",panelClass:"arbre-compact",onNodeSelect:V},{value:L(()=>[j.value===0?(g(),_("span",H,x(w.value),1)):(g(),_("span",J,x($.value),1))]),_:1},8,["modelValue","options","placeholder","disabled","loading"]))}};export{Y as _};
