'use client'

// Third-party Imports
import { getCookie } from 'cookies-next/client'

// Type Imports
import type { ChildrenType } from '@core/types'

// Component Imports
import { StaticKey } from '../@core/types/static-key.enum'
import { useRouter } from 'next/navigation'
import FallbackSpinner from '@/@core/components/spinner'

export default function AuthGuard({ children }: ChildrenType) {
  const accessToken = getCookie(StaticKey.ACCESS_TOKEN)
  const user = getCookie(StaticKey.CURRENT_USER)
  const router = useRouter()

  if (!accessToken || !user) {
    router.replace(`/login`)
    return <FallbackSpinner />
  }

  return <>{children}</>
}
