'use client'

// Next Imports

// Type Imports
import type { ChildrenType } from '@core/types'
import { useRouter } from 'next/navigation'
import { useEffect } from 'react'
import { useAuth } from '../contexts/authContext'
import Loading from '../components/layout/shared/Loading'

// Config Imports

// Util Imports

const GuestOnlyRoute = ({ children }: ChildrenType) => {
  const router = useRouter()

  const { isAuthenticated, currentUser } = useAuth()

  useEffect(() => {
    if (!isAuthenticated) return

    if (currentUser?.role === 'admin') {
      router.push('/dashboards/overview')
    } else {
      router.push('/sa/organizations/list')
    }
  }, [isAuthenticated])

  return <>{isAuthenticated ? <Loading /> : children}</>
}

export default GuestOnlyRoute
