function methods.GetMessageHeight(C,Y,n,o)o=o or n.AbsoluteSize.X local b=C:GetStringTextBounds(Y.Text,Y.Font,Y.TextSize,Vector2.new(o,1000))return b.Y end function methods.GetNumberOfSpaces(C,Y,n,o)local b=C:GetStringTextBounds(Y,n,o)local v=C:GetStringTextBounds(" ",n,o)return math.ceil(b.X/v.X)end function methods.CreateBaseMessage(b,Y,n,o,C)local v=b:GetFromObjectPool("\070\114\097\109\101")v.Selectable=false v.Size=UDim2.new(1,0,0,18)v.Visible=true v.BackgroundTransparency=1 local M=8 local X=b:GetFromObjectPool("\084\101\120\116\076\097\098\101\108")X.Selectable=false X.Size=UDim2.new(1,-(M+6),1,0)X.Position=UDim2.new(0,M,0,0)X.BackgroundTransparency=1 X.Font=n X.TextSize=o X.TextXAlignment=Enum.TextXAlignment.Left X.TextYAlignment=Enum.TextYAlignment.Top X.TextTransparency=0 X.TextStrokeTransparency=.75 X.TextColor3=C X.TextWrapped=true X.Text=Y X.Visible=true X.Parent=v return v,X end function methods.AddNameButtonToBaseMessage(b,Y,n,o,C)local v=b:GetStringTextBounds(o,Y.Font,Y.TextSize)local M=b:GetFromObjectPool("\084\101\120\116\066\117\116\116\111\110")M.Selectable=false M.Size=UDim2.new(0,v.X,0,v.Y)M.Position=UDim2.new(0,0,0,0)M.BackgroundTransparency=1 M.Font=Y.Font M.TextSize=Y.TextSize M.TextXAlignment=Y.TextXAlignment M.TextYAlignment=Y.TextYAlignment M.TextTransparency=Y.TextTransparency M.TextStrokeTransparency=Y.TextStrokeTransparency M.TextColor3=n M.Text=o M.Visible=true M.Parent=Y local X=M.MouseButton1Click:connect(function()b:NameButtonClicked(M,C)end)local f=nil f=M.Changed:connect(function(Y)if Y=="\080\097\114\101\110\116"then X:Disconnect()f:Disconnect()end end)return M end function methods.AddPrefixLabelToBaseMessage(C,Y,n,o)local b=C:GetStringTextBounds(o,Y.Font,Y.TextSize)local v=C:GetFromObjectPool("\084\101\120\116\076\097\098\101\108")v.Selectable=false v.Size=UDim2.new(0,b.X,0,b.Y)v.Position=UDim2.new(0,0,0,0)v.BackgroundTransparency=1 v.Font=Y.Font v.TextSize=Y.TextSize v.TextXAlignment=Y.TextXAlignment v.TextYAlignment=Y.TextYAlignment v.TextTransparency=Y.TextTransparency v.TextStrokeTransparency=Y.TextStrokeTransparency v.TextColor3=n v.Text=o v.Visible=true v.Parent=Y return v end function methods.AddChannelButtonToBaseMessage(b,Y,n,o,C)local v=b:GetStringTextBounds(o,Y.Font,Y.TextSize)local M=b:GetFromObjectPool("\084\101\120\116\066\117\116\116\111\110")M.Selectable=false M.Size=UDim2.new(0,v.X,0,v.Y)M.Position=UDim2.new(0,0,0,0)M.BackgroundTransparency=1 M.Font=Y.Font M.TextSize=Y.TextSize M.TextXAlignment=Y.TextXAlignment M.TextYAlignment=Y.TextYAlignment M.TextTransparency=Y.TextTransparency M.TextStrokeTransparency=Y.TextStrokeTransparency M.TextColor3=n M.Text=o M.Visible=true M.Parent=Y local X=M.MouseButton1Click:connect(function()b:ChannelButtonClicked(M,C)end)local f=nil f=M.Changed:connect(function(Y)if Y=="\080\097\114\101\110\116"then X:Disconnect()f:Disconnect()end end)return M end function GetWhisperChannelPrefix()if ChatConstants.WhisperChannelPrefix then return ChatConstants.WhisperChannelPrefix end return"\084\111 "end function methods.NameButtonClicked(o,Y,n)if not o.ChatWindow then return end if ChatSettings.ClickOnPlayerNameToWhisper then local Y=Players:FindFirstChild(n)if Y and Y~=LocalPlayer then local Y=GetWhisperChannelPrefix()..n if o.ChatWindow:GetChannel(Y)then o.ChatBar:ResetCustomState()local n=o.ChatWindow:GetTargetMessageChannel()if n~=Y then o.ChatWindow:SwitchCurrentChannel(Y)end o.ChatBar:ResetText()o.ChatBar:CaptureFocus()elseif not o.ChatBar:IsInCustomState()then local Y="\047\119 "..n o.ChatBar:CaptureFocus()o.ChatBar:SetText(Y)end end end end function methods.ChannelButtonClicked(o,Y,n)if not o.ChatWindow then return end if ChatSettings.ClickOnChannelNameToSetMainChannel then if o.ChatWindow:GetChannel(n)then o.ChatBar:ResetCustomState()local Y=o.ChatWindow:GetTargetMessageChannel()if Y~=n then o.ChatWindow:SwitchCurrentChannel(n)end o.ChatBar:ResetText()o.ChatBar:CaptureFocus()end end end function methods.RegisterChatWindow(n,Y)n.ChatWindow=Y n.ChatBar=Y:GetChatBar()end function methods.GetFromObjectPool(n,Y)if n.ObjectPool==nil then return Instance.new(Y)end return n.ObjectPool:GetInstance(Y)end function methods.RegisterObjectPool(n,Y)n.ObjectPool=Y end
--// Above was taken directly from Util.GetStringTextBounds() in the old chat corescripts.

function methods:GetMessageHeight(BaseMessage, BaseFrame, xSize)
	xSize = xSize or BaseFrame.AbsoluteSize.X
	local textBoundsSize =
		self:GetStringTextBounds(BaseMessage.Text, BaseMessage.Font, BaseMessage.TextSize, Vector2.new(xSize, 1000))
	return textBoundsSize.Y
end

function methods:GetNumberOfSpaces(str, font, textSize)
	local strSize = self:GetStringTextBounds(str, font, textSize)
	local singleSpaceSize = self:GetStringTextBounds(" ", font, textSize)
	return math.ceil(strSize.X / singleSpaceSize.X)
end

function methods:CreateBaseMessage(message, font, textSize, chatColor)
	local BaseFrame = self:GetFromObjectPool("Frame")
	BaseFrame.Selectable = false
	BaseFrame.Size = UDim2.new(1, 0, 0, 18)
	BaseFrame.Visible = true
	BaseFrame.BackgroundTransparency = 1

	local messageBorder = 8

	local BaseMessage = self:GetFromObjectPool("TextLabel")
	BaseMessage.Selectable = false
	BaseMessage.Size = UDim2.new(1, -(messageBorder + 6), 1, 0)
	BaseMessage.Position = UDim2.new(0, messageBorder, 0, 0)
	BaseMessage.BackgroundTransparency = 1
	BaseMessage.Font = font
	BaseMessage.TextSize = textSize
	BaseMessage.TextXAlignment = Enum.TextXAlignment.Left
	BaseMessage.TextYAlignment = Enum.TextYAlignment.Top
	BaseMessage.TextTransparency = 0
	BaseMessage.TextStrokeTransparency = 0.75
	BaseMessage.TextColor3 = chatColor
	BaseMessage.TextWrapped = true
	BaseMessage.Text = message
	BaseMessage.Visible = true
	BaseMessage.Parent = BaseFrame

	return BaseFrame, BaseMessage
end

function methods:AddNameButtonToBaseMessage(BaseMessage, nameColor, formatName, playerName)
	local speakerNameSize = self:GetStringTextBounds(formatName, BaseMessage.Font, BaseMessage.TextSize)
	local NameButton = self:GetFromObjectPool("TextButton")
	NameButton.Selectable = false
	NameButton.Size = UDim2.new(0, speakerNameSize.X, 0, speakerNameSize.Y)
	NameButton.Position = UDim2.new(0, 0, 0, 0)
	NameButton.BackgroundTransparency = 1
	NameButton.Font = BaseMessage.Font
	NameButton.TextSize = BaseMessage.TextSize
	NameButton.TextXAlignment = BaseMessage.TextXAlignment
	NameButton.TextYAlignment = BaseMessage.TextYAlignment
	NameButton.TextTransparency = BaseMessage.TextTransparency
	NameButton.TextStrokeTransparency = BaseMessage.TextStrokeTransparency
	NameButton.TextColor3 = nameColor
	NameButton.Text = formatName
	NameButton.Visible = true
	NameButton.Parent = BaseMessage

	local clickedConn = NameButton.MouseButton1Click:connect(function()
		self:NameButtonClicked(NameButton, playerName)
	end)

	local changedConn = nil
	changedConn = NameButton.Changed:connect(function(prop)
		if prop == "Parent" then
			clickedConn:Disconnect()
			changedConn:Disconnect()
		end
	end)

	return NameButton
end

function methods:AddPrefixLabelToBaseMessage(BaseMessage, prefixColor, formatPrefix)
	local speakerNameSize = self:GetStringTextBounds(formatPrefix, BaseMessage.Font, BaseMessage.TextSize)
	local PrefixLabel = self:GetFromObjectPool("TextLabel")
	PrefixLabel.Selectable = false
	PrefixLabel.Size = UDim2.new(0, speakerNameSize.X, 0, speakerNameSize.Y)
	PrefixLabel.Position = UDim2.new(0, 0, 0, 0)
	PrefixLabel.BackgroundTransparency = 1
	PrefixLabel.Font = BaseMessage.Font
	PrefixLabel.TextSize = BaseMessage.TextSize
	PrefixLabel.TextXAlignment = BaseMessage.TextXAlignment
	PrefixLabel.TextYAlignment = BaseMessage.TextYAlignment
	PrefixLabel.TextTransparency = BaseMessage.TextTransparency
	PrefixLabel.TextStrokeTransparency = BaseMessage.TextStrokeTransparency
	PrefixLabel.TextColor3 = prefixColor
	PrefixLabel.Text = formatPrefix
	PrefixLabel.Visible = true
	PrefixLabel.Parent = BaseMessage

	return PrefixLabel
end

function methods:AddChannelButtonToBaseMessage(BaseMessage, channelColor, formatChannelName, channelName)
	local channelNameSize = self:GetStringTextBounds(formatChannelName, BaseMessage.Font, BaseMessage.TextSize)
	local ChannelButton = self:GetFromObjectPool("TextButton")
	ChannelButton.Selectable = false
	ChannelButton.Size = UDim2.new(0, channelNameSize.X, 0, channelNameSize.Y)
	ChannelButton.Position = UDim2.new(0, 0, 0, 0)
	ChannelButton.BackgroundTransparency = 1
	ChannelButton.Font = BaseMessage.Font
	ChannelButton.TextSize = BaseMessage.TextSize
	ChannelButton.TextXAlignment = BaseMessage.TextXAlignment
	ChannelButton.TextYAlignment = BaseMessage.TextYAlignment
	ChannelButton.TextTransparency = BaseMessage.TextTransparency
	ChannelButton.TextStrokeTransparency = BaseMessage.TextStrokeTransparency
	ChannelButton.TextColor3 = channelColor
	ChannelButton.Text = formatChannelName
	ChannelButton.Visible = true
	ChannelButton.Parent = BaseMessage

	local clickedConn = ChannelButton.MouseButton1Click:connect(function()
		self:ChannelButtonClicked(ChannelButton, channelName)
	end)

	local changedConn = nil
	changedConn = ChannelButton.Changed:connect(function(prop)
		if prop == "Parent" then
			clickedConn:Disconnect()
			changedConn:Disconnect()
		end
	end)

	return ChannelButton
end

function GetWhisperChannelPrefix()
	if ChatConstants.WhisperChannelPrefix then
		return ChatConstants.WhisperChannelPrefix
	end
	return "To "
end

function methods:NameButtonClicked(nameButton, playerName)
	if not self.ChatWindow then
		return
	end

	if ChatSettings.ClickOnPlayerNameToWhisper then
		local player = Players:FindFirstChild(playerName)
		if player and player ~= LocalPlayer then
			local whisperChannel = GetWhisperChannelPrefix() .. playerName
			if self.ChatWindow:GetChannel(whisperChannel) then
				self.ChatBar:ResetCustomState()
				local targetChannelName = self.ChatWindow:GetTargetMessageChannel()
				if targetChannelName ~= whisperChannel then
					self.ChatWindow:SwitchCurrentChannel(whisperChannel)
				end
				self.ChatBar:ResetText()
				self.ChatBar:CaptureFocus()
			elseif not self.ChatBar:IsInCustomState() then
				local whisperMessage = "/w " .. playerName
				self.ChatBar:CaptureFocus()
				self.ChatBar:SetText(whisperMessage)
			end
		end
	end
end

function methods:ChannelButtonClicked(channelButton, channelName)
	if not self.ChatWindow then
		return
	end

	if ChatSettings.ClickOnChannelNameToSetMainChannel then
		if self.ChatWindow:GetChannel(channelName) then
			self.ChatBar:ResetCustomState()
			local targetChannelName = self.ChatWindow:GetTargetMessageChannel()
			if targetChannelName ~= channelName then
				self.ChatWindow:SwitchCurrentChannel(channelName)
			end
			self.ChatBar:ResetText()
			self.ChatBar:CaptureFocus()
		end
	end
end

function methods:RegisterChatWindow(chatWindow)
	self.ChatWindow = chatWindow
	self.ChatBar = chatWindow:GetChatBar()
end

function methods:GetFromObjectPool(className)
	if self.ObjectPool == nil then
		return Instance.new(className)
	end
	return self.ObjectPool:GetInstance(className)
end

function methods:RegisterObjectPool(objectPool)
	self.ObjectPool = objectPool
end
