mod env;
#[cfg(any(feature = "json", feature = "yaml", feature = "toml"))]
mod lua_table;
mod path;
mod time;

#[cfg(feature = "exec")]
mod exec;
#[cfg(feature = "fs")]
mod fs;
#[cfg(feature = "hash")]
mod hash;
#[cfg(feature = "http")]
mod http;
#[cfg(feature = "json")]
mod json;
#[cfg(feature = "toml")]
mod toml_helper;
#[cfg(feature = "yaml")]
mod yaml;

use mlua::prelude::*;

/// Register all enabled helper modules into `package.loaded`.
pub fn register(lua: &Lua) -> LuaResult<()> {
    let loaded: LuaTable = lua
        .globals()
        .get::<LuaTable>("package")?
        .get::<LuaTable>("loaded")?;

    // Always available (no extra deps)
    loaded.set("senl.env", env::create(lua)?)?;
    loaded.set("senl.path", path::create(lua)?)?;
    loaded.set("senl.time", time::create(lua)?)?;

    // Feature-gated
    #[cfg(feature = "fs")]
    loaded.set("senl.fs", fs::create(lua)?)?;
    #[cfg(feature = "json")]
    loaded.set("senl.json", json::create(lua)?)?;
    #[cfg(feature = "exec")]
    loaded.set("senl.exec", exec::create(lua)?)?;
    #[cfg(feature = "yaml")]
    loaded.set("senl.yaml", yaml::create(lua)?)?;
    #[cfg(feature = "toml")]
    loaded.set("senl.toml", toml_helper::create(lua)?)?;
    #[cfg(feature = "hash")]
    loaded.set("senl.hash", hash::create(lua)?)?;
    #[cfg(feature = "http")]
    loaded.set("senl.http", http::create(lua)?)?;

    Ok(())
}
