//! Splitter pattern: derives multiple logical messages from a single inbound `Exchange`.
//!
//! Implements the Enterprise Integration Pattern (EIP) "Splitter". A splitter breaks a
//! composite or aggregate message (e.g. a delimited string, a JSON array, a batch) into
//! multiple individual messages. In this minimal implementation the provided closure
//! returns a `Vec<Message>` representing the split parts.
//!
//! # Current Behavior & Limitation
//! The splitter invokes the user-supplied closure and, if the returned vector is non-empty,
//! stores ONLY the first resulting message in `exchange.out_msg`. The rest are discarded.
//! This is intentionally minimal for now. Future versions may:
//! * Emit all parts downstream via a `Vec<Message>` property.
//! * Produce cloned `Exchange`s for each part.
//! * Integrate with a downstream `RecipientList` or `Aggregator` automatically.
//!
//! # Use Cases
//! * Taking a CSV line and extracting the first column for downstream processing.
//! * Extracting primary record from a batch for quick validation.
//! * Demonstration / scaffolding before implementing full fan-out semantics.
//!
//! # Example (tokenizing text payload)
//! ```rust
//! use allora_core::{patterns::splitter::Splitter, route::Route, Exchange, Message};
//! let splitter = Splitter::new(|exchange: &Exchange| {
//!     exchange.in_msg.body_text()
//!         .map(|t| t.split_whitespace().map(Message::from_text).collect())
//!         .unwrap_or_else(Vec::new)
//! });
//! let route = Route::new().add(splitter).build();
//! let mut exchange = Exchange::new(Message::from_text("one two three"));
//! let rt = tokio::runtime::Runtime::new().unwrap();
//! rt.block_on(async { route.run(&mut exchange).await.unwrap(); });
//! assert_eq!(exchange.out_msg.unwrap().body_text(), Some("one"));
//! ```
//!
//! # Edge Cases
//! * Empty returned vector => `out_msg` remains unchanged (None if unset).
//! * Mixed payload types allowed; only first message used when non-empty.
//! * Closure should be pure / side-effect free aside from constructing messages.
//!
//! # Testing Strategies
//! * Verify first-element extraction from multiple tokens.
//! * Ensure no `out_msg` when closure returns empty slice.
//! * Provide heterogeneous messages and confirm only first selected.

use crate::{error::Result, message::Message, processor::Processor, Exchange};
use std::fmt::{Debug, Formatter, Result as FmtResult};

pub struct Splitter<F>
where
    F: Fn(&Exchange) -> Vec<Message> + Send + Sync + 'static,
{
    func: F,
}

impl<F> Debug for Splitter<F>
where
    F: Fn(&Exchange) -> Vec<Message> + Send + Sync + 'static,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str("Splitter{func=*closure*}")
    }
}

impl<F> Splitter<F>
where
    F: Fn(&Exchange) -> Vec<Message> + Send + Sync + 'static,
{
    pub fn new(func: F) -> Self {
        Self { func }
    }
}

#[async_trait::async_trait]
impl<F> Processor for Splitter<F>
where
    F: Fn(&Exchange) -> Vec<Message> + Send + Sync + 'static,
{
    async fn process(&self, exchange: &mut Exchange) -> Result<()> {
        let messages = (self.func)(exchange);
        if let Some(first) = messages.get(0) {
            exchange.out_msg = Some(first.clone());
        }
        Ok(())
    }
}
