local k=require("\112\108\046\116\097\098\108\101\120")local V=require("\105\110\118\111\107\097\116\105\111\110\046\099\111\110\115\116\046\099\117\115\116\111\109\095\101\118\101\110\116\115")local E={}k.update(E,V)local function D(k)return function(V,...)local E=EntIndexToHScript(V)return k(E,...)end end function E.Subscribe(k,V)return CustomGameEventManager:RegisterListener(k,D(V))end function E.Unsubscribe(k)return CustomGameEventManager:UnregisterListener(k)end function E.SendAll(k,V)return CustomGameEventManager:Send_ServerToAllClients(k,V or{})end function E.SendPlayer(k,V,E)return CustomGameEventManager:Send_ServerToPlayer(V,k,E or{})end function E.SendTeam(k,V,E)return CustomGameEventManager:Send_ServerToTeam(V,k,E or{})end return E
--- Custom events helpers.
-- @module invokation.dota2.custom_events
local tablex = require("pl.tablex")

local EVENTS = require("invokation.const.custom_events")

local M = {}

tablex.update(M, EVENTS)

local function createHandler(callback)
  return function(playerEntIdx, ...)
    local player = EntIndexToHScript(playerEntIdx)
    return callback(player, ...)
  end
end

--- Register a callback to be called when a particular custom event arrives.
-- @tparam string event
-- @tparam function callback
-- @treturn int Listener id that can be used to unregister later
function M.Subscribe(event, callback)
  return CustomGameEventManager:RegisterListener(event, createHandler(callback))
end

--- Unregister a given listener.
-- @tparam int listenerId
function M.Unsubscribe(listenerId)
  return CustomGameEventManager:UnregisterListener(listenerId)
end

--- Send event to all players.
-- @tparam string event
-- @tparam table payload
function M.SendAll(event, payload)
  return CustomGameEventManager:Send_ServerToAllClients(event, payload or {})
end

--- Send event to player.
-- @tparam string event
-- @tparam CDOTAPlayer player
-- @tparam table payload
function M.SendPlayer(event, player, payload)
  return CustomGameEventManager:Send_ServerToPlayer(player, event, payload or {})
end

--- Send event to team.
-- @tparam string event
-- @tparam int team
-- @tparam table payload
function M.SendTeam(event, team, payload)
  return CustomGameEventManager:Send_ServerToTeam(team, event, payload or {})
end

return M
