//! Testable Inspector body containment geometry.

use bevy_egui::egui;

pub(super) fn configure_component_scroll_style(style: &mut egui::Style) {
    let scroll = &mut style.spacing.scroll;
    scroll.floating = true;
    scroll.bar_width = 8.0;
    scroll.floating_width = 3.0;
    scroll.floating_allocated_width = 4.0;
    scroll.content_margin.right = 4;
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct InspectorBodyLayout {
    pub body: egui::Rect,
    pub clip: egui::Rect,
}

impl InspectorBodyLayout {
    pub(super) fn contained(available: egui::Rect, parent_clip: egui::Rect) -> Option<Self> {
        let clip = available.intersect(parent_clip);
        (available.is_positive() && clip.is_positive()).then_some(Self {
            body: available,
            clip,
        })
    }
}

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

    #[test]
    fn penpot_component_body_never_paints_above_fixed_header() {
        let header_bottom = 96.0;
        let available =
            egui::Rect::from_min_max(egui::pos2(400.0, header_bottom), egui::pos2(1040.0, 900.0));
        let parent_clip =
            egui::Rect::from_min_max(egui::pos2(400.0, 48.0), egui::pos2(1040.0, 860.0));
        let layout = InspectorBodyLayout::contained(available, parent_clip).unwrap();
        assert_eq!(layout.body.top(), header_bottom);
        assert!(layout.clip.top() >= header_bottom);
        assert_eq!(layout.clip.bottom(), 860.0);
    }

    #[test]
    fn penpot_component_body_rejects_empty_intersection_after_resize() {
        let available = egui::Rect::from_min_max(egui::pos2(0.0, 120.0), egui::pos2(200.0, 200.0));
        let parent_clip = egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(200.0, 100.0));
        assert!(InspectorBodyLayout::contained(available, parent_clip).is_none());
    }

    #[test]
    fn component_scrollbar_keeps_a_stable_content_width_when_overflow_changes() {
        let mut style = egui::Style::default();
        configure_component_scroll_style(&mut style);

        assert!(style.spacing.scroll.floating);
        assert_eq!(style.spacing.scroll.floating_allocated_width, 4.0);
        assert_eq!(style.spacing.scroll.content_margin.right, 4);
    }
}
