//! Template variable replacement utilities
//!
//! This module provides template variable replacement functionality
//! for processing template files from the remote asset system.

use std::collections::HashMap;

/// Apply template variable replacement to content
///
/// Replaces {{variable}} placeholders with their corresponding values
pub fn apply_template_vars(content: &str, vars: &HashMap<String, String>) -> String {
    let mut result = content.to_string();

    for (key, value) in vars {
        let placeholder = format!("{{{{{}}}}}", key);
        result = result.replace(&placeholder, value);
    }

    result
}

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

    #[test]
    fn test_template_variable_replacement() {
        let template = "Hello {{name}}, version {{version}}!";
        let mut vars = HashMap::new();
        vars.insert("name".to_string(), "Rustelo".to_string());
        vars.insert("version".to_string(), "1.0.0".to_string());

        let result = apply_template_vars(template, &vars);
        assert_eq!(result, "Hello Rustelo, version 1.0.0!");
    }

    #[test]
    fn test_multiple_same_variable() {
        let template = "{{name}} is great! I love {{name}}!";
        let mut vars = HashMap::new();
        vars.insert("name".to_string(), "Rustelo".to_string());

        let result = apply_template_vars(template, &vars);
        assert_eq!(result, "Rustelo is great! I love Rustelo!");
    }

    #[test]
    fn test_missing_variable() {
        let template = "Hello {{name}}, {{unknown}}!";
        let mut vars = HashMap::new();
        vars.insert("name".to_string(), "Rustelo".to_string());

        let result = apply_template_vars(template, &vars);
        assert_eq!(result, "Hello Rustelo, {{unknown}}!");
    }
}
