use std::collections::BTreeSet;
use tramlines_md::markdown::LinkTarget;
use super::states_of;
use crate::{Compiled, Document, Model, RepoPath, grammar::SokfSchema, model::PlanIndex};
pub fn planned_by(doc: &Document, schema: &SokfSchema) -> BTreeSet<RepoPath> {
let mut out = BTreeSet::new();
if schema.plans_paths.is_empty() {
return out;
}
let states = states_of(doc.frontmatter.as_ref(), schema);
let value = |key: &str| {
doc.frontmatter
.as_ref()
.and_then(|f| f.get(key))
.and_then(|v| v.as_str())
.map(str::to_string)
};
let headings = &doc.body.headings;
for plan in &schema.plans_paths {
if !plan.common.only.admits(&states) {
continue;
}
let Some(section) = schema
.core
.sections
.iter()
.find(|s| s.rule_id.as_deref() == Some(plan.section.as_str()))
else {
continue;
};
for (i, h) in headings.iter().enumerate() {
if h.level != section.level || !section.heading.shape.matches(&h.text, value) {
continue;
}
let end = headings[i + 1..]
.iter()
.find(|n| n.level <= section.level)
.map_or(usize::MAX, |n| n.line);
for link in &doc.body.links {
if link.line > h.line
&& link.line < end
&& let LinkTarget::Repo { path, .. } = &link.target
{
out.insert(path.clone());
}
}
}
}
out
}
pub fn capture_plans_of(
model: &Model,
compiled: &Compiled,
only: Option<&BTreeSet<RepoPath>>,
) -> PlanIndex {
let mut plans = PlanIndex::default();
for (k, doc) in model.documents() {
if only.is_some_and(|o| !o.contains(&doc.path)) {
continue;
}
let Some((_, schema)) = compiled.for_doc(model, k, doc) else {
continue;
};
for target in planned_by(doc, schema) {
plans.add(&doc.path, &target);
}
}
plans
}