// FILE: src/lib.rs

pub mod parser;
pub mod modules;

pub use crate::parser::interpreter::Interpreter;
pub use crate::parser::types::{Value, FunctionValue};

pub fn value_to_string(val: &Value) -> String {
    match val {
        Value::Int(x) => x.to_string(),
        Value::IntArray(xs) => format!("{:?}", xs),
        Value::Int2DArray(rows) => format!("{:?}", rows),
        Value::Float(f) => f.to_string(),
        Value::FloatArray(ff) => format!("{:?}", ff),
        Value::Float2DArray(rows) => format!("{:?}", rows),
        Value::StrArray(ss) => format!("{:?}", ss),
        Value::KeyedArray(map) => {
            let mut s = String::from("{ ");
            let mut first = true;
            for (k, v) in map.iter() {
                if !first {
                    s.push_str(", ");
                }
                first = false;
                s.push_str(k);
                s.push_str(": ");
                s.push_str(&value_to_string(v));
            }
            s.push_str(" }");
            s
        }
        Value::Function(_) => "[Function]".to_string(),
        Value::Bool(b) => b.to_string(),
        Value::BoolArray(bb) => format!("{:?}", bb),
        Value::Placeholder => "_".to_string(),
        Value::SingleString(s) => s.clone(),
        Value::Long(l) => l.to_string(),
        Value::Stream(sh) => format!("<Stream id={}, label={}>", sh.stream_id, sh.label),
        Value::InkIterator(_) => "[InkIterator]".to_string(),
        Value::InkTransform(_) => "[InkTransform]".to_string(),
        Value::Tensor(_) => "[Tensor]".to_string(),
        Value::MixedArray(items) => {
            let inner = items.iter().map(|v| value_to_string(v)).collect::<Vec<_>>().join(", ");
            format!("[{}]", inner)
        }
        Value::Ref(arc_mutex) => value_to_string(&arc_mutex.lock().unwrap()),
    }
}
