use web_sys::{Element, HtmlElement};
const PAGE_SCHEME_VAR: &str = "--psp-page-color-scheme";
const UNSET_COLOR: &str = "rgba(0, 0, 0, 0)";
pub fn read_custom_property(elem: &Element, name: &str) -> Option<String> {
let value = web_sys::window()?
.get_computed_style(elem)
.ok()??
.get_property_value(name)
.ok()?;
let value = value.trim();
(!value.is_empty() && value != UNSET_COLOR).then(|| value.to_owned())
}
pub fn read_intl_string(elem: &Element, slug: &str) -> Option<String> {
let raw = read_custom_property(elem, &format!("--psp-label--{slug}--content"))?;
let unquoted = raw
.strip_prefix('"')
.and_then(|x| x.strip_suffix('"'))
.or_else(|| raw.strip_prefix('\'').and_then(|x| x.strip_suffix('\'')))
.unwrap_or(&raw);
Some(unquoted.replace("\\\"", "\""))
}
pub fn stamp_page_color_scheme(host: &HtmlElement) {
let style = host.style();
let Some(parent) = host.parent_element() else {
let _ = style.remove_property(PAGE_SCHEME_VAR);
return;
};
let scheme = web_sys::window()
.and_then(|window| window.get_computed_style(&parent).ok().flatten())
.and_then(|computed| computed.get_property_value("color-scheme").ok())
.unwrap_or_default();
let scheme = match scheme.trim() {
"" | "normal" => "light dark",
scheme => scheme,
};
let _ = style.set_property(PAGE_SCHEME_VAR, scheme);
}