use super::super::condition_targets::{
    lower_condition_targets_in_condition, lowered_condition_true_company_target,
    lowered_condition_true_player_target, packed_record_condition_scope_import_blocker,
};
use crate::documents::{RuntimeImportBlocker, RuntimeImportContext};
use crate::event::conditions::RuntimeCondition;
use crate::inspect::smp::events::SmpLoadedPackedEventRecordSummary;

pub(crate) fn lowered_record_decoded_conditions(
    record: &SmpLoadedPackedEventRecordSummary,
    runtime_context: &RuntimeImportContext,
) -> Result<Vec<RuntimeCondition>, RuntimeImportBlocker> {
    if let Some(blocker) = packed_record_condition_scope_import_blocker(record, runtime_context) {
        return Err(blocker);
    }

    let lowered_company_target = lowered_condition_true_company_target(record)?;
    let lowered_player_target = lowered_condition_true_player_target(record)?;
    let ordinary_rows = record
        .standalone_condition_rows
        .iter()
        .filter(|row| row.raw_condition_id >= 0);
    ordinary_rows
        .zip(record.decoded_conditions.iter())
        .map(|(row, condition)| {
            lower_condition_targets_in_condition(
                condition,
                row,
                lowered_company_target.as_ref(),
                lowered_player_target.as_ref(),
                runtime_context,
            )
        })
        .collect()
}
