import{a as P}from"./chunk-KZKY3ROD.js";import{a as k}from"./chunk-W45AUQWS.js";import{a as D}from"./chunk-ZV4EAG4P.js";import{e as U}from"./chunk-BGV4AOSB.js";import{a as T,e as G,g as m,p as r,v as N}from"./chunk-ZTKV4AIV.js";import{b as C}from"./chunk-5JDCLYZA.js";import{h as O}from"./chunk-73IMXZSJ.js";import{E as I,K as c,aa as p,bb as i,ca as g,cb as E,h,ha as S,i as l,na as d,p as a,u as o}from"./chunk-FWZCAEAJ.js";import{a as L,b as w}from"./chunk-CWTPBX7D.js";var _="/auth/login",A="/auth/passwordChange",R="/auth/resetPassword",x="/auth/resetPassword/update",W="/auth/refresh",M=(()=>{let s=class s{static getSessionInformation(){let t=N();return t.name+" - "+T(t.majorVersion)+"; OS: "+t.os+"; Phone: "+T(t.mobile)}constructor(){this.httpClient=d(O),this.notificationService=d(D),this.window=U(),this.triggerLogin=new h,this.triggerLoginWithPwChange=new h,this.triggerLogout=new h,this.authState=E({status:"LOGGED_OUT"}),this.refreshTokenLoad=new l(m("refreshToken")),this.accessTokenLoad=new l(m("accessToken")),this.triggerLoginError=new h,this.login$=this.triggerLogin.pipe(p(({email:t,password:e})=>this.httpClient.post(_,{email:t,password:e,sessionInformation:s.getSessionInformation(),stayLoggedIn:!0}).pipe(c(n=>{let f=n?.error?.codeName;return f==="ACCOUNT_NOT_ACTIVATED"||f==="PASSWORD_CHANGE_REQUIRED"?(this.triggerLoginError.next(f),a(void 0)):(this.notificationService.terror("ABOUT_SIGNIN_FAILED"),a(void 0))}))),k(),o(({accessToken:t,refreshToken:e})=>({accessToken:t,refreshToken:e,status:"LOGGED_IN"})),g(t=>{r("refreshToken",t.refreshToken),r("accessToken",t.accessToken)})),this.loginWithPwChange$=this.triggerLoginWithPwChange.pipe(p(({email:t,newPassword:e,oldPassword:n})=>this.httpClient.post(A,{email:t,oldPassword:n,newPassword:e,sessionInformation:s.getSessionInformation(),stayLoggedIn:!0}).pipe(c(()=>(this.notificationService.terror("ABOUT_SIGNIN_FAILED"),a(void 0))))),k(),o(({accessToken:t,refreshToken:e})=>({accessToken:t,refreshToken:e,status:"LOGGED_IN"})),g(t=>{r("refreshToken",t.refreshToken),r("accessToken",t.accessToken)})),this.logout$=this.triggerLogout.pipe(p(()=>this.httpClient.post("/auth/logout",{refreshToken:this.refreshToken()}).pipe(c(()=>(this.clearStorage(),this.window?.location.reload(),a({status:"LOGGED_OUT"}))))),g(()=>{this.clearStorage(),this.window?.location.reload()}),o(()=>({status:"LOGGED_OUT"}))),this.refreshToken=i(()=>this.authState().refreshToken),this.accessToken=i(()=>this.authState().accessToken),this.status=i(()=>this.authState().status),this.status$=C(this.status),this.loginError=i(()=>this.authState().loginError),this.redirectUrl=i(()=>this.authState().redirectUrl),this.sendPasswordResetRequest=t=>this.httpClient.post(R,{email:t}),this.sendPasswordReset=(t,e,n)=>this.httpClient.post(x,{email:t,resetToken:e,newPassword:n}),P(this.authState,I(this.triggerLogin.pipe(o(()=>({status:"LOADING",loginError:void 0}))),this.triggerLoginWithPwChange.pipe(o(()=>({status:"LOADING",loginError:void 0}))),this.login$,this.loginWithPwChange$,this.logout$,this.triggerLoginError.pipe(o(t=>({loginError:t,status:"ERROR"}))),this.refreshTokenLoad.pipe(o(t=>({refreshToken:t,status:t?"LOGGED_IN":"LOGGED_OUT"}))),this.accessTokenLoad.pipe(o(t=>({accessToken:t})))))}setRefreshToken(t){r("refreshToken",t),this.refreshTokenLoad.next(t)}setJWTToken(t){r("accessToken",t),this.accessTokenLoad.next(t)}setRedirectUrl(t){this.authState.update(e=>w(L({},e),{redirectUrl:t}))}refreshAccessToken(){return this.httpClient.post(W,{refreshToken:this.refreshToken(),sessionInformation:s.getSessionInformation()}).pipe(g(t=>{this.setJWTToken(t.accessToken),this.setRefreshToken(t.refreshToken)}))}logout(){this.triggerLogout.next(this.refreshToken())}clearStorage(){this.setRefreshToken(void 0),this.setJWTToken(void 0),G()}isAuthenticated(){return this.status()==="LOGGED_IN"}};s.\u0275fac=function(e){return new(e||s)},s.\u0275prov=S({token:s,factory:s.\u0275fac,providedIn:"root"});let u=s;return u})();export{_ as a,A as b,R as c,x as d,W as e,M as f};
