'use client'

// Next Imports
import { redirect } from 'next/navigation'

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

// Type Imports
import type { ChildrenType } from '@core/types'
import { Locale } from 'next-intl'
import { StaticKey } from '../@core/types/static-key.enum'

// Config Imports
import themeConfig from '@configs/themeConfig'

// Util Imports
import { getLocalizedUrl } from '@/utils/i18n'

const GuestOnlyRoute = ({ children, lang }: ChildrenType & { lang: Locale }) => {
  const accessToken = getCookie(StaticKey.ACCESS_TOKEN)
  const user = getCookie(StaticKey.CURRENT_USER)

  // If the user is logged in, redirect to home page
  if (accessToken || user) {
    redirect(getLocalizedUrl(themeConfig.homePageUrl, lang))
  }

  return <>{children}</>
}

export default GuestOnlyRoute
