use crate::inspect::smp::bundle::{
    classify_container_profile, classify_header_variant_probe, classify_secondary_variant_probe,
    parse_shared_header, probe_early_content_layout,
};
use crate::inspect::smp::common::find_first_ascii_run;
use crate::inspect::smp::structures::*;
use std::fs;
use std::path::Path;

pub fn inspect_save_placed_structure_dynamic_side_buffer_file(
    path: &Path,
) -> Result<Option<SmpSavePlacedStructureDynamicSideBufferProbe>, Box<dyn std::error::Error>> {
    let bytes = fs::read(path)?;
    let file_extension_hint = path
        .extension()
        .and_then(|extension| extension.to_str())
        .map(|extension| extension.to_ascii_lowercase());
    let shared_header = parse_shared_header(&bytes);
    let header_variant_probe = shared_header.as_ref().map(classify_header_variant_probe);
    let first_ascii_run = find_first_ascii_run(&bytes);
    let early_content_probe = first_ascii_run
        .as_ref()
        .and_then(|ascii_run| probe_early_content_layout(&bytes, ascii_run));
    let secondary_variant_probe = early_content_probe
        .as_ref()
        .and_then(classify_secondary_variant_probe);
    let container_profile = classify_container_profile(
        file_extension_hint.as_deref(),
        header_variant_probe.as_ref(),
        secondary_variant_probe.as_ref(),
    );
    Ok(parse_save_placed_structure_dynamic_side_buffer_probe(
        &bytes,
        file_extension_hint.as_deref(),
        container_profile.as_ref(),
    ))
}
