import{am as l,u as n,W as d,b2 as c,J as h,$ as p}from"#entry";const U=l("user",{state:()=>({userData:null,isLoggedIn:!1,hasInitialized:!1}),actions:{async initializeUserOnPageLoad(){await this.authenticate(),this.hasInitialized=!0},async authenticate(){const e=c("UserData")??null,t=new URLSearchParams(window?.location?.search??""),o=t.get("token")??e?.value?.access_token;if(e&&(this.userData=e.value),o&&await this.fetchUserData(o),t.get("token")&&this.isLoggedIn){const s=p();await s.replace({...s.currentRoute,query:{token:void 0}})}n().public.enableAutomatedLogin==="true"&&!this.isLoggedIn&&await this.authenticateWithIDP()},async fetchUserData(e){const t=c("UserData")??null,{apiBaseUrl:o}=n().public,s=await $fetch(`/api/maxon/get-info?token=${e}`,{baseURL:o,credentials:"include"});s?(s.access_token=e,this.userData=s,t.value=s,this.isLoggedIn=!0):(this.userData=null,t.value=null)},async authenticateWithIDP(){const{authIDPUrl:e}=n().public;await h(),(await $fetch(`${e}/graph/me`,{method:"GET",credentials:"include"})).resultCode==="Success"&&this.login()},login(){const e=this.getLoginUrl(),t=`${e.URI}?response_type=${e.responseType}&client_id=${e.clientId}&redirect_uri=${e.redirectURI}&scope=${e.scope}`;window.open(t,"_self")},logout(){const{apiBaseUrl:e,authLogoutUrl:t,authIDPUrl:o,authClientId:s}=n().public;try{const i=typeof t=="string"?t.startsWith("http")?t:`${e}/${t}`:e,a={URI:`${o}/logout`,clientId:s,redirectURI:encodeURIComponent(`${i}?ret=/`)},r=`${a.URI}?triggerSingleSignout=true&redirect_uri=${a.redirectURI}&response_type=code&oauth_consumer_key=${a.clientId}`;window.open(r,"_self")}catch(i){console.error(i)}},clearSession(){this.userData=null,this.isLoggedIn=!1;const e=c("UserData");e.value=null;const t=new URL(window.location.toString());t.search="";const o=t.toString();window.location.href=o},getLoginUrl(){const{authIDPUrl:e,apiBaseUrl:t,authClientId:o,authRedirectUrl:s}=n().public,a=d().path.split("/").filter(u=>u!==""),r=a[0]?.length===2?a.slice(1).join("/"):a.join("/");return{URI:`${e}/oauth2/authz`,clientId:o,redirectURI:encodeURIComponent(`${t}/${s}?ret=/${r}`),scope:encodeURIComponent("openid profile email"),responseType:encodeURIComponent("code")}}},getters:{getUserData(e){return e.userData}}});export{U as u};