"use client";

import { useEffect } from "react";
import { useUser } from "./";
import { useRouter } from "next/navigation";
import Loading from "../app/loading";

interface LoginGuardProps {
  children: React.ReactNode;
}
const LoginGuard = ({ children }: LoginGuardProps): React.ReactNode => {
  const { isAuthenticated, isLoading } = useUser();
  const router = useRouter();

  useEffect(() => {
    if (!isLoading && isAuthenticated) {
      router.push("/");
    }
  }, [isLoading, isAuthenticated, router]);

  if (isLoading || isAuthenticated) {
    return <Loading />;
  }
  return children;
};
export default LoginGuard;
