local P=require("\105\110\118\111\107\097\116\105\111\110\046\100\111\116\097\050\046\107\118\046\073\116\101\109\075\101\121\086\097\108\117\101\115")local E=require("\112\108\046\099\108\097\115\115")local C=require("\109\111\115\101\115")local u=require("\105\110\118\111\107\097\116\105\111\110\046\099\111\110\115\116\046\105\116\101\109\115")local F=E()local Z="\094\105\116\101\109\095"local function r(E,C)if E:match(Z)and type(C)=="\116\097\098\108\101"then return E,P(E,C)end return nil,nil end function F._init(P)P.__data=C.map(u.KEY_VALUES,C.rearg(r,{2,1}))end function F.Entries(P)return pairs(P.__data)end function F.Search(E,P)local C={}for E,u in E:Entries()do if u:MatchesQuery(P)then C[u.Name]=u:Serialize()end end return C end return F
--- Collection of `ItemKeyValues`.
-- @classmod invokation.dota2.kv.ItemsKeyValues
local ItemKeyValues = require("invokation.dota2.kv.ItemKeyValues")
local class = require("pl.class")
local m = require("moses")

local ITEMS = require("invokation.const.items")

local M = class()

local ITEM_KEY_PATT = "^item_"

local function selectItemEntry(name, kv)
  if name:match(ITEM_KEY_PATT) and type(kv) == "table" then
    return name, ItemKeyValues(name, kv)
  end

  return nil, nil
end

--- Constructor.
function M:_init()
  self.__data = m.map(ITEMS.KEY_VALUES, m.rearg(selectItemEntry, { 2, 1 }))
end

--- Returns an iterator function that iterates over the @{ItemKeyValues} entries.
-- @treturn iter(string,ItemKeyValues)
function M:Entries()
  return pairs(self.__data)
end

--- Searches for item KeyValues matching the given query.
-- @tparam string query Query string
-- @treturn {[string]=ItemKeyValues,...} A table of found entries
function M:Search(query)
  local items = {}

  for _, kv in self:Entries() do
    if kv:MatchesQuery(query) then
      items[kv.Name] = kv:Serialize()
    end
  end

  return items
end

return M
