import{c as v}from"./chunk-ZMR6MHQ3.js";import{k as f,o as g}from"./chunk-QBEVFUYY.js";import{V as m,Z as l,ca as a,ma as d,p as h,ua as s,xa as p}from"./chunk-QXTL7CSW.js";var c=class o{document=a(d);currentTenantId=s(this.discoverTenant());discoverTenant(){let r=this.document.location.hostname,t=this.document.location.pathname,e=r.split(".")[0];if(e&&!["localhost","www","app"].includes(e))return e;let n=t.split("/")[1];if(n&&!["admin","dashboard","login",""].includes(n))return n;let u=new URLSearchParams(this.document.location.search).get("tenant");return u||"default"}static \u0275fac=function(t){return new(t||o)};static \u0275prov=l({token:o,factory:o.\u0275fac,providedIn:"root"})};var I={production:!1,apiUrl:"http://localhost:8080/api",mockAPI:"http://localhost:3000",VAPID_PUBLIC_KEY:"YOUR_PUBLIC_VAPID_KEY"};var S=class o{http=a(g);tenantService=a(c);API_URL=I.mockAPI+"/users";platformId=a(p);translocoService=a(v);currentUser=s(null);isAuthenticated=s(!1);constructor(){this.restoreSession()}authenticate(r,t){return this.http.get(`${this.API_URL}?email=${r}&password=${t}`).pipe(h(e=>{let n=e[0];if(!n)throw new Error(this.translocoService.translate("auth.login.invalidCredentials"));let i=this.tenantService.currentTenantId();if(console.log(`${this.translocoService.translate("auth.login.userTenant")}: ${n.tenantId} | ${this.translocoService.translate("auth.login.urlTenant")}: ${i}`),n.tenantId!==i&&i!=="default")throw new Error(`${this.translocoService.translate("auth.login.accessDenied")} ${n.tenantId}.`);return n}),m(e=>this.establishSession(e,e.token||"mock-jwt-token")))}establishSession(r,t){localStorage.setItem("auth_token",t),localStorage.setItem("user_profile",JSON.stringify(r)),this.currentUser.set(r),this.isAuthenticated.set(!0)}restoreSession(){if(f(this.platformId)){let r=localStorage.getItem("auth_token"),t=localStorage.getItem("user_profile");if(r&&t)try{let e=JSON.parse(t);return this.currentUser.set(e),this.isAuthenticated.set(!0),!0}catch(e){return console.error(this.translocoService.translate("auth.login.sessionFailed"),e),this.logout(),!1}}return!1}logout(){localStorage.removeItem("auth_token"),localStorage.removeItem("user_profile"),this.currentUser.set(null),this.isAuthenticated.set(!1)}getToken(){return localStorage.getItem("auth_token")}static \u0275fac=function(t){return new(t||o)};static \u0275prov=l({token:o,factory:o.\u0275fac,providedIn:"root"})};export{S as a};
