use serde_yaml::Value;

/// Deep merge `source` into `target` following GitLab CI semantics:
/// - Mappings are recursively merged (source keys added if absent in target).
/// - All other types (sequences, scalars): target value wins if present.
///
/// This is used for `include` merging where the included file provides defaults
/// and the main file overrides.
pub fn deep_merge(target: &mut Value, source: &Value) {
    if let (Value::Mapping(target_map), Value::Mapping(source_map)) = (target, source) {
        for (key, source_val) in source_map {
            if let Some(target_val) = target_map.get_mut(key) {
                deep_merge(target_val, source_val);
            } else {
                target_map.insert(key.clone(), source_val.clone());
            }
        }
    }
}
