'use client'

// Type Imports
import type { Locale } from '@configs/i18n'
import type { ChildrenType } from '@core/types'
import { useEffect } from 'react'
import { useAuth } from '../contexts/authContext'

// Component Imports
import { redirect } from 'next/navigation'
import Loading from '../components/layout/shared/Loading'
import { getLocalizedUrl } from '../utils/i18n'

export default function AuthGuard({ children, locale }: ChildrenType & { locale: Locale }) {
  const { isAuthenticated } = useAuth()

  useEffect(() => {
    if (!isAuthenticated) {
      redirect(getLocalizedUrl('/login', locale))
    }
  }, [isAuthenticated])

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