// authRedirect.tsx
'use client'

import { redirect, usePathname } from 'next/navigation'

// Type Imports
import { Locale } from 'next-intl'

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

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

const AuthRedirect = ({ lang }: { lang: Locale }) => {
  const pathname = usePathname()

  // ℹ️ Bring me `lang`
  const redirectUrl = `/${lang}/login`
  const login = `/${lang}/login`
  const homePage = getLocalizedUrl(themeConfig.homePageUrl, lang)

  // If the user is already on login page, no need to redirect
  if (pathname === login) {
    return null
  }

  // Redirect to login if the user is not authenticated
  return redirect(pathname === homePage ? login : redirectUrl)
}

export default AuthRedirect
