import{a as v}from"./chunk-NLZ5S4LR.js";import{a,c as n}from"./chunk-VRBUVT76.js";import{b as d}from"./chunk-JKGTJ7SW.js";import{a as u}from"./chunk-7L7QKXDE.js";import{s as y}from"./chunk-HKLJ6VPZ.js";import{d as f,j as h}from"./chunk-72CHOCZD.js";import{P as m,S as g,X as i,ta as o,xa as p}from"./chunk-56X5LYJH.js";var S=class s{constructor(e,r,t,l,c){this.http=e;this.toast=r;this.router=t;this.facilityService=l;this.platformId=c;this.isBrowser=f(this.platformId)}tokenKey="access_token";permissionsKey="permissions";rolesKey="roles";currentUserKey="currentUser";currentFacility=n.currentFacility;currentPeriod=n.currentPeriod;isBrowser;loginUrl=u.auth.login;loading=o(!1);error=o("");serverError=o([]);login(e){return this.loading.set(!0),this.error.set(""),this.serverError.set([]),this.http.post(this.loginUrl,e).pipe(m({next:r=>{let t=r.data;this.isBrowser&&t&&(this.saveAuthData(t.access_token,t.permissions,t.role,{name:t.employee.full_name,avatar:t.employee.image},t.facility,t.financialPeriod),t.facility.id?this.router.navigateByUrl("/home/accounting"):this.router.navigateByUrl("/add-new-facility")),this.loading.set(!1),r.status==!1&&(this.error.set(r?.message||"Login failed"),this.serverError.set(a(r.errorData)),this.loading.set(!1),this.toast.error(this.error()))},error:r=>{this.error.set(r.error?.message||"Login failed"),this.serverError.set(a(r.error.errorData)),this.loading.set(!1),this.toast.error(this.error())}}))}saveAuthData(e,r,t,l,c,I){localStorage.setItem(this.tokenKey,e),localStorage.setItem(this.permissionsKey,JSON.stringify(r)),localStorage.setItem(this.rolesKey,JSON.stringify(t)),localStorage.setItem(this.currentUserKey,JSON.stringify(l)),localStorage.setItem(this.currentFacility,JSON.stringify(c)),localStorage.setItem(this.currentPeriod,JSON.stringify(I))}getToken(){return this.isBrowser?localStorage.getItem(this.tokenKey):null}getPermissions(){return this.isBrowser?JSON.parse(localStorage.getItem(this.permissionsKey)||"[]"):[]}getRoles(){return this.isBrowser?JSON.parse(localStorage.getItem(this.rolesKey)||"[]"):[]}hasPermission(e){return this.getPermissions().includes(e)}hasRole(e){return this.getRoles().includes(e)}logout(){this.isBrowser&&(localStorage.clear(),sessionStorage.clear())}isAuthenticated(){return!!this.getToken()}static \u0275fac=function(r){return new(r||s)(i(h),i(d),i(y),i(v),i(p))};static \u0275prov=g({token:s,factory:s.\u0275fac,providedIn:"root"})};export{S as a};
