import { getRequestConfig } from 'next-intl/server';
import { notFound } from 'next/navigation';

const locales = ['en', 'vi'];

const messageImports = {
    en: () => import('../messages/en.json'),
    vi: () => import('../messages/vi.json')
} as const;

export default getRequestConfig(async ({ requestLocale }) => {
    // requestLocale is a Promise in Next.js 16, need to await it
    const locale = await requestLocale;
    
    console.log('getRequestConfig called with requestLocale:', locale);
    
    // Validate that the incoming `locale` parameter is valid
    if (!locale || !locales.includes(locale as any)) {
        console.log('Invalid locale:', locale);
        notFound();
    }

    const messages = (await messageImports[locale as keyof typeof messageImports]()).default;
    console.log('Messages loaded for', locale);
    return {
        locale: locale as string,
        messages
    };
});
