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

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
