use super::{AnalysisOptions, StyleAnalyzer};
use crate::{
analysis::styles::{
resolved_style::ResolvedStyle,
validation::{StyleConflict, StyleValidationIssue},
},
parser::{Section, Style},
};
use alloc::{collections::BTreeMap, vec::Vec};
impl<'a> StyleAnalyzer<'a> {
#[must_use]
pub fn extract_styles(&self) -> Option<&[Style<'a>]> {
for section in self.script.sections() {
if let Section::Styles(styles) = section {
return Some(styles);
}
}
None
}
pub(super) fn detect_style_conflicts_from_section(&mut self, styles: &[Style<'a>]) {
let mut name_counts: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
for style in styles {
name_counts.entry(style.name).or_default().push(style.name);
}
for (_name, instances) in name_counts {
if instances.len() > 1 {
self.conflicts
.push(StyleConflict::duplicate_name(instances));
}
}
}
pub(super) fn validate_style_properties(
&self,
style: &ResolvedStyle<'a>,
) -> Vec<StyleValidationIssue> {
let mut issues = Vec::new();
if style.font_size() <= 0.0 {
issues.push(StyleValidationIssue::error(
"font_size",
"Font size must be positive",
));
}
if self
.config
.options
.contains(AnalysisOptions::STRICT_VALIDATION)
&& style.font_size() > 200.0
{
issues.push(StyleValidationIssue::warning(
"font_size",
"Very large font size may cause performance issues",
));
}
issues
}
pub(super) fn analyze_style_performance(
&self,
style: &ResolvedStyle<'a>,
) -> Vec<StyleValidationIssue> {
let mut issues = Vec::new();
let thresholds = &self.config.performance_thresholds;
if style.font_size() > thresholds.large_font_threshold {
issues.push(StyleValidationIssue::info_with_suggestion(
"font_size",
"Large font size detected",
"Consider reducing font size for better performance",
));
}
if style.has_performance_issues() {
issues.push(StyleValidationIssue::warning(
"complexity",
"Style has high rendering complexity",
));
}
issues
}
}