use core::f64::consts::{PI, TAU};

#[must_use]
pub(crate) fn wrap_delta(a: f64) -> f64 {
    (a + PI).rem_euclid(TAU) - PI
}

#[must_use]
pub(crate) fn contains(theta: f64, start: f64, sweep: f64, eps: f64) -> bool {
    let delta = if sweep >= 0.0 {
        (theta - start).rem_euclid(TAU)
    } else {
        (start - theta).rem_euclid(TAU)
    };
    delta <= sweep.abs() + eps || delta >= TAU - eps
}

#[must_use]
pub(crate) fn clamp(theta: f64, start: f64, sweep: f64, eps: f64) -> f64 {
    if contains(theta, start, sweep, eps) {
        return theta;
    }
    let end = start + sweep;
    let to_start = wrap_delta(theta - start).abs();
    let to_end = wrap_delta(theta - end).abs();
    if to_start <= to_end { start } else { end }
}
