import React from 'react';
import { Navigate } from 'react-router-dom';

interface ProtectedRouteProps {
    element: JSX.Element;
    [key: string]: any; // Allow additional props
}

const ProtectedRoute: React.FC<ProtectedRouteProps> = ({ element, ...rest }) => {
    const isAuthenticated = () => {
        const token = localStorage.getItem('token');
        console.log("Is Authenticated: ", token !== null);
        return token !== null;
    };

    return isAuthenticated()
        ? React.cloneElement(element, { ...rest }) // Pass all additional props
        : <Navigate to="/login" />;
};

export default ProtectedRoute;