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