//! Common button input handling.

use ratatui::layout::Rect;

pub fn hit_test(position: (u16, u16), rect: Rect) -> bool {
    position.0 >= rect.x
        && position.0 < rect.x + rect.width
        && position.1 >= rect.y
        && position.1 < rect.y + rect.height
}

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

    #[test]
    fn test_hit_test_inside() {
        let rect = Rect::new(10, 20, 5, 3);
        assert!(hit_test((10, 20), rect));
        assert!(hit_test((14, 22), rect));
        assert!(hit_test((12, 21), rect));
    }

    #[test]
    fn test_hit_test_outside() {
        let rect = Rect::new(10, 20, 5, 3);
        assert!(!hit_test((9, 20), rect));
        assert!(!hit_test((10, 19), rect));
        assert!(!hit_test((15, 20), rect));
        assert!(!hit_test((10, 23), rect));
    }

    #[test]
    fn test_hit_test_edge() {
        let rect = Rect::new(10, 20, 5, 3);
        assert!(hit_test((14, 22), rect));
        assert!(!hit_test((15, 22), rect));
        assert!(!hit_test((14, 23), rect));
    }
}
