use techy::core::node::{BuildId, NodeRef};
use techy::latexlike::MathGroupForm;
use crate::core::lowering::{LowerCx, LowerError, Lowerer};
use crate::lang::text::plain_text;
use crate::lang::{Flm, FlmGroupType};
use crate::model::addr::NodeAddr;
use crate::model::aspects::PointId;
use crate::model::element::element_value;
use crate::stdlib::backends::lowering::lower_run;
use crate::stdlib::features::numbering::{Counters, FormatError, FormatOptions, NumberSegment};
use crate::stdlib::features::refs::Anchors;
use super::MathEnvironment;
use super::lines::{EquationLine, equation_lines};
pub fn formula_display(node: &NodeRef<'_, Flm>) -> bool {
matches!(node.group_type(), Some(FlmGroupType::Math(MathGroupForm::Display)))
}
#[derive(Debug, Clone)]
pub struct LineFrame<'t> {
pub line: EquationLine<'t>,
pub anchor: Option<String>,
pub shows: LineShows<'t>,
}
#[derive(Debug, Clone)]
pub enum LineShows<'t> {
Nothing,
Number(Vec<NumberSegment>),
Tag {
starred: bool,
content: Vec<NodeRef<'t, Flm>>,
},
}
pub fn line_frames<'t>(
anchors: Option<&Anchors>,
counters: Option<&Counters>,
addr: NodeAddr,
node: &NodeRef<'t, Flm>,
) -> Result<Vec<LineFrame<'t>>, FormatError> {
let numbered = element_value::<MathEnvironment>(node)
.is_some_and(|value| value.numbered && value.numbered_equations);
let options = FormatOptions::new().without_word();
let mut frames = Vec::new();
for (index, line) in equation_lines(node).into_iter().enumerate() {
let point = PointId { addr, index: index as u32 };
let anchor = anchors.and_then(|anchors| anchors.id(point)).map(String::from);
let shows = match &line.tag {
Some(tag) => LineShows::Tag { starred: tag.star, content: tag.content.clone() },
None if numbered && !line.nonumber => {
let number = match counters.and_then(|counters| counters.format(point, &options)) {
None => Vec::new(),
Some(written) => written?,
};
LineShows::Number(number)
}
None => LineShows::Nothing,
};
frames.push(LineFrame { line, anchor, shows });
}
Ok(frames)
}
pub fn number_text(frame: &LineFrame<'_>) -> Option<String> {
match &frame.shows {
LineShows::Number(segments) => {
Some(segments.iter().map(|segment| plain_text(segment.content())).collect())
}
LineShows::Nothing | LineShows::Tag { .. } => None,
}
}
pub fn stage_tag_content(
cx: &mut LowerCx<'_>,
lw: &mut dyn Lowerer,
frame: &LineFrame<'_>,
) -> Result<Vec<BuildId>, LowerError> {
let mut content = Vec::new();
match &frame.shows {
LineShows::Nothing => {}
LineShows::Number(segments) => {
for segment in segments {
content.extend(lw.splice(cx, segment.content())?);
}
}
LineShows::Tag { starred, content: nodes } => {
if !starred {
content.push(lw.text(cx, "(")?);
}
content.extend(lower_run(cx, lw, nodes)?);
if !starred {
content.push(lw.text(cx, ")")?);
}
}
}
Ok(content)
}
#[cfg(test)]
mod tests {
use super::*;
fn frame(shows: LineShows<'static>) -> LineFrame<'static> {
LineFrame { line: EquationLine::default(), anchor: None, shows }
}
#[test]
fn a_line_showing_nothing_or_a_tag_has_no_number_text() {
assert_eq!(number_text(&frame(LineShows::Nothing)), None);
let tag = LineShows::Tag { starred: true, content: Vec::new() };
assert_eq!(number_text(&frame(tag)), None);
}
#[test]
fn an_empty_number_is_an_empty_text() {
assert_eq!(number_text(&frame(LineShows::Number(Vec::new()))), Some(String::new()));
}
}