use crate::abi;
use crate::params::Params;
use substreams_ethereum::pb::eth::v2::{Call, Log, TransactionTrace};
use substreams_ethereum::Event;
use tycho_substreams::models::{
    ImplementationType, ProtocolComponent,
};

/// Potentially constructs a new ProtocolComponent given a call
///
/// This method is given each individual call within a transaction, the corresponding
/// logs emitted during that call as well as the full transaction trace.
///
/// If this call creates a component in your protocol please construct and return it
/// here. Otherwise, simply return None.
pub fn maybe_create_component(
    params: &Params,
    call: &Call,
    _log: &Log,
    _tx: &TransactionTrace,
) -> Option<ProtocolComponent> {
    if call.address.as_slice() == params.planner {
        if let Some(event) = abi::party_planner::events::PartyStarted::match_and_decode(_log) {
            return Some(ProtocolComponent::new(&format!("0x{}", hex::encode(&event.pool)))
                            .with_tokens(&event.tokens.clone())
                            .with_contracts(&vec![event.pool.clone(), params.mint_impl.clone(), params.swap_impl.clone()])
                            .as_swap_type("liquidityparty_pool", ImplementationType::Vm));
        }
    }
    None
}
