import { Navigate, Outlet, useLocation } from 'react-router-dom'
import { useIsAuthenticated } from '@features/auth'
import { ROUTES } from '@shared/config/routes'
import { Spinner } from '@shared/ui'

export function GuestRoute() {
  const { isAuthenticated, isLoading } = useIsAuthenticated()
  const location = useLocation()
  const from = (location.state as { from?: { pathname: string } })?.from?.pathname ?? ROUTES.WALLET

  if (isLoading) return <Spinner overlay size="lg" label="Загрузка" />
  if (isAuthenticated) return <Navigate to={from} replace />
  return <Outlet />
}
