use std::path::Path;

use super::assembly::{derive_loaded_save_slice_parts, finish_loaded_save_slice};
use super::notes::build_loaded_save_slice_notes;
use crate::inspect::smp::save_load::*;

pub fn load_save_slice_file(path: &Path) -> Result<SmpLoadedSaveSlice, Box<dyn std::error::Error>> {
    let inspection = inspect_smp_file(path)?;
    load_save_slice_from_report(&inspection)
        .map_err(|err| -> Box<dyn std::error::Error> { err.into() })
}

pub fn load_save_slice_from_report(
    report: &SmpInspectionReport,
) -> Result<SmpLoadedSaveSlice, String> {
    let summary = report
        .save_load_summary
        .as_ref()
        .ok_or_else(|| "inspection did not expose a recognizable save-load summary".to_string())?;
    let parts = derive_loaded_save_slice_parts(report);
    let notes = build_loaded_save_slice_notes(report, summary, &parts);
    Ok(finish_loaded_save_slice(summary, parts, notes))
}
