use ratatui::buffer::Buffer;
use crate::lookout::frames::coloured_palette;
#[cfg_attr(windows, allow(dead_code))]
pub(super) fn row_for<'a>(frame: &'a str, name: &str) -> Option<&'a str> {
frame.lines().find(|line| {
let mut tokens = line.trim_start_matches('>').split_whitespace();
tokens.next().is_some_and(|id| id.parse::<u32>().is_ok()) && tokens.next() == Some(name)
})
}
pub(super) fn gallery_ground() -> ratatui::style::Color {
coloured_palette()
.ground()
.bg
.expect("the gallery's coloured palette always paints a ground")
}
pub(super) fn row_is_selected(buffer: &Buffer, y: u16) -> bool {
buffer
.cell((0, y))
.is_some_and(|cell| cell.bg == gallery_ground())
}
#[cfg_attr(windows, allow(dead_code))]
pub(super) fn selected_line<'a>(text: &'a str, buffer: &Buffer) -> Option<&'a str> {
(0..buffer.area.height)
.find(|&y| row_is_selected(buffer, y))
.and_then(|y| text.lines().nth(usize::from(y)))
}
pub(super) fn selected_row_count(buffer: &Buffer) -> usize {
(0..buffer.area.height.saturating_sub(1))
.filter(|&y| row_is_selected(buffer, y))
.count()
}
pub(super) fn dog_row_for<'a>(frame: &'a str, name: &str) -> Option<&'a str> {
frame
.lines()
.find(|line| line.trim_start_matches('>').split_whitespace().next() == Some(name))
}
#[cfg_attr(windows, allow(dead_code))]
pub(super) fn tab_dog_row_for<'a>(frame: &'a str, name: &str) -> Option<&'a str> {
frame.lines().find(|line| {
let mut tokens = line.trim_start_matches('>').split_whitespace();
matches!(tokens.next(), Some("\u{25b8}" | "\u{25be}")) && tokens.next() == Some(name)
})
}
#[cfg_attr(windows, allow(dead_code))]
pub(super) fn marked_row_name_starts_with(text: &str, buffer: &Buffer, prefix: &str) -> bool {
selected_line(text, buffer).is_some_and(|line| {
line.trim_start_matches('>')
.split_whitespace()
.nth(1)
.is_some_and(|name| name.starts_with(prefix))
})
}