--core/ticker.lua, a tiny generic repeating-callback registry (backend).
--feature-agnostic: any module can Register(name, interval, callback) to run callback every
--`interval` seconds, and Unregister(name) to stop it. built on C_Timer.NewTicker; registering an
--existing name replaces the previous ticker. e.g. core/manual_hide.lua uses it to re-hide frames
--that re-show themselves. keep callbacks cheap -- they run on the timer, not a per-frame OnUpdate.

local _ = nil
local addonName, private = ...

private.Core = private.Core or {}

---@class FPSPP_Ticker
local Ticker = {}
private.Core.Ticker = Ticker

--active tickers by name: [name] = C_Timer ticker handle (has :Cancel()).
local tickers = {}

--registers (or replaces) a named repeating callback fired every `interval` seconds.
---@param name string
---@param interval number seconds between calls
---@param callback function
function Ticker:Register(name, interval, callback)
	if tickers[name] then
		tickers[name]:Cancel()
	end

	tickers[name] = C_Timer.NewTicker(interval, callback)
end

--stops and forgets a named ticker (no-op if it was never registered).
---@param name string
function Ticker:Unregister(name)
	local ticker = tickers[name]
	if ticker then
		ticker:Cancel()
		tickers[name] = nil
	end
end

--returns whether a named ticker is currently running.
---@param name string
---@return boolean
function Ticker:IsRegistered(name)
	return tickers[name] ~= nil
end
