use serde_json::{Map, Value};

#[must_use]
pub fn add_value_to_map(
    mut map: Map<String, Value>,
    (key, value): (String, Value),
) -> Map<String, Value> {
    use Value::Array;
    use serde_json::map::Entry::{Occupied, Vacant};
    match map.entry(key) {
        Vacant(vacant) => {
            vacant.insert(value);
        }
        Occupied(mut old_entry) => {
            let mut new_array = if let Array(v) = value { v } else { vec![value] };
            match old_entry.get_mut() {
                Array(old_array) => old_array.append(&mut new_array),
                old_scalar => {
                    new_array.insert(0, old_scalar.take());
                    *old_scalar = Array(new_array);
                }
            }
        }
    }
    map
}

macro_rules! static_filename {
    ($filename:expr) => {
        include_str!(concat!(env!("OUT_DIR"), "/", $filename, ".filename.txt"))
    };
}

pub(crate) use static_filename;
