local q={}q.__index=q local function U()local q=Instance.new("\070\114\097\109\101")q.Selectable=false q.Size=UDim2.new(1,0,1,0)q.BackgroundTransparency=1 local U=Instance.new("\083\099\114\111\108\108\105\110\103\070\114\097\109\101")U.Selectable=ChatSettings.GamepadNavigationEnabled U.Name="\083\099\114\111\108\108\101\114"U.BackgroundTransparency=1 U.BorderSizePixel=0 U.Position=UDim2.new(0,0,0,3)U.Size=UDim2.new(1,-4,1,-6)U.CanvasSize=UDim2.new(0,0,0,0)U.ScrollBarThickness=module.ScrollBarThickness U.Active=FFlagUserChatHistorySinksInput U.Parent=q local Q=Instance.new("\085\073\076\105\115\116\076\097\121\111\117\116")Q.SortOrder=Enum.SortOrder.LayoutOrder Q.Parent=U return q,U,Q end function q.Destroy(q)q.GuiObject:Destroy()q.Destroyed=true end function q.SetActive(U,q)U.GuiObject.Visible=q end function q.UpdateMessageFiltered(U,q)local Q=nil local T=1 local R=U.MessageObjectLog while#R>=T do local U=R[T]if U.ID==q.ID then Q=U break end T=T+1 end if Q then Q.UpdateTextFunction(q)U:PositionMessageLabelInWindow(Q,T)end end function q.AddMessage(U,q)U:WaitUntilParentedCorrectly()local Q=MessageLabelCreator:CreateMessageLabel(q,U.CurrentChannelName)if Q==nil then return end table.insert(U.MessageObjectLog,Q)U:PositionMessageLabelInWindow(Q,#U.MessageObjectLog)end function q.AddMessageAtIndex(Q,q,U)local T=MessageLabelCreator:CreateMessageLabel(q,Q.CurrentChannelName)if T==nil then return end table.insert(Q.MessageObjectLog,U,T)Q:PositionMessageLabelInWindow(T,U)end function q.RemoveLastMessage(q)q:WaitUntilParentedCorrectly()local U=q.MessageObjectLog[1]U:Destroy()table.remove(q.MessageObjectLog,1)end function q.IsScrolledDown(q)local U=q.Scroller.CanvasSize.Y.Offset local Q=q.Scroller.AbsoluteWindowSize.Y local T=q.Scroller.CanvasPosition.Y return U<Q or U-T<=Q+5 end function q.UpdateMessageTextHeight(U,q)local Q=q.BaseFrame for T=1,10,1 do if q.BaseMessage.TextFits then break end local R=U.Scroller.AbsoluteSize.X-T Q.Size=UDim2.new(1,0,0,q.GetHeightFunction(R))end end function q.PositionMessageLabelInWindow(Q,q,U)Q:WaitUntilParentedCorrectly()local T=Q:IsScrolledDown()local R=q.BaseFrame local D=1 if Q.MessageObjectLog[U-1]then if U==#Q.MessageObjectLog then D=Q.MessageObjectLog[U-1].BaseFrame.LayoutOrder+1 else D=Q.MessageObjectLog[U-1].BaseFrame.LayoutOrder end end R.LayoutOrder=D R.Size=UDim2.new(1,0,0,q.GetHeightFunction(Q.Scroller.AbsoluteSize.X))R.Parent=Q.Scroller if q.BaseMessage then Q:UpdateMessageTextHeight(q)end if T then Q.Scroller.CanvasPosition=Vector2.new(0,math.max(0,Q.Scroller.CanvasSize.Y.Offset-Q.Scroller.AbsoluteSize.Y))end end function q.ReorderAllMessages(q)q:WaitUntilParentedCorrectly()if q.GuiObject.AbsoluteSize.Y<1 then return end local U=q.Scroller.CanvasPosition local Q=q:IsScrolledDown()for U,Q in pairs(q.MessageObjectLog)do q:UpdateMessageTextHeight(Q)end if not Q then q.Scroller.CanvasPosition=U else q.Scroller.CanvasPosition=Vector2.new(0,math.max(0,q.Scroller.CanvasSize.Y.Offset-q.Scroller.AbsoluteSize.Y))end end function q.Clear(q)for q,U in pairs(q.MessageObjectLog)do U:Destroy()end q.MessageObjectLog={}end function q.SetCurrentChannelName(U,q)U.CurrentChannelName=q end function q.FadeOutBackground(U,q) end function q.FadeInBackground(U,q) end function q.FadeOutText(U,q)for Q=1,#U.MessageObjectLog,1 do if U.MessageObjectLog[Q].FadeOutFunction then U.MessageObjectLog[Q].FadeOutFunction(q,CurveUtil)end end end function q.FadeInText(U,q)for Q=1,#U.MessageObjectLog,1 do if U.MessageObjectLog[Q].FadeInFunction then U.MessageObjectLog[Q].FadeInFunction(q,CurveUtil)end end end function q.Update(U,q)for Q=1,#U.MessageObjectLog,1 do if U.MessageObjectLog[Q].UpdateAnimFunction then U.MessageObjectLog[Q].UpdateAnimFunction(q,CurveUtil)end end end
--////////////////////////////// Methods
--//////////////////////////////////////
local methods = {}
methods.__index = methods

local function CreateGuiObjects()
	local BaseFrame = Instance.new("Frame")
	BaseFrame.Selectable = false
	BaseFrame.Size = UDim2.new(1, 0, 1, 0)
	BaseFrame.BackgroundTransparency = 1

	local Scroller = Instance.new("ScrollingFrame")
	Scroller.Selectable = ChatSettings.GamepadNavigationEnabled
	Scroller.Name = "Scroller"
	Scroller.BackgroundTransparency = 1
	Scroller.BorderSizePixel = 0
	Scroller.Position = UDim2.new(0, 0, 0, 3)
	Scroller.Size = UDim2.new(1, -4, 1, -6)
	Scroller.CanvasSize = UDim2.new(0, 0, 0, 0)
	Scroller.ScrollBarThickness = module.ScrollBarThickness
	Scroller.Active = FFlagUserChatHistorySinksInput
	Scroller.Parent = BaseFrame

	local Layout = Instance.new("UIListLayout")
	Layout.SortOrder = Enum.SortOrder.LayoutOrder
	Layout.Parent = Scroller

	return BaseFrame, Scroller, Layout
end

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

function methods:SetActive(active)
	self.GuiObject.Visible = active
end

function methods:UpdateMessageFiltered(messageData)
	local messageObject = nil
	local searchIndex = 1
	local searchTable = self.MessageObjectLog

	while #searchTable >= searchIndex do
		local obj = searchTable[searchIndex]

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

		searchIndex = searchIndex + 1
	end

	if messageObject then
		messageObject.UpdateTextFunction(messageData)
		self:PositionMessageLabelInWindow(messageObject, searchIndex)
	end
end

function methods:AddMessage(messageData)
	self:WaitUntilParentedCorrectly()

	local messageObject = MessageLabelCreator:CreateMessageLabel(messageData, self.CurrentChannelName)
	if messageObject == nil then
		return
	end

	table.insert(self.MessageObjectLog, messageObject)
	self:PositionMessageLabelInWindow(messageObject, #self.MessageObjectLog)
end

function methods:AddMessageAtIndex(messageData, index)
	local messageObject = MessageLabelCreator:CreateMessageLabel(messageData, self.CurrentChannelName)
	if messageObject == nil then
		return
	end

	table.insert(self.MessageObjectLog, index, messageObject)

	self:PositionMessageLabelInWindow(messageObject, index)
end

function methods:RemoveLastMessage()
	self:WaitUntilParentedCorrectly()

	local lastMessage = self.MessageObjectLog[1]

	lastMessage:Destroy()
	table.remove(self.MessageObjectLog, 1)
end

function methods:IsScrolledDown()
	local yCanvasSize = self.Scroller.CanvasSize.Y.Offset
	local yContainerSize = self.Scroller.AbsoluteWindowSize.Y
	local yScrolledPosition = self.Scroller.CanvasPosition.Y

	return (yCanvasSize < yContainerSize or yCanvasSize - yScrolledPosition <= yContainerSize + 5)
end

function methods:UpdateMessageTextHeight(messageObject)
	local baseFrame = messageObject.BaseFrame
	for i = 1, 10 do
		if messageObject.BaseMessage.TextFits then
			break
		end

		local trySize = self.Scroller.AbsoluteSize.X - i
		baseFrame.Size = UDim2.new(1, 0, 0, messageObject.GetHeightFunction(trySize))
	end
end

function methods:PositionMessageLabelInWindow(messageObject, index)
	self:WaitUntilParentedCorrectly()

	local wasScrolledDown = self:IsScrolledDown()

	local baseFrame = messageObject.BaseFrame

	local layoutOrder = 1
	if self.MessageObjectLog[index - 1] then
		if index == #self.MessageObjectLog then
			layoutOrder = self.MessageObjectLog[index - 1].BaseFrame.LayoutOrder + 1
		else
			layoutOrder = self.MessageObjectLog[index - 1].BaseFrame.LayoutOrder
		end
	end
	baseFrame.LayoutOrder = layoutOrder

	baseFrame.Size = UDim2.new(1, 0, 0, messageObject.GetHeightFunction(self.Scroller.AbsoluteSize.X))
	baseFrame.Parent = self.Scroller

	if messageObject.BaseMessage then
		self:UpdateMessageTextHeight(messageObject)
	end

	if wasScrolledDown then
		self.Scroller.CanvasPosition =
			Vector2.new(0, math.max(0, self.Scroller.CanvasSize.Y.Offset - self.Scroller.AbsoluteSize.Y))
	end
end

function methods:ReorderAllMessages()
	self:WaitUntilParentedCorrectly()

	--// Reordering / reparenting with a size less than 1 causes weird glitches to happen
	-- with scrolling as repositioning happens.
	if self.GuiObject.AbsoluteSize.Y < 1 then
		return
	end

	local oldCanvasPositon = self.Scroller.CanvasPosition
	local wasScrolledDown = self:IsScrolledDown()

	for _, messageObject in pairs(self.MessageObjectLog) do
		self:UpdateMessageTextHeight(messageObject)
	end

	if not wasScrolledDown then
		self.Scroller.CanvasPosition = oldCanvasPositon
	else
		self.Scroller.CanvasPosition =
			Vector2.new(0, math.max(0, self.Scroller.CanvasSize.Y.Offset - self.Scroller.AbsoluteSize.Y))
	end
end

function methods:Clear()
	for _, v in pairs(self.MessageObjectLog) do
		v:Destroy()
	end
	self.MessageObjectLog = {}
end

function methods:SetCurrentChannelName(name)
	self.CurrentChannelName = name
end

function methods:FadeOutBackground(duration)
	--// Do nothing
end

function methods:FadeInBackground(duration)
	--// Do nothing
end

function methods:FadeOutText(duration)
	for i = 1, #self.MessageObjectLog do
		if self.MessageObjectLog[i].FadeOutFunction then
			self.MessageObjectLog[i].FadeOutFunction(duration, CurveUtil)
		end
	end
end

function methods:FadeInText(duration)
	for i = 1, #self.MessageObjectLog do
		if self.MessageObjectLog[i].FadeInFunction then
			self.MessageObjectLog[i].FadeInFunction(duration, CurveUtil)
		end
	end
end

function methods:Update(dtScale)
	for i = 1, #self.MessageObjectLog do
		if self.MessageObjectLog[i].UpdateAnimFunction then
			self.MessageObjectLog[i].UpdateAnimFunction(dtScale, CurveUtil)
		end
	end
end
