import{Kt as e,X as t,bt as n,vt as r}from"./xY0c-UfG.js";import{Y as i,g as a}from"./BW-sf0N4.js";function o(){return i(`/api/auth/status`)}function s(){return i(`/api/auth/me`)}function c(e,t){return i(`/api/auth/login`,{method:`POST`,body:JSON.stringify({email:e,password:t})})}function l(){return i(`/api/auth/logout`,{method:`POST`})}function u(e,t,n){return i(`/api/auth/setup`,{method:`POST`,body:JSON.stringify({email:e,password:t,displayName:n})})}function d(){return i(`/api/auth/users`)}function f(e,t,n,r){return i(`/api/auth/users`,{method:`POST`,body:JSON.stringify({email:e,password:t,displayName:n,role:r})})}function p(e){return i(`/api/auth/users/${e}/deletion-preview`)}function m(e){return i(`/api/auth/users/${e}`,{method:`DELETE`})}function h(e){return i(`/api/auth/me`,{method:`PATCH`,body:JSON.stringify(e)})}function g(e,t){return i(`/api/auth/users/${e}/password`,{method:`PATCH`,body:JSON.stringify({password:t})})}function _(e,t){return i(`/api/auth/users/${e}`,{method:`PATCH`,body:JSON.stringify(t)})}function v(){return i(`/api/auth/tokens`)}function y(e){return i(`/api/auth/tokens`,{method:`POST`,body:JSON.stringify({name:e})})}function b(e){return i(`/api/auth/tokens/${e}`,{method:`DELETE`})}var x=660*60*1e3,S=new Map;function C(e,t,n){return`${e}/${t}/${n}`}async function w(e,t,n={}){let r=C(e,t,n.commit??n.version??`live`),i=S.get(r);if(!n.force&&i&&!i.inflight&&Date.now()-i.fetchedAt<x)return i.token;if(!n.force&&i?.inflight)return i.inflight;let o=a(e,t,n.commit).then(e=>(S.set(r,{token:e,fetchedAt:Date.now()}),e)).catch(e=>{throw S.get(r)?.inflight===o&&S.delete(r),e});return S.set(r,{token:i?.token??``,fetchedAt:i?.fetchedAt??0,inflight:o}),o}function T(){S.clear()}var E=e({auth:()=>L}),D=n(null),O=n(null),k=n(!1),A=n(!1),j=n(!0),M=n(``),N=n(!1),P=n(``),F=n(``),I=n(``),L={get user(){return t(D)},get personRef(){return t(O)},get authEnabled(){return t(k)},get needsSetup(){return t(A)},get loading(){return t(j)},get workspaceName(){return t(M)},get managedAuth(){return t(N)},get cloudUrl(){return t(P)},get managedAuthLoginUrl(){return t(F)},get managedAuthLogoutUrl(){return t(I)},async checkAuth(){r(j,!0);try{let e=await o();if(r(N,!!e.managedAuth,!0),r(P,e.cloudUrl||``,!0),r(F,e.managedAuthLoginUrl||``,!0),r(I,e.managedAuthLogoutUrl||``,!0),r(k,e.authEnabled||t(N),!0),r(A,!t(N)&&e.userCount===0,!0),e.workspaceName&&r(M,e.workspaceName,!0),t(A))return r(j,!1),{authenticated:!1,authEnabled:!1,needsSetup:!0};if(!t(k))return r(j,!1),{authenticated:!0,authEnabled:!1,needsSetup:!1};try{let{user:e,personRef:t}=await s();return r(D,e,!0),r(O,t,!0),r(j,!1),{authenticated:!0,authEnabled:!0,needsSetup:!1}}catch{return r(D,null),r(O,null),r(j,!1),{authenticated:!1,authEnabled:!0,needsSetup:!1}}}catch{return r(j,!1),{authenticated:!0,authEnabled:!1,needsSetup:!1}}},async login(e,t){let{user:n}=await c(e,t);return r(D,n,!0),r(k,!0),n},async setup(e,t,n){let{user:i}=await u(e,t,n);return r(D,i,!0),r(k,!0),r(A,!1),i},async logout(){let e=await l();return r(D,null),r(O,null),T(),e.logoutUrl||t(I)||null}};export{f as a,d as c,b as d,_ as f,y as i,p as l,E as n,m as o,h as p,w as r,v as s,L as t,g as u};