use crossfoot_core::xml::Node;
use crossfoot_core::{ns, Context, Dts, Fact, Instance, Period, QName, Unit};
use crossfoot_dts::schema_util::{attribute_info, type_attributes, value_key};
pub fn element_key(dts: &Dts, n: Node<'_>, out: &mut String) -> Option<()> {
out.push('<');
out.push_str(&n.name().clark());
let decl = dts.schemas.element(n.name());
let ty = decl.and_then(|d| d.type_name.clone());
let mut attrs: Vec<(String, String)> = Vec::new();
for a in n.attributes() {
let an = &a.name;
if &*an.ns == ns::XSI
&& matches!(&*an.local, "schemaLocation" | "noNamespaceSchemaLocation")
{
continue;
}
let aq = QName::new(&an.ns, &an.local);
let at = ty
.as_ref()
.and_then(|t| attribute_info(dts, t, &aq))
.and_then(|i| i.type_name)
.or_else(|| {
if an.ns.is_empty() {
None
} else {
dts.schemas.attribute(&aq).and_then(|g| g.type_name.clone())
}
});
let v = match at.and_then(|t| dts.schemas.validate_simple(&t, &a.value, Some(n)).ok()) {
Some(val) => value_key(&val)?,
None => format!("s:{}", a.value.trim()),
};
attrs.push((aq.clark(), v));
}
if let Some(t) = ty.as_ref() {
for (aq, info) in type_attributes(dts, t) {
let Some(dv) = info.default.as_ref().or(info.fixed.as_ref()) else {
continue;
};
let k = aq.clark();
if attrs.iter().any(|(x, _)| *x == k) {
continue;
}
let v = match info
.type_name
.as_ref()
.and_then(|at| dts.schemas.validate_simple(at, dv, Some(n)).ok())
{
Some(val) => value_key(&val)?,
None => format!("s:{}", dv.trim()),
};
attrs.push((k, v));
}
}
attrs.sort();
for (k, v) in attrs {
out.push(' ');
out.push_str(&k);
out.push('=');
out.push_str(&v);
}
out.push('>');
let kids: Vec<Node<'_>> = n.elements().collect();
if kids.is_empty() {
let mut text = n.string_value();
if text.is_empty() {
if let Some(dv) = decl.and_then(|d| d.default.as_ref().or(d.fixed.as_ref())) {
text = dv.to_string();
}
}
let v = match ty
.as_ref()
.and_then(|t| dts.schemas.validate_simple(t, &text, Some(n)).ok())
{
Some(val) => value_key(&val)?,
None => format!("s:{}", text.trim()),
};
out.push_str(&v);
} else {
for k in kids {
element_key(dts, k, out)?;
}
}
out.push_str("</>");
Some(())
}
fn period_key(p: &Period) -> String {
match p {
Period::Instant(i) => format!("i{}", i.canonical()),
Period::Duration { start, end } => format!("d{}/{}", start.canonical(), end.canonical()),
Period::Forever => "f".into(),
}
}
pub fn context_key(dts: &Dts, inst: &Instance, c: &Context) -> String {
let mut k = String::new();
k.push_str(c.entity_scheme.trim());
k.push('|');
k.push_str(&c.entity_id.split_whitespace().collect::<Vec<_>>().join(" "));
k.push('|');
k.push_str(&period_key(&c.period));
let mut nan = false;
for (tag, part) in [("|S", c.segment), ("|C", c.scenario)] {
k.push_str(tag);
if let Some(f) = part {
if (f.doc as usize) < inst.docs.len() {
let n = inst.node(f);
for ch in n.elements() {
if element_key(dts, ch, &mut k).is_none() {
nan = true;
}
}
}
}
}
if nan {
k.push_str("|#nan#");
k.push_str(&c.id);
}
k
}
pub fn unit_key(u: &Unit) -> String {
u.key()
}
pub fn fact_value_key(dts: &Dts, inst: &Instance, f: &Fact) -> Option<String> {
let v = f.value.as_deref()?;
let c = dts.concept(&f.concept)?;
if let Some(t) = &c.type_name {
let node = f
.node
.filter(|fr| (fr.doc as usize) < inst.docs.len())
.map(|fr| inst.node(fr));
if let Ok(val) = dts.schemas.validate_simple(t, v, node) {
return value_key(&val);
}
}
Some(format!("s:{}", v.trim()))
}