use super::Slot;
use crate::i18n::Text;
use crate::render::Render;
use crate::value::Value;
pub const MAX_RATING: i64 = 10;
pub const DEFAULT_RATING: i64 = 5;
pub(crate) fn stars(max: Option<i64>) -> i64 {
max.unwrap_or(DEFAULT_RATING)
.clamp(1, MAX_RATING)
}
pub(crate) fn render_rating(out: &mut String, r: &Render, s: &Slot) {
let max = stars(s.m.max());
let current = match s.value {
Value::Int(n) => *n,
_ => 0,
};
let required = s.m.is_required();
let star = if required {
r#"<span class="z-required" aria-hidden="true">*</span>"#
} else {
""
};
out.push_str(&format!(
r#"<fieldset class="z-rating" aria-describedby="{}-error"><legend>{}{star}</legend><div class="z-rating-list">"#,
s.id,
r.label(s.m)
));
let required = if required { " required" } else { "" };
let disabled = if s.m.readonly { " disabled" } else { "" };
for n in 1..=max {
let id = if n == 1 {
s.id.to_string()
} else {
format!("{}-{n}", s.id)
};
let checked = if n == current { " checked" } else { "" };
let name = r.text(
&Text::key("zeyn.ui.rating.value")
.arg("n", n.to_string())
.arg("max", max.to_string()),
);
out.push_str(&format!(
r#"<label class="z-rating-star" for="{id}"><input type="radio" class="z-rating-input" id="{id}" name="{}" value="{n}" data-testid="{}.field.{}.{n}"{checked}{required}{disabled}><span class="z-rating-mark" aria-hidden="true">★</span><span class="z-sr-only">{name}</span></label>"#,
s.m.name,
r.scope,
s.m.name
));
}
out.push_str("</div></fieldset>");
}