use crate::virt_what::get_virt_what;
use rhai::{Dynamic, Engine, EvalAltResult, Map};

pub fn register_virtwhat_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
    // Register virt_what()
    engine.register_fn("virt_what", || -> Result<Map, Box<EvalAltResult>> {
        let info = get_virt_what();
        let mut map = Map::new();

        map.insert("is_virtualized".into(), Dynamic::from(info.is_virtualized));
        map.insert(
            "virtualization_type".into(),
            info.virtualization_type
                .map(Dynamic::from)
                .unwrap_or(Dynamic::UNIT),
        );
        map.insert(
            "hypervisor".into(),
            info.hypervisor.map(Dynamic::from).unwrap_or(Dynamic::UNIT),
        );
        map.insert(
            "container".into(),
            info.container.map(Dynamic::from).unwrap_or(Dynamic::UNIT),
        );
        map.insert("platform".into(), Dynamic::from(info.platform));

        Ok(map)
    });

    Ok(())
}
