import { Navigate, useLocation } from 'react-router-dom'

function ProtectedRoute({ children }) {
  const location = useLocation()
  const token = localStorage.getItem('access_token')

  if (!token) {
    const returnTo = encodeURIComponent(`${location.pathname}${location.search}${location.hash}`)
    return <Navigate to={`/login?redirect=${returnTo}`} replace />
  }

  return children
}

export default ProtectedRoute
