import { Navigate, useLocation } from 'react-router-dom'
import { useAppSelector } from '../hooks'
import { appPublicRoutes } from '.'

export function PrivateRoute({ children }: { children: JSX.Element }) {
  const location = useLocation()
  const isLoggedIn = useAppSelector((state) => state.auth?.loggedIn)
  if (!isLoggedIn) {
    return (
      <Navigate
        to={{
          pathname: appPublicRoutes.landingPage,
          search: `?callbackPath=${btoa(location.pathname)}`
        }}
        replace
      />
    )
  }

  return children
}
