local d do local D,P=pcall(function()return(UserSettings()):IsUserFeatureEnabled("\085\115\101\114\070\108\097\103\082\101\109\111\118\101\077\101\115\115\097\103\101\070\114\111\109\077\101\115\115\097\103\101\076\111\103")end)d=D and P end local D=false do local d,P=pcall(function()return(UserSettings()):IsUserFeatureEnabled("\085\115\101\114\073\115\067\104\097\116\084\114\097\110\115\108\097\116\105\111\110\069\110\097\098\108\101\100\050")end)D=d and P end local P={}local q=script.Parent.Parent.Parent local B=game:GetService("\067\104\097\116")local y=q:WaitForChild("\067\108\105\101\110\116\067\104\097\116\077\111\100\117\108\101\115")local b=script.Parent local R=require(y:WaitForChild("\067\104\097\116\083\101\116\116\105\110\103\115"))local m=nil if D then m=b:FindFirstChild("\067\104\097\116\084\114\097\110\115\108\097\116\105\111\110\069\110\097\098\108\101\100")if m==nil then local d d=b.ChildAdded:Connect(function(D)if D.Name=="\067\104\097\116\084\114\097\110\115\108\097\116\105\111\110\069\110\097\098\108\101\100"then m=D d:Disconnect()end end)end end local l={}l.__index=l function l.Destroy(d)d.Destroyed=true end function l.SetActive(D,d)if d==D.Active then return end if d==false then D.MessageLogDisplay:Clear()else D.MessageLogDisplay:SetCurrentChannelName(D.Name)for d=1,#D.MessageLog,1 do D.MessageLogDisplay:AddMessage(D.MessageLog[d])end end D.Active=d end function l.UpdateMessageFiltered(q,P)local B=1 local y=q.MessageLog local b=nil while#y>=B do local d=y[B]if d.ID==P.ID then b=d break end B=B+1 end if b then b.Message=P.Message b.IsFiltered=true if D and(m~=nil and m.Value)then b.TranslatedMessage=P.TranslatedMessage b.ShowTranslated=true end if q.Active then if d then if b.Message==""then table.remove(q.MessageLog,B)end end q.MessageLogDisplay:UpdateMessageFiltered(b)end else q:AddMessageToChannelByTimeStamp(P)end end function l.AddMessageToChannel(D,d)table.insert(D.MessageLog,d)if D.Active then D.MessageLogDisplay:AddMessage(d)end if#D.MessageLog>R.MessageHistoryLengthPerChannel then D:RemoveLastMessageFromChannel()end end function l.InternalAddMessageAtTimeStamp(D,d)for P=1,#D.MessageLog,1 do if d.Time<D.MessageLog[P].Time then table.insert(D.MessageLog,P,d)return end end table.insert(D.MessageLog,d)end function l.AddMessagesToChannelByTimeStamp(P,d,D)for D=D,#d,1 do P:InternalAddMessageAtTimeStamp(d[D])end while#P.MessageLog>R.MessageHistoryLengthPerChannel do table.remove(P.MessageLog,1)end if P.Active then P.MessageLogDisplay:Clear()for d=1,#P.MessageLog,1 do P.MessageLogDisplay:AddMessage(P.MessageLog[d])end end end function l.AddMessageToChannelByTimeStamp(D,d)if#D.MessageLog>=1 then if D.MessageLog[1].Time>d.Time then return elseif d.Time>=D.MessageLog[#D.MessageLog].Time then D:AddMessageToChannel(d)return end for P=1,#D.MessageLog,1 do if d.Time<D.MessageLog[P].Time then table.insert(D.MessageLog,P,d)if#D.MessageLog>R.MessageHistoryLengthPerChannel then D:RemoveLastMessageFromChannel()end if D.Active then D.MessageLogDisplay:AddMessageAtIndex(d,P)end return end end else D:AddMessageToChannel(d)end end function l.RemoveLastMessageFromChannel(d)table.remove(d.MessageLog,1)if d.Active then d.MessageLogDisplay:RemoveLastMessage()end end function l.ClearMessageLog(d)d.MessageLog={}if d.Active then d.MessageLogDisplay:Clear()end end function l.RegisterChannelTab(D,d)D.ChannelTab=d end function P.new(d,D)local P=setmetatable({},l)P.Destroyed=false P.Active=false P.MessageLog={}P.MessageLogDisplay=D P.ChannelTab=nil P.Name=d return P end return P
--!nonstrict
--	// FileName: ChatChannel.lua
--	// Written by: Xsitsu
--	// Description: ChatChannel class for handling messages being added and removed from the chat channel.

local UserFlagRemoveMessageFromMessageLog
do
	local success, value = pcall(function()
		return UserSettings():IsUserFeatureEnabled("UserFlagRemoveMessageFromMessageLog")
	end)
	UserFlagRemoveMessageFromMessageLog = success and value
end

local userIsChatTranslationEnabled = false
do
	local success, value = pcall(function()
		return UserSettings():IsUserFeatureEnabled("UserIsChatTranslationEnabled2")
	end)
	userIsChatTranslationEnabled = success and value
end

local module = {}
--////////////////////////////// Include
--//////////////////////////////////////
local chatFolder = script.Parent.Parent.Parent

local Chat = game:GetService("Chat")
local clientChatModules = chatFolder:WaitForChild("ClientChatModules")
local modulesFolder = script.Parent

local ChatSettings = require(clientChatModules:WaitForChild("ChatSettings"))

local chatTranslationEnabled = nil
if userIsChatTranslationEnabled then
	chatTranslationEnabled = modulesFolder:FindFirstChild("ChatTranslationEnabled")
	if chatTranslationEnabled == nil then
		local chatTranslationSettingSignal
		chatTranslationSettingSignal = modulesFolder.ChildAdded:Connect(function(child)
			if child.Name == "ChatTranslationEnabled" then
				chatTranslationEnabled = child

				chatTranslationSettingSignal:Disconnect()
			end
		end)
	end
end

--////////////////////////////// Methods
--//////////////////////////////////////
local methods = {}
methods.__index = methods

function methods:Destroy()
	self.Destroyed = true
end

function methods:SetActive(active)
	if active == self.Active then
		return
	end
	if active == false then
		self.MessageLogDisplay:Clear()
	else
		self.MessageLogDisplay:SetCurrentChannelName(self.Name)
		for i = 1, #self.MessageLog do
			self.MessageLogDisplay:AddMessage(self.MessageLog[i])
		end
	end
	self.Active = active
end

function methods:UpdateMessageFiltered(messageData)
	local searchIndex = 1
	local searchTable = self.MessageLog
	local messageObj = nil
	while #searchTable >= searchIndex do
		local obj = searchTable[searchIndex]

		if obj.ID == messageData.ID then
			messageObj = obj
			break
		end

		searchIndex = searchIndex + 1
	end

	if messageObj then
		messageObj.Message = messageData.Message
		messageObj.IsFiltered = true
		if userIsChatTranslationEnabled and chatTranslationEnabled ~= nil and chatTranslationEnabled.Value then
			messageObj.TranslatedMessage = messageData.TranslatedMessage
			messageObj.ShowTranslated = true
		end
		if self.Active then
			if UserFlagRemoveMessageFromMessageLog then
				if messageObj.Message == "" then
					table.remove(self.MessageLog, searchIndex)
				end
			end
			self.MessageLogDisplay:UpdateMessageFiltered(messageObj)
		end
	else
		-- We have not seen this filtered message before, but we should still add it to our log.
		self:AddMessageToChannelByTimeStamp(messageData)
	end
end

function methods:AddMessageToChannel(messageData)
	table.insert(self.MessageLog, messageData)
	if self.Active then
		self.MessageLogDisplay:AddMessage(messageData)
	end
	if #self.MessageLog > ChatSettings.MessageHistoryLengthPerChannel then
		self:RemoveLastMessageFromChannel()
	end
end

function methods:InternalAddMessageAtTimeStamp(messageData)
	for i = 1, #self.MessageLog do
		if messageData.Time < self.MessageLog[i].Time then
			table.insert(self.MessageLog, i, messageData)
			return
		end
	end
	table.insert(self.MessageLog, messageData)
end

function methods:AddMessagesToChannelByTimeStamp(messageLog, startIndex)
	for i = startIndex, #messageLog do
		self:InternalAddMessageAtTimeStamp(messageLog[i])
	end
	while #self.MessageLog > ChatSettings.MessageHistoryLengthPerChannel do
		table.remove(self.MessageLog, 1)
	end
	if self.Active then
		self.MessageLogDisplay:Clear()
		for i = 1, #self.MessageLog do
			self.MessageLogDisplay:AddMessage(self.MessageLog[i])
		end
	end
end

function methods:AddMessageToChannelByTimeStamp(messageData)
	if #self.MessageLog >= 1 then
		-- These are the fast cases to evalutate.
		if self.MessageLog[1].Time > messageData.Time then
			return
		elseif messageData.Time >= self.MessageLog[#self.MessageLog].Time then
			self:AddMessageToChannel(messageData)
			return
		end

		for i = 1, #self.MessageLog do
			if messageData.Time < self.MessageLog[i].Time then
				table.insert(self.MessageLog, i, messageData)

				if #self.MessageLog > ChatSettings.MessageHistoryLengthPerChannel then
					self:RemoveLastMessageFromChannel()
				end

				if self.Active then
					self.MessageLogDisplay:AddMessageAtIndex(messageData, i)
				end

				return
			end
		end
	else
		self:AddMessageToChannel(messageData)
	end
end

function methods:RemoveLastMessageFromChannel()
	table.remove(self.MessageLog, 1)

	if self.Active then
		self.MessageLogDisplay:RemoveLastMessage()
	end
end

function methods:ClearMessageLog()
	self.MessageLog = {}

	if self.Active then
		self.MessageLogDisplay:Clear()
	end
end

function methods:RegisterChannelTab(tab)
	self.ChannelTab = tab
end

--///////////////////////// Constructors
--//////////////////////////////////////

function module.new(channelName, messageLogDisplay)
	local obj = setmetatable({}, methods)
	obj.Destroyed = false
	obj.Active = false

	obj.MessageLog = {}
	obj.MessageLogDisplay = messageLogDisplay
	obj.ChannelTab = nil
	obj.Name = channelName

	return obj
end

return module
