local P=require("\105\110\118\111\107\097\116\105\111\110\046\100\111\116\097\050\046\107\118")local E=require("\112\108\046\099\108\097\115\115")local C=require("\109\111\115\101\115")local u=E()function u._init(F,E,u)local Z=C.extend({},{Name=E},u)C.extend(F,Z)F.__data=Z F.AbilitySpecial=P.AbilitySpecials(u.AbilitySpecial)F.AbilityBehavior=P.Flags(u.AbilityBehavior)F.ItemDeclarations=P.Strings(u.ItemDeclarations,"\124")F.ItemShopTags=P.Strings(u.ItemShopTags,"\059")F.ItemAliases=P.Strings(u.ItemAliases,"\059")F.ShouldBeSuggested=P.Bool(u.ShouldBeSuggested)F.SideShop=P.Bool(u.SideShop)end function u.Serialize(P)return P.__data end function u.Entries(P)return pairs(P:Serialize())end function u.MatchesQuery(E,P)local u=(((C.chain(string.match)):partialRight(P)):unary()):value()return C.isTable(E.ItemShopTags)and C.findIndex(E.ItemShopTags,u)or C.isTable(E.ItemAliases)and C.findIndex(E.ItemAliases,u)end return u
--- ItemKeyValues class.
-- @classmod invokation.dota2.kv.ItemKeyValues
local KV = require("invokation.dota2.kv")
local class = require("pl.class")
local m = require("moses")

local M = class()

--- Constructor.
-- @tparam string name Item name
-- @tparam {[string]=any,...} kv KeyValues data
function M:_init(name, kv)
  local fields = m.extend({}, { Name = name }, kv)

  m.extend(self, fields)

  self.__data = fields
  self.AbilitySpecial = KV.AbilitySpecials(kv.AbilitySpecial)
  self.AbilityBehavior = KV.Flags(kv.AbilityBehavior)
  self.ItemDeclarations = KV.Strings(kv.ItemDeclarations, "|")
  self.ItemShopTags = KV.Strings(kv.ItemShopTags, ";")
  self.ItemAliases = KV.Strings(kv.ItemAliases, ";")
  self.ShouldBeSuggested = KV.Bool(kv.ShouldBeSuggested)
  self.SideShop = KV.Bool(kv.SideShop)
end

--- Serializes the KeyValues data
-- @treturn {[string]=any,...} Serialized data
function M:Serialize()
  return self.__data
end

--- Returns an iterator function that iterates over the KeyValues entries.
-- @treturn iter(string,any)
function M:Entries()
  return pairs(self:Serialize())
end

--- Checks if the item matches the given search query.
--
-- It searches for matches in these entries:
--
-- * `ItemShopTags`
-- * `ItemAliases`
--
-- @tparam string query Query string
-- @treturn bool
function M:MatchesQuery(query)
  local matcher = m.chain(string.match):partialRight(query):unary():value()

  return (m.isTable(self.ItemShopTags) and m.findIndex(self.ItemShopTags, matcher))
    or (m.isTable(self.ItemAliases) and m.findIndex(self.ItemAliases, matcher))
end

return M
