import { o as __toESM } from "../_runtime.mjs";
import { h as require_react, m as require_jsx_runtime } from "../_libs/react+tanstack__react-query.mjs";
import { n as trpc } from "./trpc-client-CQOIB5UU.mjs";
import { l as useNavigate } from "../_libs/@tanstack/react-router+[...].mjs";
//#region node_modules/.nitro/vite/services/ssr/assets/auth-context-DzjwonUC.js
var import_react = /* @__PURE__ */ __toESM(require_react());
var import_jsx_runtime = require_jsx_runtime();
var AuthContext = (0, import_react.createContext)(void 0);
function AuthProvider({ children }) {
	const [state, setState] = (0, import_react.useState)({
		user: null,
		userDetails: null,
		isAuthenticated: false,
		isLoading: true,
		token: null
	});
	const navigate = useNavigate();
	(0, import_react.useEffect)(() => {
		const storedToken = localStorage.getItem("auth_token");
		const storedUserId = localStorage.getItem("user_id");
		if (storedToken && storedUserId) setState((prev) => ({
			...prev,
			token: storedToken,
			isAuthenticated: true
		}));
		else setState((prev) => ({
			...prev,
			isLoading: false
		}));
	}, []);
	const { data: selfData } = trpc.user.user.getSelfData.useQuery(void 0, {
		enabled: !!state.token && state.isAuthenticated,
		retry: 1
	});
	(0, import_react.useEffect)(() => {
		if (selfData?.data) setState((prev) => ({
			...prev,
			user: selfData.data,
			userDetails: selfData.data,
			isLoading: false
		}));
		else if (selfData === void 0 && state.token) {}
	}, [selfData, state.token]);
	const login = (0, import_react.useCallback)((token, user) => {
		localStorage.setItem("auth_token", token);
		localStorage.setItem("user_id", String(user.id));
		setState({
			user,
			userDetails: user,
			isAuthenticated: true,
			isLoading: false,
			token
		});
	}, []);
	const loginWithToken = (0, import_react.useCallback)((token, user) => {
		login(token, user);
	}, [login]);
	const register = (0, import_react.useCallback)((token, user) => {
		login(token, user);
	}, [login]);
	const logout = (0, import_react.useCallback)(() => {
		localStorage.removeItem("auth_token");
		localStorage.removeItem("user_id");
		setState({
			user: null,
			userDetails: null,
			isAuthenticated: false,
			isLoading: false,
			token: null
		});
		navigate({ to: "/login" });
	}, [navigate]);
	const updateUser = (0, import_react.useCallback)((user) => {
		setState((prev) => ({
			...prev,
			user
		}));
	}, []);
	const updateUserDetails = (0, import_react.useCallback)((details) => {
		setState((prev) => ({
			...prev,
			userDetails: details
		}));
	}, []);
	return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AuthContext.Provider, {
		value: {
			...state,
			login,
			loginWithToken,
			register,
			logout,
			updateUser,
			updateUserDetails
		},
		children
	});
}
function useAuth() {
	const context = (0, import_react.useContext)(AuthContext);
	if (!context) throw new Error("useAuth must be used within AuthProvider");
	return context;
}
//#endregion
export { useAuth as n, AuthProvider as t };
