use rdom_core::NodeId;
use super::model::{display_size, option_label, selected_options};
use crate::TuiDom;
use crate::render::paint_pass::ChromeText;
const OPEN_ATTR: &str = "data-rdom-open";
pub fn is_dropdown(dom: &TuiDom, select: NodeId) -> bool {
!dom.node(select).has_attribute("multiple") && display_size(dom, select) <= 1
}
pub fn is_open(dom: &TuiDom, select: NodeId) -> bool {
dom.node(select).has_attribute(OPEN_ATTR)
}
pub fn open(dom: &mut TuiDom, select: NodeId) {
if !is_dropdown(dom, select) {
return;
}
let _ = dom.set_attribute(select, OPEN_ATTR, "");
}
pub fn close(dom: &mut TuiDom, select: NodeId) {
if !is_dropdown(dom, select) {
return;
}
let _ = dom.remove_attribute(select, OPEN_ATTR);
}
pub(crate) fn inline_chrome(dom: &TuiDom, id: NodeId, _width: u16) -> Option<ChromeText> {
if dom.node(id).tag_name() != Some("select") {
return None;
}
if !is_dropdown(dom, id) {
return None;
}
if is_open(dom, id) {
return None;
}
let selected = selected_options(dom, id);
let label = selected
.first()
.map(|&opt| option_label(dom, opt))
.unwrap_or_default();
Some(ChromeText {
text: label,
fg: None,
})
}