--[[
	Creates a throttle function that drops any repeat calls within a cooldown period and instead returns the result of the last call
]]

function FunctionUtils.throttle(fn, secondsCooldown)
	assert(FunctionUtils.isCallable(fn))
	assert(type(secondsCooldown) == "number")
	assert(secondsCooldown > 0)

	local cached = false
	local lastResult = nil

	return function(...)
		if not cached then
			cached = true
			lastResult = fn(...)
			delay(
				secondsCooldown,
				function()
					cached = false
				end
			)
		end
		return lastResult
	end
end

function FunctionUtils.isCallable(thing)
	return type(thing) == "function" or
		(type(thing) == "table" and getmetatable(thing) and getmetatable(thing).__call ~= nil)
end

return FunctionUtils
