-- Copyright 2025 David Vazgenovich Shakaryan local state = {} local mt = {} mt.__index = mt function state.new() return setmetatable({ menus = {}, depth = 0, favourites = {}, playing_id = nil, }, mt) end function mt:menu() return self.menus[self.depth] end function mt:push_menu(t) local menu = { options = {}, cursor = 1, view_top = 1, } for k, v in pairs(t) do menu[k] = v end self.depth = self.depth + 1 self.menus[self.depth] = menu end -- returns index if found function mt:favourited(id) for i, v in ipairs(self.favourites) do if v == id then return i end end end function mt:add_favourite(id) self.favourites[#self.favourites+1] = id end function mt:remove_favourite_at(i) table.remove(self.favourites, i) end -- inserts the given id into the favourites array before the next favourited -- menu option, starting from the next cursor position, or the end if no such -- option is found. this is meant for in-place favouriting from the favourites -- menu. function mt:insert_favourite_before_next_in_menu(id) local menu = self:menu() for i = menu.cursor+1, #menu.options do local ind = self:favourited(menu.options[i].id) if ind then table.insert(self.favourites, ind, id) return end end self:add_favourite(id) end return state