import en from './locales/en.json';
import es from './locales/es.json';

// Type for available locales - easily extendable
export type Locale = 'en' | 'es';

// Add new languages here
export const locales: Record<Locale, typeof en> = {
  en,
  es,
};

// Language display names
export const localeNames: Record<Locale, string> = {
  en: 'English',
  es: 'Español',
};

// Language flags (emoji or use icons)
export const localeFlags: Record<Locale, string> = {
  en: '🇺🇸',
  es: '🇪🇸',
};

export const defaultLocale: Locale = 'en';

// Get nested translation value
function getNestedValue(obj: any, path: string): string {
  return path.split('.').reduce((acc, part) => acc && acc[part], obj) || path;
}

// Translation function
export function t(locale: Locale, key: string, params?: Record<string, string | number>): string {
  const translations = locales[locale] || locales[defaultLocale];
  let value = getNestedValue(translations, key);
  
  // Replace parameters
  if (params) {
    Object.entries(params).forEach(([paramKey, paramValue]) => {
      value = value.replace(`{${paramKey}}`, String(paramValue));
    });
  }
  
  return value;
}

// HOW TO ADD NEW LANGUAGES:
// 1. Create a new JSON file in locales/ (e.g., pt.json for Portuguese)
// 2. Add the locale to the Locale type above
// 3. Import and add to the locales object
// 4. Add display name to localeNames
// 5. Add flag to localeFlags
// 
// Example for Portuguese:
// import pt from './locales/pt.json';
// export type Locale = 'en' | 'es' | 'pt';
// export const locales = { en, es, pt };
// export const localeNames = { en: 'English', es: 'Español', pt: 'Português' };
// export const localeFlags = { en: '🇺🇸', es: '🇪🇸', pt: '🇧🇷' };
