import { useEffect, useState } from "react";
import { userService } from "@/services/user.service";

export function checkIsLogged(redirectURL = "") {
	const [isLogged, setIsLogged] = useState(false);

	useEffect(() => {
		async function checkAuth() {
               
			const loggedIn = (!userService.userValue) ? (false):(true);
			setIsLogged(loggedIn);

			if (loggedIn) {
				window.location.href = redirectURL;
			}
		}

		checkAuth();
	}, [redirectURL]);

	return isLogged;
}

export function checkIsNotLogged(redirectURL = "") {
	const [isLogged, setIsLogged] = useState(false);

	useEffect(() => {
		async function checkAuth() {
               
			const loggedIn = (!userService.userValue) ? (false):(true);
			setIsLogged(loggedIn);

			if (!loggedIn) {
				window.location.href = redirectURL;
			}
		}

		checkAuth();
	}, [redirectURL]);

	return isLogged;
}
