//! Internationalization utilities

#[allow(unused_imports)]
use std::collections::HashMap;

/// I18n manager
pub struct I18nManager {
    /// Language messages
    messages: HashMap<String, HashMap<String, String>>,
    /// Default language
    default_language: String,
}

impl I18nManager {
    /// Create a new I18n manager
    pub fn new(default_language: String) -> Self {
        Self {
            messages: HashMap::new(),
            default_language,
        }
    }

    /// Add messages for a language
    pub fn add_messages(&mut self, language: String, messages: HashMap<String, String>) {
        self.messages.insert(language, messages);
    }

    /// Get message for key and language
    pub fn get_message(&self, language: &str, key: &str) -> Option<&String> {
        self.messages
            .get(language)?
            .get(key)
            .or_else(|| self.messages.get(&self.default_language)?.get(key))
    }

    /// Format message with parameters
    pub fn format_message(
        &self,
        language: &str,
        key: &str,
        params: &HashMap<String, String>,
    ) -> Option<String> {
        let message = self.get_message(language, key)?;
        let mut result = message.clone();

        for (param_key, param_value) in params {
            result = result.replace(&format!("{{{}}}", param_key), param_value);
        }

        Some(result)
    }
}

impl Default for I18nManager {
    fn default() -> Self {
        Self::new("en".to_string())
    }
}
