//! Standalone URL localization utilities
//!
//! Converts between default-language paths and localized equivalents using the
//! same route table that drives the server routing — no hardcoded mappings.

/// Localize `path` for `target_lang`.
///
/// - If `target_lang` is the default language the path is reverse-mapped back
///   to its canonical default-language form (stripping any localized path).
/// - Otherwise the path is forward-mapped to the target language equivalent.
/// - Paths that have no mapping entry are returned unchanged.
pub fn localize_url(path: &str, target_lang: &str) -> String {
    if !crate::i18n::discovery::is_supported_language(target_lang) {
        return path.to_string();
    }

    let default_lang = crate::i18n::discovery::get_default_language();
    let (forward, reverse) = crate::routing::config::build_route_mappings();

    if target_lang == default_lang {
        reverse
            .get(path)
            .cloned()
            .unwrap_or_else(|| path.to_string())
    } else {
        forward
            .get(path)
            .cloned()
            .unwrap_or_else(|| path.to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_english_passthrough() {
        assert_eq!(localize_url("/about", "en"), "/about");
        assert_eq!(localize_url("/services", "en"), "/services");
    }

    #[test]
    fn test_unsupported_language_passthrough() {
        assert_eq!(localize_url("/contact", "fr"), "/contact");
    }
}
