import{c as e,d as t,g as n,h as r,n as i,p as a,s as o}from"./apiFetch-C5jBrj9m.js";import{t as s}from"./imageUploadService-CCRAfVfv.js";var c=r();function l(t){if(!t)return null;if(t.startsWith(`data:`))return t;let n=t;if(t.startsWith(`http://localhost:3000/`)){let r=t.replace(`http://localhost:3000`,``);(r.startsWith(`/api/v1/avatars`)||r.startsWith(`/avatars/`))&&(n=`${e.API_GATEWAY.URL}${r}`)}else t.startsWith(`/avatars/`)?n=`${e.API_GATEWAY.URL}/api/v1${t}`:t.startsWith(`/api/v1/avatars`)&&(n=`${e.API_GATEWAY.URL}${t}`);return(n.includes(`/api/v1/avatars`)||n.includes(`/avatars/`))&&!n.includes(`?v=`)&&(n=`${n}?v=${Math.floor(Date.now()/864e5)}`),n}function u(e,t){return{id:e.id||t,firstname:e.firstname||``,lastname:e.lastname||``,pseudonyme:e.pseudonyme||e.pseudoname||``,email:e.email||``,role:e.role||``,avatar_url:l(e.avatar_url||e.profile_picture_url),is_organizer:e.is_organizer??e.isOrganizer??!1,isOrganizer:e.is_organizer??e.isOrganizer??!1,phone_number:e.phone||e.phone_number||null,email_verified:e.email_verified||!1,phone_verified:e.phone_verified||!1,created_at:e.created_at,updated_at:e.updated_at,last_login_at:e.last_login_at||null,auth_type:e.auth_type||`email`,preferences:{...e.preferences,language:e.preferences?.language||`fr`,timezone:e.preferences?.timezone||`Europe/Paris`,email_notifications:e.preferences?.email_notifications??!0,push_notifications:e.preferences?.push_notifications??!0,sms_notifications:e.preferences?.sms_notifications??!1,public_profile:e.preferences?.public_profile??!0,show_real_name:e.preferences?.show_real_name??!1,show_email:e.preferences?.show_email??!1,show_phone:e.preferences?.show_phone??!1}}}var d={getCurrentUserProfile:async()=>{try{let e=d.getUserIDFromToken();if(!e)return d.createBasicProfileFromToken();let t=await i(`${c}/${e}`),r=u(t.user||t,e);if(!r.role)try{let e=n.get(a.COOKIE_NAME);e&&(r.role=JSON.parse(atob(e.split(`.`)[1])).role||``)}catch{}return r}catch{return d.createBasicProfileFromToken()}},getUserIDFromToken:()=>{try{let e=n.get(a.COOKIE_NAME);if(!e)return null;let t=JSON.parse(atob(e.split(`.`)[1]));return t.userID||t.sub||t.user_id||null}catch{return null}},createBasicProfileFromToken:()=>{try{let t=n.get(a.COOKIE_NAME);if(!t)throw Error(`No token`);let r=JSON.parse(atob(t.split(`.`)[1])),i=r.email||r.sub,o=i?.includes(`@`)?i.split(`@`)[0]:null,s=r.role||r.roles?.[0];return{id:r.userID||r.sub||r.user_id||r.id,firstname:r.firstname||r.given_name||r.name?.split(` `)[0]||`Utilisateur`,lastname:r.lastname||r.family_name||r.name?.split(` `).slice(1).join(` `)||``,pseudonyme:r.pseudonyme||r.username||r.preferred_username||o||`utilisateur`,email:i||`user@example.com`,role:s,is_organizer:r.is_organizer??s===`organizer`,isOrganizer:r.is_organizer??s===`organizer`,avatar_url:(()=>{let t=r.picture||r.avatar_url;return t?t.startsWith(`/avatars/`)?`${e.API_GATEWAY.URL}/api/v1${t}`:t:null})(),phone_number:r.phone_number||r.phone||null,preferences:{language:r.locale||`fr`,timezone:`Europe/Paris`,email_notifications:!0,push_notifications:!0,sms_notifications:!1,public_profile:!0,show_real_name:!1,show_email:!1,show_phone:!1}}}catch{return{firstname:`Utilisateur`,lastname:``,pseudonyme:`utilisateur`,email:`user@example.com`,role:`player`,avatar_url:null,phone_number:null,preferences:{language:`fr`,timezone:`Europe/Paris`,email_notifications:!0,push_notifications:!0,sms_notifications:!1,public_profile:!0,show_real_name:!1,show_email:!1,show_phone:!1}}}},updateUserProfile:async e=>{let t=d.getUserIDFromToken();if(!t)throw Error(`Impossible de récupérer l'ID utilisateur`);let n=await i(`${c}/${t}`,{method:`PUT`,body:e});return u(n.user||n)},updateUserRole:async(e,t)=>{await i(`${c}/${e}`,{method:`PUT`,body:{role:t}})},becomeOrganizer:async()=>{await i(`${c}/me/become-organizer`,{method:`POST`})},updateUserStatus:async(e,t)=>{await i(`${c}/${e}`,{method:`PUT`,body:{status:t}})},deleteUserById:async e=>{await i(`${c}/${e}`,{method:`DELETE`})},changePassword:async(e,t)=>{let n=d.getUserIDFromToken();if(!n)throw Error(`Impossible de récupérer l'ID utilisateur`);await i(`${c}/${n}/change-password`,{method:`PUT`,body:{current_password:e,new_password:t}})},uploadProfilePicture:async t=>{let n=await s.optimizeImage(t,400,400,.8),r=await new Promise((e,t)=>{let r=new FileReader;r.onload=()=>e(r.result.split(`,`)[1]),r.onerror=()=>t(Error(`Erreur lecture fichier`)),r.readAsDataURL(n)}),a=d.getUserIDFromToken();if(!a)throw Error(`Impossible de récupérer l'ID utilisateur`);let o=await i(`${c}/${a}/avatar`,{method:`POST`,body:{avatar_data:r,avatar_filename:n.name}}),l=o.avatar_url||o.url;return l?.startsWith(`/avatars/`)?`${e.API_GATEWAY.URL}/api/v1${l}`:l},deleteProfilePicture:async()=>{await d.updateUserProfile({avatar_url:``})},getUserStats:async()=>i(`${c}/profile/stats`),updateUserPreferences:async e=>{await i(`${c}/profile/preferences`,{method:`PUT`,body:{preferences:e}})},getUserById:async e=>{let t=await i(`${c}/${e}`);return u(t.user||t,e)},getAllUsers:async()=>((await i(`${c}`)).users||[]).map(e=>{let t=e.status;typeof t==`string`&&t.toLowerCase().startsWith(`user_status_`)&&(t=t.toLowerCase().replace(`user_status_`,``));let n=e.is_organizer??e.isOrganizer??!1;return{...u(e),status:t,is_organizer:n,isOrganizer:n}}),deleteAccount:async e=>{let t=d.getUserIDFromToken();if(!t)throw Error(`Impossible de récupérer l'ID utilisateur`);await i(`${c}/${t}`,{method:`DELETE`,body:e?{password:e}:void 0}),n.remove(a.COOKIE_NAME)},createUser:async e=>{let t=await i(`${c}`,{method:`POST`,body:e});return u(t.user||t)},switchToEmail:async e=>{await i(`${t.API_BASE}/auth/switch-to-email`,{method:`PUT`,body:{new_password:e},timeout:o.TIMEOUTS.DEFAULT})}};export{l as n,d as t};