import React from 'react';
import {Navigate, useLocation} from 'react-router-dom';
import {useAppSelector} from 'store';
import {selectIsAuthenticated} from 'store/slices/Authentication.slice';

export function ProtectedRoute({component}: {component: React.JSX.Element}): React.JSX.Element {
  const isAuthenticated = useAppSelector(selectIsAuthenticated);
  const location = useLocation();

  if (!isAuthenticated) {
    return <Navigate to="/login" state={{from: location}} replace />;
  }

  return component;
}
