import{b as a,c as r}from"./CF1ujZlL.js";import{b as i}from"./DlhQF7fG.js";function u(e){try{return JSON.parse(atob(e.split(".")[1])).sub??""}catch{return""}}async function o(e,t){r.setTokens(e.access_token,e.refresh_token,e.expires_in);const s=u(e.access_token)||t;if(e.user){r.setUser(e.user.username||s,e.user.fullname||s,e.user.email||"",e.user.avatar_url||"",e.user.content_editor||""),r.setPermissions(e.user.super_admin??!1,e.user.access??{});return}try{const n=await a.get(`/users/${s}`);r.setUser(s,n.fullname||s,n.email||"",n.avatar_url||""),r.setPermissions(n.super_admin??!1,n.access??{})}catch{r.setUser(s,s),r.setPermissions(!1,{})}}async function l(e,t){const s=await a.post("/auth/token",{username:e,password:t});return"requires_2fa"in s&&s.requires_2fa?{requires2fa:!0,challengeToken:s.challenge_token}:(await o(s,e),{requires2fa:!1})}async function p(){const e=await a.get("/auth/setup");return{setup_required:!!e?.setup_required,password_policy:e?.password_policy}}async function m(e){const t=await a.post("/auth/setup",e);await o(t,e.username)}async function _(e,t){const s=await a.post("/auth/2fa/verify",{challenge_token:e,code:t});await o(s,"")}async function y(e){const t=typeof window<"u"?window.location.origin+i:void 0,s={email:e};return t&&(s.admin_base_url=t),a.post("/auth/forgot-password",s)}async function w(e,t,s){return a.post("/auth/reset-password",{username:e,token:t,password:s})}async function h(){try{const e=await a.get("/me");r.setUser(e.username||r.username,e.fullname||r.username,e.email||"",e.avatar_url||void 0,e.content_editor||""),r.setPermissions(e.super_admin??!1,e.access??{}),r.setVersions(e.grav_version,e.admin_version)}catch{}}async function d(){try{r.refreshToken&&await a.post("/auth/revoke",{refresh_token:r.refreshToken})}catch{}finally{r.logout()}}export{h as a,d as b,y as f,p as g,l,w as r,m as s,_ as v};
