//#region src/auth/index.ts
var e = {
	accessTokenKey: "auth_access_token",
	refreshTokenKey: "auth_refresh_token",
	apiBasePath: "/api/v1/auth"
};
function t(t = {}) {
	let n = t.accessTokenKey ?? e.accessTokenKey, r = t.refreshTokenKey ?? e.refreshTokenKey;
	return {
		getAccessToken: () => localStorage.getItem(n),
		getRefreshToken: () => localStorage.getItem(r),
		setTokens: (e, t) => {
			localStorage.setItem(n, e), t && localStorage.setItem(r, t);
		},
		clearTokens: () => {
			localStorage.removeItem(n), localStorage.removeItem(r);
		}
	};
}
function n(e = {}) {
	let t = e.accessToken ?? null, n = e.refreshToken ?? null;
	return {
		getAccessToken: () => t,
		getRefreshToken: () => n,
		setTokens: (e, r) => {
			t = e, r && (n = r);
		},
		clearTokens: () => {
			t = null, n = null;
		}
	};
}
function r(n = {}) {
	let r = {
		...e,
		...n
	}, i = n.tokenStorage ?? t({
		accessTokenKey: r.accessTokenKey,
		refreshTokenKey: r.refreshTokenKey
	});
	function a() {
		return i.getAccessToken();
	}
	function o() {
		return i.getRefreshToken();
	}
	function s(e, t) {
		i.setTokens(e, t);
	}
	function c() {
		i.clearTokens();
	}
	function l(e) {
		try {
			let t = e.split(".");
			if (t.length !== 3) return null;
			let n = t[1];
			return n ? JSON.parse(atob(n)) : null;
		} catch {
			return null;
		}
	}
	function u(e) {
		let t = l(e);
		if (!t) return !0;
		let n = Math.floor(Date.now() / 1e3);
		return t.exp < n;
	}
	function d() {
		let e = a();
		return e ? !u(e) : !1;
	}
	async function f() {
		let e = o();
		if (!e) return null;
		try {
			let t = await fetch(`${r.apiBasePath}/refresh`, {
				method: "POST",
				headers: { "Content-Type": "application/json" },
				body: JSON.stringify({ refresh_token: e })
			});
			if (!t.ok) return c(), null;
			let n = await t.json();
			return s(n.access_token, n.refresh_token), n.access_token;
		} catch {
			return c(), null;
		}
	}
	async function p() {
		let e = a();
		if (e) try {
			await fetch(`${r.apiBasePath}/logout`, {
				method: "POST",
				headers: { Authorization: `Bearer ${e}` }
			});
		} catch {}
		c();
	}
	async function m(e, t = {}) {
		let n = a();
		n && u(n) && (n = await f());
		let r = new Headers(t.headers);
		return n && r.set("Authorization", `Bearer ${n}`), fetch(e, {
			...t,
			headers: r
		});
	}
	function h(e) {
		return typeof e == "object" && e && "access_token" in e ? e : null;
	}
	function g(e) {
		return typeof e == "object" && e && "challenge_id" in e ? e : null;
	}
	return {
		getAccessToken: a,
		getRefreshToken: o,
		setTokens: s,
		clearTokens: c,
		parseToken: l,
		isTokenExpired: u,
		isAuthenticated: d,
		refreshAccessToken: f,
		logout: p,
		fetchWithAuth: m,
		extractTokenFromResponse: h,
		extractMfaChallengeFromResponse: g,
		config: r,
		tokenStorage: i
	};
}
var i = null;
function a(e) {
	return i ??= r(e), i;
}
var o = () => a().getAccessToken(), s = () => a().getRefreshToken(), c = (e, t) => {
	a().setTokens(e, t);
}, l = () => {
	a().clearTokens();
}, u = (e) => a().parseToken(e), d = (e) => a().isTokenExpired(e), f = () => a().isAuthenticated(), p = () => a().refreshAccessToken(), m = () => a().logout(), h = (e, t) => a().fetchWithAuth(e, t);
//#endregion
export { l as clearTokens, r as createAuthManager, t as createLocalStorageTokenStorage, n as createMemoryTokenStorage, h as fetchWithAuth, o as getAccessToken, a as getDefaultAuthManager, s as getRefreshToken, f as isAuthenticated, d as isTokenExpired, m as logout, u as parseToken, p as refreshAccessToken, c as setTokens };

//# sourceMappingURL=index.js.map