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

pub(in crate::inspect::smp) fn parse_event_runtime_record_summaries(
    records_payload: &[u8],
    records_payload_offset: usize,
    live_entry_ids: &[u32],
) -> Vec<SmpLoadedPackedEventRecordSummary> {
    try_parse_synthetic_event_runtime_record_summaries(
        records_payload,
        records_payload_offset,
        live_entry_ids,
    )
    .or_else(|| {
        try_parse_real_event_runtime_record_summaries(
            records_payload,
            records_payload_offset,
            live_entry_ids,
        )
    })
    .unwrap_or_else(|| {
        build_unsupported_event_runtime_record_summaries(
            live_entry_ids,
            "0x4e9a payload did not match the current packed-event record decode harness",
        )
    })
}

pub(in crate::inspect::smp) fn build_unsupported_event_runtime_record_summaries(
    live_entry_ids: &[u32],
    note: &str,
) -> Vec<SmpLoadedPackedEventRecordSummary> {
    live_entry_ids
        .iter()
        .copied()
        .enumerate()
        .map(
            |(record_index, live_entry_id)| SmpLoadedPackedEventRecordSummary {
                record_index,
                live_entry_id,
                payload_offset: None,
                payload_len: None,
                decode_status: "unsupported_framing".to_string(),
                payload_family: "unsupported_framing".to_string(),
                trigger_kind: None,
                active: None,
                marks_collection_dirty: None,
                one_shot: None,
                compact_control: None,
                text_bands: Vec::new(),
                standalone_condition_row_count: 0,
                standalone_condition_rows: Vec::new(),
                negative_sentinel_scope: None,
                grouped_effect_row_counts: vec![0, 0, 0, 0],
                grouped_effect_rows: Vec::new(),
                decoded_conditions: Vec::new(),
                decoded_actions: Vec::new(),
                executable_import_ready: false,
                notes: vec![note.to_string()],
            },
        )
        .collect()
}
