//! Edge classification (spec §3.2).
//!
//! After the parcel polygon is built and its frontage edge index
//! recorded, this pass labels every edge as `Frontage`, `Side`, or
//! `Back`. Side edges are the two adjacent to the frontage edge in
//! ring order; back edges are everything else.

use super::EdgeKind;

/// Build an edge-kind vector parallel to a polygon's edge ring.
#[must_use]
pub(crate) fn classify_edges(num_edges: usize, frontage_idx: usize) -> Vec<EdgeKind> {
    let mut kinds = vec![EdgeKind::Back; num_edges];
    if num_edges == 0 {
        return kinds;
    }
    kinds[frontage_idx] = EdgeKind::Frontage;
    let prev = (frontage_idx + num_edges - 1) % num_edges;
    let next = (frontage_idx + 1) % num_edges;
    if prev != frontage_idx {
        kinds[prev] = EdgeKind::Side;
    }
    if next != frontage_idx && next != prev {
        kinds[next] = EdgeKind::Side;
    }
    kinds
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn quadrilateral_classification() {
        let k = classify_edges(4, 0);
        assert_eq!(
            k,
            vec![
                EdgeKind::Frontage,
                EdgeKind::Side,
                EdgeKind::Back,
                EdgeKind::Side,
            ]
        );
    }

    #[test]
    fn pentagonal_classification() {
        let k = classify_edges(5, 2);
        assert_eq!(
            k,
            vec![
                EdgeKind::Back,
                EdgeKind::Side,
                EdgeKind::Frontage,
                EdgeKind::Side,
                EdgeKind::Back,
            ]
        );
    }
}
