use argonauts_core::workflow::{StepKind, StepRef};
use crate::state::workflow::Workflows;
use crate::status::folder::words;
pub fn line(workflows: &Workflows) -> Option<String> {
let standard = workflows.current_standard()?;
let running = workflows.running();
let (_, progress) = running.last()?;
let at = place(workflows, &progress.step, progress.kind, progress.round);
Some(format!("{} · {at}", words(&standard)))
}
pub fn place(workflows: &Workflows, step: &StepRef, kind: StepKind, round: u32) -> String {
let crumbs = breadcrumb(workflows, step).join(" › ");
let record = workflows.by_id.get(&step.workflow);
let number = step.path.first().map_or(1, |index| index + 1);
let count = record
.and_then(|record| record.phase_definition(step.phase))
.map(|phase| phase.steps.len());
let name = record
.and_then(|record| record.step_definition(step))
.and_then(|definition| definition.name.clone())
.unwrap_or_else(|| words(&kind));
let mut text = match count {
Some(count) => format!("{crumbs}, step {number} of {count}: {name}"),
None => format!("{crumbs}, step {number}: {name}"),
};
if round > 1 {
text.push_str(&format!(", round {round}"));
}
text
}
fn breadcrumb(workflows: &Workflows, step: &StepRef) -> Vec<String> {
let mut crumbs = Vec::new();
let mut at = Some(step);
for _ in 0..8 {
let Some(step) = at else { break };
let Some(record) = workflows.by_id.get(&step.workflow) else {
break;
};
crumbs.push(
record
.phase_definition(step.phase)
.map(|phase| phase.name.clone())
.unwrap_or_else(|| format!("phase {}", step.phase + 1)),
);
at = record.parent.as_ref();
if at.is_some() {
crumbs.push(record.workflow.name.clone());
}
}
crumbs.reverse();
crumbs
}