//! Extraction of discrete products from a `SIMPLE` briefing narrative.
//!
//! The `SIMPLE` briefing is a plain-text document; pulling the labelled
//! METAR/TAF lines out as [`Product`]s lets a Leidos briefing be
//! compared product-by-product against a product-oriented source. The
//! narrative is kept too — extraction is additive, never lossy. One
//! report per physical line is assumed (the synthetic format); folded
//! multi-line TAFs would need joining first.

use aerocontext_core::{Product, ProductKind};

/// Pull METAR/TAF products out of a `SIMPLE` briefing narrative.
pub(crate) fn extract_products(narrative: &str) -> Vec<Product> {
    narrative.lines().filter_map(parse_line).collect()
}

fn parse_line(line: &str) -> Option<Product> {
    let trimmed = line.trim();
    let mut tokens = trimmed.split_whitespace();
    let kind = match tokens.next()? {
        "METAR" | "SPECI" => ProductKind::Metar,
        "TAF" => ProductKind::Taf,
        _ => return None,
    };
    let station = tokens.next().filter(|s| is_station(s))?;
    Some(Product::new(kind, trimmed).with_location(Some(station.to_owned())))
}

fn is_station(token: &str) -> bool {
    (3..=4).contains(&token.len())
        && token
            .chars()
            .all(|c| c.is_ascii_uppercase() || c.is_ascii_digit())
}

#[cfg(test)]
mod tests;
