#[cfg(test)]
mod test;

use tracing::instrument;

use super::{v2x, Report, Warning};
use crate::{
    json::{self, FieldsAsExt as _},
    required_field,
    warning::{self, DeescalateError as _, GatherWarnings as _},
};

#[instrument(skip_all)]
pub(crate) fn lint(tariff_elem: &json::Element<'_>, mut warnings: warning::Set<Warning>) -> Report {
    let Some(fields) = tariff_elem.as_object_fields() else {
        return Report { warnings };
    };

    let fields = fields.as_raw_map();

    if let Some(elem) = required_field!(tariff_elem, fields, "currency", warnings) {
        let _drop: Option<()> = v2x::currency::lint(elem)
            .gather_warnings_into(&mut warnings)
            .deescalate_error_into(&mut warnings);
    }

    {
        if let Some(elem) = required_field!(tariff_elem, fields, "elements", warnings) {
            let _drop: Option<()> = v2x::elements::lint(elem)
                .gather_warnings_into(&mut warnings)
                .deescalate_error_into(&mut warnings);
        }
    }

    Report { warnings }
}
