import{f as m}from"./chunk-QWKADPWV.js";import{a as y}from"./chunk-2Y3X2RE2.js";import{d}from"./chunk-E6DFULXS.js";import{O as h,S as w,e as g}from"./chunk-KBLF2WKH.js";import{f as n}from"./chunk-S5EYVFES.js";var U=(()=>{let c=class c{constructor(t,r,o){this.mysqlService=t,this.authService=r,this.logger=o,this.followingSubject=new g([]),this.following$=this.followingSubject.asObservable(),this.authService.currentUser$.subscribe(e=>{e?this.loadFollowing():e!==void 0&&this.followingSubject.next([])})}followUser(t){return n(this,null,function*(){let r=yield this.authService.ensureInitialized();if(!r||r.id===t)throw new Error("Impossible de se suivre soi-m\xEAme ou utilisateur non connect\xE9");try{let{error:o}=yield this.mysqlService.followUser(r.id,t);if(o)throw o;yield this.logger.logFollow(t,"follow");let e=this.followingSubject.value;this.followingSubject.next([...e,t])}catch(o){throw console.error("Erreur lors du suivi de l'utilisateur:",o),o}})}unfollowUser(t){return n(this,null,function*(){let r=yield this.authService.ensureInitialized();if(!r)throw new Error("Utilisateur non connect\xE9");try{let{error:o}=yield this.mysqlService.unfollowUser(r.id,t);if(o)throw o;yield this.logger.logFollow(t,"unfollow");let e=this.followingSubject.value;this.followingSubject.next(e.filter(i=>i!==t))}catch(o){throw console.error("Erreur lors de l'arr\xEAt du suivi:",o),o}})}isFollowing(t){return this.followingSubject.value.includes(t)}getFollowStats(t){return n(this,null,function*(){let r=yield this.authService.ensureInitialized();try{let{data:o,error:e}=yield this.mysqlService.getFollowersCount(t);if(e)throw e;let{data:i,error:a}=yield this.mysqlService.getFollowingCount(t);if(a)throw a;let s=!1;if(r&&r.id!==t){let{data:l,error:u}=yield this.mysqlService.isFollowing(r.id,t);!u&&l&&(s=!0)}return{followersCount:o||0,followingCount:i||0,isFollowing:s}}catch(o){return console.error("Erreur lors de la r\xE9cup\xE9ration des stats de follow:",o),{followersCount:0,followingCount:0,isFollowing:!1}}})}getUserProfile(t){return n(this,null,function*(){try{let{data:r,error:o}=yield this.mysqlService.getUsersByIds([t]);if(o)return console.error("Erreur profil:",o),null;let e;Array.isArray(r?.users)?e=r.users:Array.isArray(r)?e=r:Array.isArray(r?.data)?e=r.data:e=[];let i=e?.[0];if(!i)return console.warn("Profil introuvable pour",t),null;let{data:a,error:s}=yield this.mysqlService.getUserFailsCount(t);s&&console.error("Erreur fails count:",s);let l=(a||0)*10,u=yield this.getFollowStats(t);return{id:i.id,display_name:i.display_name||"Utilisateur",avatar_url:i.avatar_url||"assets/profil/base.png",bio:i.bio,totalFails:a||0,couragePoints:l,followersCount:u.followersCount,followingCount:u.followingCount,isFollowing:u.isFollowing,joinedAt:new Date(i.created_at)}}catch(r){return console.error("Erreur lors de la r\xE9cup\xE9ration du profil utilisateur:",r),null}})}loadFollowing(){return n(this,null,function*(){let t=yield this.authService.ensureInitialized();if(t)try{let{data:r,error:o}=yield this.mysqlService.getFollowing(t.id);if(o)throw o;let e=r?.map(i=>i.following_id)||[];this.followingSubject.next(e)}catch(r){console.error("Erreur lors du chargement des follows:",r),this.followingSubject.next([])}})}getFollowing(t){return n(this,null,function*(){let r=t||(yield this.authService.ensureInitialized())?.id;if(!r)return[];try{let{data:o,error:e}=yield this.mysqlService.getFollowing(r);if(e)throw e;let i=o?.map(l=>l.following_id)||[];if(i.length===0)return[];let{data:a,error:s}=yield this.mysqlService.getUsersByIds(i);if(s)throw s;return a?.map(l=>({id:l.id,display_name:l.display_name||"Utilisateur",avatar_url:l.avatar_url||"assets/profil/base.png",bio:l.bio,totalFails:0,couragePoints:0,followersCount:0,followingCount:0,joinedAt:new Date}))||[]}catch(o){return console.error("Erreur lors de la r\xE9cup\xE9ration des following:",o),[]}})}getFollowers(t){return n(this,null,function*(){let r=t||(yield this.authService.ensureInitialized())?.id;if(!r)return[];try{let{data:o,error:e}=yield this.mysqlService.getFollowers(r);if(e)throw e;let i=o?.map(l=>l.follower_id)||[];if(i.length===0)return[];let{data:a,error:s}=yield this.mysqlService.getUsersByIds(i);if(s)throw s;return a?.map(l=>({id:l.id,display_name:l.display_name||"Utilisateur",avatar_url:l.avatar_url||"assets/profil/base.png",bio:l.bio,totalFails:0,couragePoints:0,followersCount:0,followingCount:0,joinedAt:new Date}))||[]}catch(o){return console.error("Erreur lors de la r\xE9cup\xE9ration des followers:",o),[]}})}};c.\u0275fac=function(r){return new(r||c)(w(d),w(m),w(y))},c.\u0275prov=h({token:c,factory:c.\u0275fac,providedIn:"root"});let f=c;return f})();export{U as a};
