use eth_etl_core::{EthLog, EthTokenTransfer};
use eth_etl_mappers::log_to_token_transfer;

/// Extract token transfers from event logs
pub struct TokenTransferExtractor;

impl TokenTransferExtractor {
    /// Extract all token transfers from a list of logs
    pub fn extract(logs: &[EthLog]) -> Vec<EthTokenTransfer> {
        logs.iter().filter_map(log_to_token_transfer).collect()
    }

    /// Extract token transfers from logs, filtering by token addresses
    pub fn extract_filtered(
        logs: &[EthLog],
        token_addresses: Option<&[String]>,
    ) -> Vec<EthTokenTransfer> {
        let transfers = Self::extract(logs);

        if let Some(addresses) = token_addresses {
            let addresses_lower: Vec<String> = addresses.iter().map(|a| a.to_lowercase()).collect();
            transfers
                .into_iter()
                .filter(|t| addresses_lower.contains(&t.token_address.to_lowercase()))
                .collect()
        } else {
            transfers
        }
    }
}

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

    #[test]
    fn test_extract_empty_logs() {
        let logs: Vec<EthLog> = vec![];
        let transfers = TokenTransferExtractor::extract(&logs);
        assert!(transfers.is_empty());
    }
}
