const std = @import("std");
const zlua = @import("zlua");
const lua = zlua.Lua;

const luafuncts = @import("Libraries/Test.zig");

pub var LuaRuntime: *zlua.Lua = undefined;

pub fn Init(allocator: std.mem.Allocator) !void {
    LuaRuntime = try lua.init(allocator);
    lua.openLibs(LuaRuntime);

    // Add the mistox library
    CreateLibrary(luafuncts, "Mistox");
}


pub fn CreateMetaTable(MetaTableName: []const u8){
    lua.newMetatable(LuaRuntime, MetaTableName);
}


pub fn AddFnToLib(Function: zlua.CFn, FunctionName: []const u8 ) void {
    const str = lua.pushString(LuaRuntime, FunctionName);
    _ = str;
    lua.pushFunction(LuaRuntime, Function);
    lua.setTable(LuaRuntime, -3);
}

fn CreateLibrary(LibraryFolder: type ,LibraryName: [:0]const u8) void {
    lua.newTable(LuaRuntime);
    LibraryFolder.ImportFns();
    lua.setGlobal(LuaRuntime, LibraryName);
}

pub fn DeInit() void {
    LuaRuntime.deinit();
}

const MetaTable_t = struct {
    Function: *usize,
    Name: []const u8
}