import{a as m,b as f}from"./chunk-RC3JXGN6.js";import{a as A}from"./chunk-52JZ5I3Y.js";import{n as v,w as S}from"./chunk-COFYR5MX.js";import{D as r,W as i,_ as d,a as c,b as g,da as o,o as s,ta as p}from"./chunk-2MUIW2M6.js";var b=class u{http=o(v);storageService=o(f);toastService=o(A);router=o(S);API_URL=`${m.apiUrl}/auth`;authStateSignal=p({user:this.storageService.getUserData(),isAuthenticated:this.storageService.isAuthenticated(),isLoading:!1,error:null});authState=this.authStateSignal.asReadonly();register(e,t,l,n){this.setLoading(!0);let h={username:e,email:t,password:l,guestSessionId:n};return this.http.post(`${this.API_URL}/register`,h).pipe(i(a=>{this.storageService.setToken(a.data.token,!0),this.storageService.setUserData(a.data.user),n&&this.storageService.clearGuestToken(),this.updateAuthState(a.data.user,null);let U=a.migratedStats?`Welcome ${a.data.user.username}! Your guest progress has been saved.`:`Welcome ${a.data.user.username}! Your account has been created.`;this.toastService.success(U),this.router.navigate(["/categories"])}),r(a=>(this.handleAuthError(a),s(()=>a))))}login(e,t,l=!1,n="/categories"){this.setLoading(!0);let h={email:e,password:t};return this.http.post(`${this.API_URL}/login`,h).pipe(i(a=>{console.log(a.data.user),this.storageService.setToken(a.data.token,l),this.storageService.setUserData(a.data.user),this.storageService.clearGuestToken(),this.updateAuthState(a.data.user,null),this.toastService.success(`Welcome back, ${a.data.user.username}!`),this.router.navigate([n])}),r(a=>(this.handleAuthError(a),s(()=>a))))}logout(){return this.setLoading(!0),this.http.post(`${this.API_URL}/logout`,{}).pipe(i(()=>{this.storageService.clearAll(),this.authStateSignal.set({user:null,isAuthenticated:!1,isLoading:!1,error:null}),this.toastService.success("You have been logged out successfully."),this.router.navigate(["/login"])}),r(e=>(this.storageService.clearAll(),this.authStateSignal.set({user:null,isAuthenticated:!1,isLoading:!1,error:null}),this.router.navigate(["/login"]),s(()=>e))))}verifyToken(){return this.storageService.getToken()?(this.setLoading(!0),this.http.get(`${this.API_URL}/verify`).pipe(i(t=>{t.success&&t.data.user?(this.storageService.setUserData(t.data.user),this.updateAuthState(t.data.user,null)):this.clearAuth()}),r(t=>(this.clearAuth(),s(()=>t))))):(this.authStateSignal.update(t=>g(c({},t),{isAuthenticated:!1,user:null})),s(()=>new Error("No token found")))}clearAuth(){this.storageService.clearToken(),this.storageService.clearUserData(),this.authStateSignal.set({user:null,isAuthenticated:!1,isLoading:!1,error:null})}updateAuthState(e,t){this.authStateSignal.set({user:e,isAuthenticated:!!e,isLoading:!1,error:t})}setLoading(e){this.authStateSignal.update(t=>g(c({},t),{isLoading:e}))}handleAuthError(e){let t="An error occurred. Please try again.";e.status===400?t="Invalid input. Please check your information.":e.status===401?t="Invalid email or password.":e.status===409?t=e.error?.message||"Email or username already exists.":e.status===429?t="Too many attempts. Please try again later.":e.status===0&&(t="Unable to connect to server. Please check your internet connection."),this.updateAuthState(null,t),this.toastService.error(t)}getCurrentUser(){return this.authStateSignal().user}isAuthenticated(){return this.authStateSignal().isAuthenticated}isAdmin(){return this.getCurrentUser()?.role==="admin"}static \u0275fac=function(t){return new(t||u)};static \u0275prov=d({token:u,factory:u.\u0275fac,providedIn:"root"})};export{b as a};