import { getRequestConfig } from 'next-intl/server'
import { cookies } from 'next/headers'

export const supportedLocales = ['en', 'fr'] as const
export type SupportedLocale = (typeof supportedLocales)[number]

export const defaultLocale: SupportedLocale = 'en'

export default getRequestConfig(async () => {
  const store = await cookies()
  const cookieLocale = store.get('locale')?.value

  // Validate the locale from cookie
  const locale = supportedLocales.includes(cookieLocale as SupportedLocale)
    ? (cookieLocale as SupportedLocale)
    : defaultLocale

  return {
    locale,
    messages: (await import(`../../messages/${locale}.json`)).default,
  }
})
