//! Utility functions for syntaxis-tui
//!
//! This module provides utilities leveraging the tools_tui_shared library
//! for common operations like text formatting, list navigation, and terminal utilities.

// Re-export commonly used utilities from tools_tui_shared
#[allow(unused_imports)]
pub use tools_tui_shared::utils::list::{navigate_down, navigate_up};
#[allow(unused_imports)]
pub use tools_tui_shared::utils::text::{center_text, normalize_spaces, truncate_with_ellipsis};

/// Format a project name for display with safe truncation
///
/// Truncates project names to fit in the display while adding ellipsis if needed.
/// Uses the tools_tui_shared truncate utility.
///
/// # Example
///
/// ```ignore
/// let name = format_project_name("My Very Long Project Name", 20);
/// // Result: "My Very Long Project…" (20 chars max)
/// ```
#[allow(dead_code)]
pub fn format_project_name(name: &str, max_width: usize) -> String {
    truncate_with_ellipsis(name, max_width)
}

/// Format a project description for display
///
/// Normalizes spaces and truncates to fit.
///
/// # Example
///
/// ```ignore
/// let desc = format_project_description("Multi    line   description", 30);
/// ```
#[allow(dead_code)]
pub fn format_project_description(desc: &str, max_width: usize) -> String {
    let normalized = normalize_spaces(desc);
    truncate_with_ellipsis(&normalized, max_width)
}

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

    #[test]
    fn test_format_project_name() {
        let name = "Very Long Project Name";
        let formatted = format_project_name(name, 15);
        // Ellipsis is 3 bytes in UTF-8, so result may be up to 15 + 2 bytes
        assert!(formatted.len() <= 17);
        assert!(formatted.ends_with("…") || formatted.len() < 15);
    }

    #[test]
    fn test_format_project_description() {
        let desc = "Multi   space   description";
        let formatted = format_project_description(desc, 20);
        // Ellipsis is 3 bytes in UTF-8, so result may be up to 20 + 2 bytes
        assert!(formatted.len() <= 22);
        // Should have normalized spaces (but may be truncated)
        assert!(!formatted.contains("   "));
    }

    #[test]
    fn test_truncate_with_ellipsis() {
        let text = "Hello World";
        let truncated = truncate_with_ellipsis(text, 8);
        // Note: ellipsis is 3 bytes in UTF-8, so max_len of 8 results in ~10 bytes total
        // (max_len-1 characters + 3-byte ellipsis)
        assert!(!truncated.contains("World")); // Should be truncated
        assert!(truncated.ends_with("…")); // Should have ellipsis
    }

    #[test]
    fn test_normalize_spaces() {
        let text = "Multi    space    text";
        let normalized = normalize_spaces(text);
        assert!(!normalized.contains("    "));
    }

    #[test]
    fn test_center_text() {
        let text = "Hi";
        let centered = center_text(text, 10);
        assert_eq!(centered.len(), 10);
    }

    #[test]
    fn test_navigate_down() {
        let new_index = navigate_down(0, 5);
        assert_eq!(new_index, 1);

        let new_index = navigate_down(4, 5); // Wrap around
        assert_eq!(new_index, 0);
    }

    #[test]
    fn test_navigate_up() {
        let new_index = navigate_up(1, 5);
        assert_eq!(new_index, 0);

        let new_index = navigate_up(0, 5); // Wrap around
        assert_eq!(new_index, 4);
    }
}
