use eidos_kernel::schema::Node;

use super::helpers::slugify;

/// Resolve a raw target string to an existing node id (by exact id, kind-suffix slug,
/// title slug, or alias slug). Returns None if nothing matches (no dangling edges).
pub(super) fn resolve_target(target: &str, nodes: &[Node]) -> Option<String> {
    if let Some(n) = nodes.iter().find(|n| n.id == target) {
        return Some(n.id.clone());
    }
    let t = slugify(target);
    nodes
        .iter()
        .find(|n| {
            n.id.rsplit('.').next() == Some(t.as_str())
                || slugify(&n.title) == t
                || n.aliases.iter().any(|a| slugify(a) == t)
        })
        .map(|n| n.id.clone())
}
