// i18n/request.ts
import { getRequestConfig } from 'next-intl/server';
import { headers } from 'next/headers';

const LOCALES = ["uz", "ru", "en"] as const;
const DEFAULT_LOCALE = "uz";

type Locale = typeof LOCALES[number];

export default getRequestConfig(async () => {
  const headersList = await headers();
  
  // ✅ Middleware allaqachon locale'ni set qilgan
  const locale = headersList.get('x-locale') || DEFAULT_LOCALE;
  
  // Validate
  const validLocale = LOCALES.includes(locale as Locale) 
    ? locale 
    : DEFAULT_LOCALE;

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