local db
do
    db = {}
    setmetatable(db, {
        __index = function(self, key)
            return database.read(key)
        end,
        __newindex = function(self, key, value)
            return database.write(key, value)
        end
    })
end
client.set_event_callback("shutdown", function()
    db.configs = database
end)
function menu:init()
    public.config = {
        list = private.group:listbox("list", {}),
        name = private.group:textbox(" "),
        load = private.group:button("load"),
        save = private.group:button("save"),
        delete = private.group:button("delete"),
        export = private.group:button("export"),
        import = private.group:button("import")
    }
    do
        local database = db.configs or {}
        local this = {}
        local instance = pui.setup(public, true)
        function this:update_ui()
            local tbl = {}
            for key, value in pairs(database) do
                tbl[#tbl + 1] = key
            end
            ui.update(public.config.list.ref, tbl)
        end; this:update_ui()
        function this:load()
            local name = ui.get(public.config.name.ref)
            local success, parsed = pcall(json.parse, database[name])
            if not success then
                print("failed to parse config")
                return
            end
            print(database[name])
            instance:load(parsed)
            print("config was successfully loaded.")
            this:update_ui()
        end
        function this:save()
            local name = ui.get(public.config.name.ref)
            database[name] = json.stringify(instance:save())
            this:update_ui()
        end;
        function this:delete()
            local name = ui.get(public.config.name.ref)
            if database[name] ~= nil then
                database[name] = nil
            end
            ui.set(public.config.name.ref, '')
            this:update_ui()
        end
        function this:export()
            local name = ui.get(public.config.name.ref)
            if database[name] ~= nil then
                clipboard.set(database[name])
            end
            print("config was successfully copied.")
            this:update_ui()
        end
        function this:import()
            local cdata = clipboard.get() or nil
            local name = ui.get(public.config.name.ref)
            if cdata ~= nil then
                database[name] = cdata
            end
            instance:load(cdata)
            this:update_ui()
        end
        function this:shutdown()
            db.configs = database
        end; client.set_event_callback("shutdown", function() this:shutdown() end)
        do
            public.config.save:set_callback(function() this:save() end)
            public.config.delete:set_callback(function() this:delete() end)
            public.config.load:set_callback(function() this:load() end)
            public.config.save:set_callback(function() this:save() end)
            public.config.export:set_callback(function() this:export() end)
            public.config.import:set_callback(function() this:import() end)
        end
    end
    setmetatable(public, self)
    self.__index = self;
    return public
end
local vars = menu:init()