eazygit 0.5.1

A fast TUI for Git with staging, conflicts, rebase, and palette-first UX
Documentation
//! Example integration of TEA runtime with the event loop.
//!
//! This module demonstrates how the TEA (The Elm Architecture) runtime
//! can be integrated into the main application event loop.
//!
//! # Usage Pattern
//!
//! ```no_run
//! use eazygit::core::{Runtime, Msg};
//! use eazygit::services::GitService;
//! use std::sync::Arc;
//!
//! async fn example_integration() {
//!     // Create runtime
//!     let git_service = Arc::new(GitService::new());
//!     let mut runtime = Runtime::new(git_service, ".".to_string());
//!     
//!     // Create state
//!     let mut state = eazygit::app::AppState::new(".".to_string());
//!     
//!     // Dispatch messages
//!     runtime.dispatch(&mut state, Msg::Tick).await;
//!     runtime.dispatch(&mut state, Msg::ToggleHelp).await;
//!     
//!     // Recursive dispatch (handles effect results)
//!     runtime.dispatch_recursive(&mut state, Msg::RequestStage {
//!         path: "file.rs".to_string()
//!     }).await;
//! }
//! ```

#![cfg(FALSE)]  // TODO: Remove when Runtime module is implemented

use crate::core::{Runtime, Msg, Effect};
use crate::app::AppState;
use crate::services::GitService;
use std::sync::Arc;

/// Helper for integrating TEA pattern into existing code.
/// 
/// This provides a bridge between the new TEA architecture and
/// the existing Command pattern during the migration.
// pub struct TeaBridge {
    runtime: Runtime,
}

// impl TeaBridge {
    /// Create a new TEA bridge.
    pub fn new(git_service: Arc<GitService>, repo_path: String) -> Self {
        Self {
            runtime: Runtime::new(git_service, repo_path),
        }
    }
    
    /// Dispatch a message synchronously (blocking).
    /// 
    /// Use this during the migration when you need to integrate
    /// TEA updates with existing synchronous code.
    pub fn dispatch_sync(&mut self, state: &mut AppState, msg: Msg) {
        // For now, just call update directly without executing effects
        // Full effect execution happens in async context
        let _effect = crate::core::update(state, msg.clone());
        
        // Log the action
        self.runtime.action_logger_mut().log_action(&msg, false);
    }
    
    /// Convert an existing Action to a Msg (migration helper).
    /// 
    /// This helps migrate code incrementally by translating
    /// old Actions to new Msgs.
    pub fn action_to_msg(action: &crate::app::Action) -> Option<Msg> {
        use crate::app::Action;
        
        match action {
            Action::Tick => Some(Msg::Tick),
            Action::Quit => Some(Msg::RequestQuit),
            Action::ToggleHelp => Some(Msg::ToggleHelp),
            Action::ShowThemePicker => Some(Msg::ShowThemePicker),
            Action::HideThemePicker => Some(Msg::HideThemePicker),
            Action::SetFeedback(msg) => Some(Msg::SetFeedback { 
                message: msg.clone() 
            }),
            Action::FocusNext => Some(Msg::FocusNext),
            Action::FocusPrev => Some(Msg::FocusPrev),
            Action::StartCommit => Some(Msg::StartCommitInput),
            Action::CancelCommit => Some(Msg::CancelCommitInput),
            Action::SetCommitInput(text) => Some(Msg::UpdateCommitInput { 
                text: text.clone() 
            }),
            // Add more mappings as needed during migration
            _ => None,
        }
    }
    
    /// Get action statistics for debugging.
    pub fn action_stats(&self) -> std::collections::HashMap<String, usize> {
        self.runtime.action_logger().action_stats()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_action_to_msg_mapping() {
        use crate::app::Action;
        
        assert!(matches!(
            TeaBridge::action_to_msg(&Action::Tick),
            Some(Msg::Tick)
        ));
        
        assert!(matches!(
            TeaBridge::action_to_msg(&Action::ToggleHelp),
            Some(Msg::ToggleHelp)
        ));
        
        assert!(matches!(
            TeaBridge::action_to_msg(&Action::Quit),
            Some(Msg::RequestQuit)
        ));
    }
}