//! Utility functions for route generation

use super::types::Route;

/// Get the correct module/directory name from page component name
/// This function now uses the route's module_path configuration when available,
/// falling back to a simple camelCase conversion only when not configured
pub fn get_module_name_from_page_component_with_route(route: &Route) -> String {
    // Use module_path from route configuration if available
    if let Some(module_path) = &route.module_path {
        return module_path.clone();
    }

    // Fallback to deriving from page_component name
    let default_page_component = format!("{}Page", route.component);
    let page_component = route
        .page_component
        .as_ref()
        .unwrap_or(&default_page_component);

    get_module_name_from_page_component(page_component)
}

/// Simple fallback function for cases where we don't have a Route object
/// Uses only camelCase conversion without any hardcoded mappings
pub fn get_module_name_from_page_component(page_component: &str) -> String {
    let base_name = page_component
        .strip_suffix("Page")
        .unwrap_or(page_component);

    // Simply convert to camelCase - no hardcoded mappings
    // The actual module paths should be configured in routes.toml files
    to_camel_case(base_name)
}

/// Convert PascalCase to camelCase (first letter lowercase)
pub fn to_camel_case(s: &str) -> String {
    if s.is_empty() {
        return String::new();
    }

    let mut chars = s.chars();
    match chars.next() {
        None => String::new(),
        Some(first_char) => first_char.to_lowercase().collect::<String>() + chars.as_str(),
    }
}

/// Format patterns for Rust code
pub fn format_patterns_for_rust_code(patterns: &[String]) -> String {
    let quoted_patterns: Vec<String> = patterns.iter().map(|p| format!("\"{}\"", p)).collect();
    format!("&[{}]", quoted_patterns.join(", "))
}
