use openehr_am::v2_4::aom2::constraint_model::c_object::CObject;
use super::builder_v2_4::{attr_children, cstring_under};
use super::model::WebTemplateNode;
pub(super) fn build_path(
parent: Option<&WebTemplateNode>,
attr: Option<&str>,
rm_type: &str,
id: &str,
name: Option<&str>,
) -> String {
let (Some(parent), Some(a)) = (parent, attr) else {
return String::new();
};
let path = &parent.aql_path;
if id.is_empty() || rm_type.starts_with("DV_") {
format!("{path}/{a}")
} else if let Some(name) = name {
format!("{path}/{a}[{id},'{name}']")
} else {
format!("{path}/{a}[{id}]")
}
}
pub(super) fn name_constraint(co: &CObject) -> Option<String> {
attr_children(co, "name").find_map(|name| {
let value = cstring_under(name, "value")?;
match value.constraint.as_deref() {
Some([literal]) if !literal.starts_with('/') => Some(literal.clone()),
_ => None,
}
})
}