use crate::inspect::smp::catalog::conditions::*;
use crate::inspect::smp::catalog::{
    known_special_condition_definition_for_label_id, real_grouped_effect_descriptor_metadata,
};

pub(in crate::inspect::smp) fn real_ordinary_condition_metadata(
    raw_condition_id: i32,
) -> Option<RealOrdinaryConditionMetadata> {
    REAL_ORDINARY_CONDITION_METADATA
        .iter()
        .copied()
        .find(|metadata| metadata.raw_condition_id == raw_condition_id)
        .or_else(|| {
            known_special_condition_definition_for_label_id(raw_condition_id as u32).map(
                |definition| {
                    let kind = if let Some(world_toggle) =
                        real_grouped_effect_descriptor_metadata(110 + definition.slot_index as u32)
                            .filter(|metadata| metadata.parameter_family == "world_flag_toggle")
                    {
                        RealOrdinaryConditionKind::WorldState(RealWorldConditionKind::WorldFlag {
                            key: world_toggle.runtime_key.unwrap_or(world_toggle.label),
                        })
                    } else {
                        RealOrdinaryConditionKind::WorldState(
                            RealWorldConditionKind::SpecialCondition {
                                label: definition.label,
                            },
                        )
                    };
                    RealOrdinaryConditionMetadata {
                        raw_condition_id,
                        label: definition.label,
                        kind,
                    }
                },
            )
        })
}
