import { createAuthManager as e, getDefaultAuthManager as t } from "../../auth/index.js";
import { useCallback as n, useEffect as r, useMemo as i, useRef as a, useState as o } from "react";
//#region src/react/hooks/useAuth.ts
function s(e) {
	return e ? {
		id: e.sub,
		username: e.sub,
		email: "",
		roles: e.roles ?? [],
		groups: e.groups ?? [],
		is_active: !0,
		is_verified: !0
	} : null;
}
function c(c) {
	let l = i(() => c === void 0 ? t() : e(c), [c]), [u, d] = o(() => l.isAuthenticated()), [f, p] = o(() => {
		let e = l.getAccessToken();
		return e ? s(l.parseToken(e)) : null;
	}), m = a(null), h = n(() => {
		let e = l.isAuthenticated();
		d(e);
		let t = l.getAccessToken();
		return p(t ? s(l.parseToken(t)) : null), e;
	}, [l]), g = n(() => {
		m.current !== null && (clearTimeout(m.current), m.current = null);
		let e = l.getAccessToken();
		if (!e) return;
		let t = l.parseToken(e);
		if (!t) return;
		let n = Math.floor(Date.now() / 1e3), r = t.exp - n;
		if (r <= 0) return;
		let i = Math.max((r - 30) * 1e3, 0);
		m.current = setTimeout(() => {
			l.refreshAccessToken().then(() => {
				h(), g();
			});
		}, i);
	}, [l, h]);
	return r(() => (h(), g(), () => {
		m.current !== null && (clearTimeout(m.current), m.current = null);
	}), [h, g]), {
		manager: l,
		isAuthenticated: u,
		user: f,
		refresh: n(() => {
			h();
		}, [h]),
		refreshToken: n(async () => {
			let e = await l.refreshAccessToken();
			return h(), g(), e;
		}, [
			l,
			h,
			g
		]),
		logout: n(async () => {
			await l.logout(), h();
		}, [l, h])
	};
}
//#endregion
export { c as useAuth };

//# sourceMappingURL=useAuth.js.map