/**
 * @file config.js - Initializes and configures i18next for the Rapid Workplace application.
 * Includes automatic browser language detection and bundled translation resources for English and Ukrainian languages, falling back to English.
 * This module is imported once at application startup (in `main.tsx`) so that translations are available before any component renders.
 */

import i18next from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import { initReactI18next } from 'react-i18next';
import en from './en/translation.json';
import uk from './uk/translation.json';

/** ISO language code used when the detected language has no matching translation */
export const fallbackLanguage = 'en';

/**
 * Describes a language supported by the application, including its ISO code, a flag emoji for visual display, and a function that returns the language's display name localised to a given language code.
 */
type SupportedLanguage = {
  /** ISO 639-1 language code (e.g. `'en'`, `'uk'`) */
  code: string;

  /** Flag emoji representing the language's primary locale */
  flagEmoji: string;

  /**
   * Returns the human-readable name of this language, localised to the
   * supplied language code.
   *
   * @param code The active language code used to localise the display name
   * @returns The language display name
   */
  displayName: (code: string) => string;
};

/** All languages the application ships translations for */
export const supportedLanguages: SupportedLanguage[] = [
  {
    code: 'en',
    flagEmoji: '🇺🇸',
    displayName: (code: string) => {
      switch (code) {
        case 'uk':
          return 'Англійська';
        default:
          return 'English';
      }
    },
  },
  {
    code: 'uk',
    flagEmoji: '🇺🇦',
    displayName: (code: string) => {
      switch (code) {
        case 'uk':
          return 'Українська';
        default:
          return 'Ukrainian';
      }
    },
  },
];

// Configure and initialise i18next with language detection and React bindings
i18next
  .use(LanguageDetector) // Auto-detect the user's browser language
  .use(initReactI18next) // Bind i18next to React's context system
  .init({
    // Language support config
    supportedLngs: supportedLanguages.map((language) => language.code),
    fallbackLng: fallbackLanguage,
    nonExplicitSupportedLngs: true, // Allow region variants (e.g. 'en-US') to match base codes
    load: 'languageOnly', // Ignore region subtags when loading resource bundles

    // Bundled translation resources keyed by language code
    resources: {
      en: {
        translation: en,
      },
      uk: {
        translation: uk,
      },
    },

    // React already escapes rendered output, so disable i18next's default escaping
    interpolation: {
      escapeValue: false,
    },

    react: {
      // HTML tags preserved by the <Trans> component (adds 'b' to the default set)
      // Default: ['br', 'strong', 'i', 'p']
      transKeepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p', 'b'],
    },
  });
