import { Navigate } from 'react-router-dom';
import { useAuth } from '../hooks/useAuth';

export function PublicRoute({ element }: { element: React.ReactNode }) {
  const { isAuthenticated } = useAuth();

  if (isAuthenticated) {
    return <Navigate to="/" />;
  }

  return <>{element}</>;
}

export function PrivateRoute({
  element,
  additionalGuards = [],
}: {
  element: React.ReactNode;
  additionalGuards?: Array<boolean | (() => boolean)>;
}) {
  const { isAuthenticated } = useAuth();
  const isGuardsPassed = additionalGuards.every((item) =>
    typeof item === 'boolean' ? item : item(),
  );

  if (!isGuardsPassed) {
    return <span>Forbidden</span>;
  }

  if (isAuthenticated) {
    return <>{element}</>;
  }

  return <Navigate to="/login" />;
}
