//! Specificity arithmetic and comparison.

use std::cmp::Ordering;

use super::ast::Specificity;

/// Component-wise sum of two specificities.
pub fn sum_spec(a: Specificity, b: Specificity) -> Specificity {
    [a[0] + b[0], a[1] + b[1], a[2] + b[2]]
}

/// Lexicographically greater of two specificities (id, then class, then tag).
fn max_spec(a: Specificity, b: Specificity) -> Specificity {
    if compare_specificity(a, b) != Ordering::Less {
        a
    } else {
        b
    }
}

/// Reduce a list of specificities with `max_spec`, starting from `[0, 0, 0]`.
pub fn max_all_spec(specs: &[Specificity]) -> Specificity {
    specs.iter().fold([0, 0, 0], |acc, s| max_spec(acc, *s))
}

/// Compare two specificities.
///
/// Lexicographic on `[id, class, tag]`: id outranks class, class outranks tag.
pub fn compare_specificity(a: Specificity, b: Specificity) -> Ordering {
    a.cmp(&b)
}
