use crate::inspect::smp::events::*;
use crate::inspect::smp::*;

pub(super) fn decode_train_effect(
    row: &SmpLoadedPackedEventGroupedEffectRowSummary,
    compact_control: &SmpLoadedPackedEventCompactControlSummary,
    descriptor_metadata: RealGroupedEffectDescriptorMetadata,
    target_scope_ordinal: u8,
) -> Option<RuntimeEffect> {
    if descriptor_metadata.executable_in_runtime
        && descriptor_metadata.descriptor_id == 15
        && row.row_shape == "bool_toggle"
        && row.raw_scalar_value != 0
    {
        let company_target = real_grouped_company_target(target_scope_ordinal);
        let territory_target = compact_control
            .grouped_territory_selectors_0x80f
            .get(row.group_index)
            .copied()
            .filter(|selector| *selector >= 0)
            .map(|selector| RuntimeTerritoryTarget::Ids {
                ids: vec![selector as u32],
            });
        if company_target.is_none() && territory_target.is_none() {
            return None;
        }
        return Some(RuntimeEffect::RetireTrains {
            company_target,
            territory_target,
            locomotive_name: row.locomotive_name.clone(),
        });
    }

    None
}
