import{u as T}from"./LVI_DxwS.js";import{u as E}from"./Cr9kIIpZ.js";import{u as p}from"./DjHBbOhD.js";const D=()=>{const{apiUrl:C}=T(),{handleUnauthorized:h}=E(),f=()=>{const e=p("user_type"),t=p("admin_data",{default:()=>null}),a=p("user_data",{default:()=>null}),s=p("current_etablissement",{default:()=>null}),n=p("name_base",{default:()=>null});let r=null;e.value==="admin"?r=t.value||a.value:r=a.value;const u=r?.matricule||r?.Matricule||r?.matricule_user||null,c=s.value?Number(s.value):null,l=n.value||null;return{matricule:u,idEtablissement:c,nameBase:l}},m=async(e,t={})=>{const s=p("auth_token").value;if(!s)throw console.error("❌ Token manquant pour l'appel API:",e),h(),new Error("Token d'authentification manquant");const{matricule:n,idEtablissement:r,nameBase:u}=f();let c=e.startsWith("http")?e:`${C}${e.startsWith("/")?e:`/${e}`}`;const l=new URLSearchParams;if(n&&l.append("matricule",n),r&&l.append("idEtablissement",r.toString()),u&&l.append("nameBase",u),l.toString()){const i=c.includes("?")?"&":"?";c=`${c}${i}${l.toString()}`}const P={Authorization:`Bearer ${s}`,Accept:"application/json","Content-Type":"application/json",...n&&{"X-Matricule":n},...r&&{"X-Id-Etablissement":r.toString()},...u&&{"X-Name-Base":u},...t.headers};let o=t.body;const d=(t.method||"GET").toUpperCase();(d==="POST"||d==="PUT"||d==="PATCH")&&o&&typeof o=="object"&&!(o instanceof FormData)&&!(o instanceof URLSearchParams)&&(o={...o,...n&&!o.matricule&&{matricule:n},...r&&!o.idEtablissement&&!o.id_etablissement&&{idEtablissement:r},...u&&!o.nameBase&&!o.name_base&&{nameBase:u}});try{const i={...t,headers:P,credentials:"include"};return(d==="POST"||d==="PUT"||d==="PATCH")&&o&&(i.body=o),await $fetch(c,i)}catch(i){throw i?.status===401||i?.statusCode===401?(console.error("❌ Erreur 401: Token invalide ou expiré pour:",e),h(),i):(console.error("❌ API Error:",{endpoint:e,url:c,status:i?.status||i?.statusCode,message:i?.message,data:i?.data}),i)}};return{apiCall:m,apiGet:async(e,t={})=>m(e,{...t,method:"GET"}),apiPost:async(e,t,a={})=>m(e,{...a,method:"POST",body:t}),apiPut:async(e,t,a={})=>m(e,{...a,method:"PUT",body:t}),apiPatch:async(e,t,a={})=>m(e,{...a,method:"PATCH",body:t}),apiDelete:async(e,t={})=>m(e,{...t,method:"DELETE"}),getUserContext:f,addUserContextToHeaders:(e={})=>{const{matricule:t,idEtablissement:a,nameBase:s}=f();return{...e,...t&&{"X-Matricule":t},...a&&{"X-Id-Etablissement":a.toString()},...s&&{"X-Name-Base":s}}},addUserContextToBody:(e={})=>{if(!e||typeof e!="object"||e instanceof FormData||e instanceof URLSearchParams)return e;const{matricule:t,idEtablissement:a,nameBase:s}=f();return{...e,...t&&!e.matricule&&{matricule:t},...a&&!e.idEtablissement&&!e.id_etablissement&&{idEtablissement:a},...s&&!e.nameBase&&!e.name_base&&{nameBase:s}}},addUserContextToQuery:e=>{const{matricule:t,idEtablissement:a,nameBase:s}=f(),n=new URLSearchParams;if(t&&n.append("matricule",t),a&&n.append("idEtablissement",a.toString()),s&&n.append("nameBase",s),n.toString()){const r=e.includes("?")?"&":"?";return`${e}${r}${n.toString()}`}return e}}};export{D as u};
