local M=script.Parent.Parent local D=game:GetService("\067\104\097\116")local K=M:WaitForChild("\067\108\105\101\110\116\067\104\097\116\077\111\100\117\108\101\115")local W=require(K:WaitForChild("\067\104\097\116\067\111\110\115\116\097\110\116\115"))local f=W.StandardPriority if f==nil then f=10 end local P={}P.__index=P local p={}do local M={}M.__index=M function M.RebuildProcessCommandsPriorities(M)M.RegisteredPriorites={}for D,K in pairs(M.FunctionMap)do local W=true for M,D in pairs(K)do W=false break end if not W then table.insert(M.RegisteredPriorites,D)end end table.sort(M.RegisteredPriorites,function(M,D)return M>D end)end function M.HasFunction(D,M)if D.RegisteredFunctions[M]==nil then return false end return true end function M.RemoveFunction(D,M)local K=D.RegisteredFunctions[M]D.RegisteredFunctions[M]=nil D.FunctionMap[K][M]=nil D:RebuildProcessCommandsPriorities()end function M.AddFunction(W,M,D,K)if K==nil then K=f end if W.RegisteredFunctions[M]then error(M.." \105\115 \097\108\114\101\097\100\121 \105\110 \117\115\101\033")end W.RegisteredFunctions[M]=K if W.FunctionMap[K]==nil then W.FunctionMap[K]={}end W.FunctionMap[K][M]=D W:RebuildProcessCommandsPriorities()end function M.GetIterator(M)local D=1 local K=nil local W=nil return function()while true do if D>#M.RegisteredPriorites then return end local f=M.RegisteredPriorites[D]K,W=next(M.FunctionMap[f],K)if K==nil then D=D+1 else return K,W,f end end end end function p.new()local D=setmetatable({},M)D.RegisteredFunctions={}D.RegisteredPriorites={}D.FunctionMap={}return D end end function P.NewSortedFunctionContainer(M)return p.new()end return P
--!nonstrict
--	// FileName: Util.lua
--	// Written by: TheGamer101
--	// Description: Utility code used by the server side chat implementation.

local chatFolder = script.Parent.Parent

local Chat = game:GetService("Chat")
local ReplicatedModules = chatFolder:WaitForChild("ClientChatModules")
local ChatConstants = require(ReplicatedModules:WaitForChild("ChatConstants"))

local DEFAULT_PRIORITY = ChatConstants.StandardPriority
if DEFAULT_PRIORITY == nil then
	DEFAULT_PRIORITY = 10
end

local Util = {}
Util.__index = Util

local SortedFunctionContainer = {}
do
	-- This sorted function container is used to handle the logic around storing filter functions and
	-- command processors by priority.

	local methods = {}
	methods.__index = methods

	function methods:RebuildProcessCommandsPriorities()
		self.RegisteredPriorites = {}
		for priority, functions in pairs(self.FunctionMap) do
			local functionsEmpty = true
			for funcId, funciton in pairs(functions) do
				functionsEmpty = false
				break
			end
			if not functionsEmpty then
				table.insert(self.RegisteredPriorites, priority)
			end
		end
		table.sort(self.RegisteredPriorites, function(a, b)
			return a > b
		end)
	end

	function methods:HasFunction(funcId)
		if self.RegisteredFunctions[funcId] == nil then
			return false
		end
		return true
	end

	function methods:RemoveFunction(funcId)
		local functionPriority = self.RegisteredFunctions[funcId]
		self.RegisteredFunctions[funcId] = nil
		self.FunctionMap[functionPriority][funcId] = nil
		self:RebuildProcessCommandsPriorities()
	end

	function methods:AddFunction(funcId, func, priority)
		if priority == nil then
			priority = DEFAULT_PRIORITY
		end

		if self.RegisteredFunctions[funcId] then
			error(funcId .. " is already in use!")
		end

		self.RegisteredFunctions[funcId] = priority

		if self.FunctionMap[priority] == nil then
			self.FunctionMap[priority] = {}
		end

		self.FunctionMap[priority][funcId] = func
		self:RebuildProcessCommandsPriorities()
	end

	function methods:GetIterator()
		local priorityIndex = 1
		local funcId = nil
		local func = nil

		return function()
			while true do
				if priorityIndex > #self.RegisteredPriorites then
					return
				end
				local priority = self.RegisteredPriorites[priorityIndex]
				funcId, func = next(self.FunctionMap[priority], funcId)
				if funcId == nil then
					priorityIndex = priorityIndex + 1
				else
					return funcId, func, priority
				end
			end
		end
	end

	function SortedFunctionContainer.new()
		local obj = setmetatable({}, methods)

		obj.RegisteredFunctions = {}
		obj.RegisteredPriorites = {}
		obj.FunctionMap = {}

		return obj
	end
end

function Util:NewSortedFunctionContainer()
	return SortedFunctionContainer.new()
end

return Util
