//! Runtime helpers for executing async code in synchronous Rhai context

use std::sync::OnceLock;

/// Get or create a tokio runtime for blocking async calls
pub fn get_runtime() -> &'static tokio::runtime::Runtime {
    static RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
    RUNTIME.get_or_init(|| {
        tokio::runtime::Runtime::new().expect("Failed to create Tokio runtime")
    })
}

/// Helper to create Rhai errors from string messages
pub fn to_rhai_err(msg: impl AsRef<str>) -> Box<rhai::EvalAltResult> {
    Box::new(rhai::EvalAltResult::ErrorSystem(
        msg.as_ref().to_string(),
        Box::new(std::io::Error::new(
            std::io::ErrorKind::Other,
            msg.as_ref(),
        )),
    ))
}
