function methods.GetMessageHeight(D,F,c,b)b=b or c.AbsoluteSize.X local W=D:GetStringTextBounds(F.Text,F.Font,F.TextSize,Vector2.new(b,1000))return W.Y end function methods.GetNumberOfSpaces(D,F,c,b)local W=D:GetStringTextBounds(F,c,b)local e=D:GetStringTextBounds(" ",c,b)return math.ceil(W.X/e.X)end function methods.CreateBaseMessage(W,F,c,b,D)local e=W:GetFromObjectPool("\070\114\097\109\101")e.Selectable=false e.Size=UDim2.new(1,0,0,18)e.Visible=true e.BackgroundTransparency=1 local V=8 local J=W:GetFromObjectPool("\084\101\120\116\076\097\098\101\108")J.Selectable=false J.Size=UDim2.new(1,-(V+6),1,0)J.Position=UDim2.new(0,V,0,0)J.BackgroundTransparency=1 J.Font=c J.TextSize=b J.TextXAlignment=Enum.TextXAlignment.Left J.TextYAlignment=Enum.TextYAlignment.Top J.TextTransparency=0 J.TextStrokeTransparency=.75 J.TextColor3=D J.TextWrapped=true J.Text=F J.Visible=true J.Parent=e return e,J end function methods.AddNameButtonToBaseMessage(W,F,c,b,D)local e=W:GetStringTextBounds(b,F.Font,F.TextSize)local V=W:GetFromObjectPool("\084\101\120\116\066\117\116\116\111\110")V.Selectable=false V.Size=UDim2.new(0,e.X,0,e.Y)V.Position=UDim2.new(0,0,0,0)V.BackgroundTransparency=1 V.Font=F.Font V.TextSize=F.TextSize V.TextXAlignment=F.TextXAlignment V.TextYAlignment=F.TextYAlignment V.TextTransparency=F.TextTransparency V.TextStrokeTransparency=F.TextStrokeTransparency V.TextColor3=c V.Text=b V.Visible=true V.Parent=F local J=V.MouseButton1Click:connect(function()W:NameButtonClicked(V,D)end)local v=nil v=V.Changed:connect(function(F)if F=="\080\097\114\101\110\116"then J:Disconnect()v:Disconnect()end end)return V end function methods.AddChannelButtonToBaseMessage(W,F,c,b,D)local e=W:GetStringTextBounds(b,F.Font,F.TextSize)local V=W:GetFromObjectPool("\084\101\120\116\066\117\116\116\111\110")V.Selectable=false V.Size=UDim2.new(0,e.X,0,e.Y)V.Position=UDim2.new(0,0,0,0)V.BackgroundTransparency=1 V.Font=F.Font V.TextSize=F.TextSize V.TextXAlignment=F.TextXAlignment V.TextYAlignment=F.TextYAlignment V.TextTransparency=F.TextTransparency V.TextStrokeTransparency=F.TextStrokeTransparency V.TextColor3=c V.Text=b V.Visible=true V.Parent=F local J=V.MouseButton1Click:connect(function()W:ChannelButtonClicked(V,D)end)local v=nil v=V.Changed:connect(function(F)if F=="\080\097\114\101\110\116"then J:Disconnect()v:Disconnect()end end)return V end function GetWhisperChannelPrefix()if ChatConstants.WhisperChannelPrefix then return ChatConstants.WhisperChannelPrefix end return"\084\111 "end function methods.NameButtonClicked(b,F,c)if not b.ChatWindow then return end if ChatSettings.ClickOnPlayerNameToWhisper then local F=Players:FindFirstChild(c)if F and F~=LocalPlayer then local F=GetWhisperChannelPrefix()..c if b.ChatWindow:GetChannel(F)then b.ChatBar:ResetCustomState()local c=b.ChatWindow:GetTargetMessageChannel()if c~=F then b.ChatWindow:SwitchCurrentChannel(F)end b.ChatBar:ResetText()b.ChatBar:CaptureFocus()elseif not b.ChatBar:IsInCustomState()then local F="\047\119 "..c b.ChatBar:CaptureFocus()b.ChatBar:SetText(F)end end end end function methods.ChannelButtonClicked(b,F,c)if not b.ChatWindow then return end if ChatSettings.ClickOnChannelNameToSetMainChannel then if b.ChatWindow:GetChannel(c)then b.ChatBar:ResetCustomState()local F=b.ChatWindow:GetTargetMessageChannel()if F~=c then b.ChatWindow:SwitchCurrentChannel(c)end b.ChatBar:ResetText()b.ChatBar:CaptureFocus()end end end function methods.RegisterChatWindow(c,F)c.ChatWindow=F c.ChatBar=F:GetChatBar()end function methods.GetFromObjectPool(c,F)if c.ObjectPool==nil then return Instance.new(F)end return c.ObjectPool:GetInstance(F)end function methods.RegisterObjectPool(c,F)c.ObjectPool=F 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: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
