// =============================================
// TRANSLATIONS — Multi-Language Loader
// =============================================
// Each language lives in its own file under ./translations/<code>.js
// Only the selected language + English fallback are loaded at runtime.
//
// To add a new language:
//   1. Create translations/<code>.js exporting a default object with all keys
//   2. Add the code + display name to availableLanguages below
//
// Dynamic placeholders use {placeholder} syntax:
//   t('page.indicator', { current: 1, total: 10 }) → "1 / 10"

export const availableLanguages = {
    en: 'English',
    es: 'Español',
    fr: 'Français',
    de: 'Deutsch',
    it: 'Italiano',
    pt: 'Português (BR)',
    nl: 'Nederlands',
    sv: 'Svenska',
    da: 'Dansk',
    no: 'Norsk',
    fi: 'Suomi',
    ru: 'Русский',
    pl: 'Polski',
    tr: 'Türkçe',
    ar: 'العربية',
    hi: 'हिन्दी',
    zh: '中文 (简体)',
    'zh-TW': '中文 (繁體)',
    ja: '日本語',
    ko: '한국어',
    // More European
    // More Slavic
    // More Turkic/Central Asian
    // More Caucasian
    // More Middle Eastern
    // More South Asian
    // Southeast Asian
    // Pacific/Oceanian
    // More African
    // Berber Languages
    // Native American
    // Creole/Pidgin
    // Constructed/Classical
};

// RTL languages
export const rtlLanguages = ["ar"];

// Cache for loaded translations
const cache = {};

/**
 * Load a language translation file.
 * Returns the translation object (falls back to English on error).
 */
export async function loadTranslation(lang) {
    if (cache[lang]) return cache[lang];
    try {
        const module = await import(`./${lang}.js`);
        cache[lang] = module.default;
        return cache[lang];
    } catch (e) {
        console.warn(`Translation file for "${lang}" not found, falling back to English.`);
        if (lang !== 'en') return loadTranslation('en');
        return {};
    }
}

/**
 * Load English (fallback) + the target language.
 * Returns { strings, fallback } ready for the t() function.
 */
export async function initTranslations(lang) {
    const fallback = await loadTranslation('en');
    const strings = lang === 'en' ? fallback : await loadTranslation(lang);
    return { strings, fallback };
}
