local N={}do function N.Signal()local N={}local F=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local O=nil local l=nil function N.fire(N,...)O={...}l=select("\035",...)F:Fire()end function N.connect(W,N)if not N then error("\099\111\110\110\101\099\116\040\110\105\108\041",2)end return F.Event:connect(function()N(unpack(O,1,l))end)end function N.Wait(N)F.Event:Wait()assert(O,"\077\105\115\115\105\110\103 \097\114\103 \100\097\116\097\044 \108\105\107\101\108\121 \100\117\101 \116\111 \058\084\119\101\101\110\083\105\122\101\047\080\111\115\105\116\105\111\110 \099\111\114\114\117\112\116\105\110\103 \116\104\114\101\097\100\114\101\102\115\046")return unpack(O,1,l)end return N end end function SetVisibility(N)ChatWindow:SetVisible(N)moduleApiTable.VisibilityStateChanged:fire(N)moduleApiTable.Visible=N if moduleApiTable.IsCoreGuiEnabled then if N then InstantFadeIn()else InstantFadeOut()end end end do moduleApiTable.TopbarEnabled=true moduleApiTable.MessageCount=0 moduleApiTable.Visible=true moduleApiTable.IsCoreGuiEnabled=true function moduleApiTable.ToggleVisibility(N)SetVisibility(not ChatWindow:GetVisible())end function moduleApiTable.SetVisible(F,N)if ChatWindow:GetVisible()~=N then SetVisibility(N)end end function moduleApiTable.FocusChatBar(N)ChatBar:CaptureFocus()end function moduleApiTable.GetVisibility(N)return ChatWindow:GetVisible()end function moduleApiTable.GetMessageCount(N)return N.MessageCount end function moduleApiTable.TopbarEnabledChanged(F,N)F.TopbarEnabled=N F.CoreGuiEnabled:fire((game:GetService("\083\116\097\114\116\101\114\071\117\105")):GetCoreGuiEnabled(Enum.CoreGuiType.Chat))end function moduleApiTable.IsFocused(F,N)return ChatBar:IsFocused()end moduleApiTable.ChatBarFocusChanged=N.Signal()moduleApiTable.VisibilityStateChanged=N.Signal()moduleApiTable.MessagesChanged=N.Signal()moduleApiTable.MessagePosted=N.Signal()moduleApiTable.CoreGuiEnabled=N.Signal()moduleApiTable.ChatMakeSystemMessageEvent=N.Signal()moduleApiTable.ChatWindowPositionEvent=N.Signal()moduleApiTable.ChatWindowSizeEvent=N.Signal()moduleApiTable.ChatBarDisabledEvent=N.Signal()function moduleApiTable.fChatWindowPosition(N)return ChatWindow.GuiObject.Position end function moduleApiTable.fChatWindowSize(N)return ChatWindow.GuiObject.Size end function moduleApiTable.fChatBarDisabled(N)return not ChatBar:GetEnabled()end function moduleApiTable.SpecialKeyPressed(O,N,F)if N==Enum.SpecialKey.ChatHotkey then if canChat then DoChatBarFocus()end end end end moduleApiTable.CoreGuiEnabled:connect(function(N)moduleApiTable.IsCoreGuiEnabled=N N=N and(moduleApiTable.TopbarEnabled or ChatSettings.ChatOnWithTopBarOff)ChatWindow:SetCoreGuiEnabled(N)if not N then ChatBar:ReleaseFocus()InstantFadeOut()else InstantFadeIn()end end)function trimTrailingSpaces(N)local F=#N while F>0 do if N:find("\094\037\115",F)then F=F-1 else break end end return N:sub(1,F)end moduleApiTable.ChatMakeSystemMessageEvent:connect(function(N)if N.Text and type(N.Text)=="\115\116\114\105\110\103"then while not DidFirstChannelsLoads do wait()end local F=ChatSettings.GeneralChannelName local O=ChatWindow:GetChannel(F)if O then local l={ID=-1;FromSpeaker=nil,SpeakerUserId=0;OriginalChannel=F,IsFiltered=true;MessageLength=string.len(N.Text);Message=trimTrailingSpaces(N.Text);MessageType=ChatConstants.MessageTypeSetCore,Time=os.time();ExtraData=N}O:AddMessageToChannel(l)ChannelsBar:UpdateMessagePostedInChannel(F)moduleApiTable.MessageCount=moduleApiTable.MessageCount+1 moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)end end end)moduleApiTable.ChatBarDisabledEvent:connect(function(N)if canChat then ChatBar:SetEnabled(not N)if N then ChatBar:ReleaseFocus()end end end)moduleApiTable.ChatWindowSizeEvent:connect(function(N)ChatWindow.GuiObject.Size=N end)moduleApiTable.ChatWindowPositionEvent:connect(function(N)ChatWindow.GuiObject.Position=N end)
--////////////////////////////////////////////////////////////////////////////////////////////
--///////////// Code to talk to topbar and maintain set/get core backwards compatibility stuff
--////////////////////////////////////////////////////////////////////////////////////////////
local Util = {}
do
	function Util.Signal()
		local sig = {}

		local mSignaler = Instance.new("BindableEvent")

		local mArgData = nil
		local mArgDataCount = nil

		function sig:fire(...)
			mArgData = { ... }
			mArgDataCount = select("#", ...)
			mSignaler:Fire()
		end

		function sig:connect(f)
			if not f then
				error("connect(nil)", 2)
			end
			return mSignaler.Event:connect(function()
				f(unpack(mArgData, 1, mArgDataCount))
			end)
		end

		function sig:Wait()
			mSignaler.Event:Wait()
			assert(mArgData, "Missing arg data, likely due to :TweenSize/Position corrupting threadrefs.")
			return unpack(mArgData, 1, mArgDataCount)
		end

		return sig
	end
end

function SetVisibility(val)
	ChatWindow:SetVisible(val)
	moduleApiTable.VisibilityStateChanged:fire(val)
	moduleApiTable.Visible = val

	if moduleApiTable.IsCoreGuiEnabled then
		if val then
			InstantFadeIn()
		else
			InstantFadeOut()
		end
	end
end

do
	moduleApiTable.TopbarEnabled = true
	moduleApiTable.MessageCount = 0
	moduleApiTable.Visible = true
	moduleApiTable.IsCoreGuiEnabled = true

	function moduleApiTable:ToggleVisibility()
		SetVisibility(not ChatWindow:GetVisible())
	end

	function moduleApiTable:SetVisible(visible)
		if ChatWindow:GetVisible() ~= visible then
			SetVisibility(visible)
		end
	end

	function moduleApiTable:FocusChatBar()
		ChatBar:CaptureFocus()
	end

	function moduleApiTable:GetVisibility()
		return ChatWindow:GetVisible()
	end

	function moduleApiTable:GetMessageCount()
		return self.MessageCount
	end

	function moduleApiTable:TopbarEnabledChanged(enabled)
		self.TopbarEnabled = enabled
		self.CoreGuiEnabled:fire(game:GetService("StarterGui"):GetCoreGuiEnabled(Enum.CoreGuiType.Chat))
	end

	function moduleApiTable:IsFocused(useWasFocused)
		return ChatBar:IsFocused()
	end

	moduleApiTable.ChatBarFocusChanged = Util.Signal()
	moduleApiTable.VisibilityStateChanged = Util.Signal()
	moduleApiTable.MessagesChanged = Util.Signal()

	moduleApiTable.MessagePosted = Util.Signal()
	moduleApiTable.CoreGuiEnabled = Util.Signal()

	moduleApiTable.ChatMakeSystemMessageEvent = Util.Signal()
	moduleApiTable.ChatWindowPositionEvent = Util.Signal()
	moduleApiTable.ChatWindowSizeEvent = Util.Signal()
	moduleApiTable.ChatBarDisabledEvent = Util.Signal()

	function moduleApiTable:fChatWindowPosition()
		return ChatWindow.GuiObject.Position
	end

	function moduleApiTable:fChatWindowSize()
		return ChatWindow.GuiObject.Size
	end

	function moduleApiTable:fChatBarDisabled()
		return not ChatBar:GetEnabled()
	end

	function moduleApiTable:SpecialKeyPressed(key, modifiers)
		if key == Enum.SpecialKey.ChatHotkey then
			if canChat then
				DoChatBarFocus()
			end
		end
	end
end

moduleApiTable.CoreGuiEnabled:connect(function(enabled)
	moduleApiTable.IsCoreGuiEnabled = enabled

	enabled = enabled and (moduleApiTable.TopbarEnabled or ChatSettings.ChatOnWithTopBarOff)

	ChatWindow:SetCoreGuiEnabled(enabled)

	if not enabled then
		ChatBar:ReleaseFocus()
		InstantFadeOut()
	else
		InstantFadeIn()
	end
end)

function trimTrailingSpaces(str)
	local lastSpace = #str
	while lastSpace > 0 do
		--- The pattern ^%s matches whitespace at the start of the string. (Starting from lastSpace)
		if str:find("^%s", lastSpace) then
			lastSpace = lastSpace - 1
		else
			break
		end
	end
	return str:sub(1, lastSpace)
end

moduleApiTable.ChatMakeSystemMessageEvent:connect(function(valueTable)
	if valueTable["Text"] and type(valueTable["Text"]) == "string" then
		while not DidFirstChannelsLoads do
			wait()
		end

		local channel = ChatSettings.GeneralChannelName
		local channelObj = ChatWindow:GetChannel(channel)

		if channelObj then
			local messageObject = {
				ID = -1,
				FromSpeaker = nil,
				SpeakerUserId = 0,
				OriginalChannel = channel,
				IsFiltered = true,
				MessageLength = string.len(valueTable.Text),
				Message = trimTrailingSpaces(valueTable.Text),
				MessageType = ChatConstants.MessageTypeSetCore,
				Time = os.time(),
				ExtraData = valueTable,
			}
			channelObj:AddMessageToChannel(messageObject)
			ChannelsBar:UpdateMessagePostedInChannel(channel)

			moduleApiTable.MessageCount = moduleApiTable.MessageCount + 1
			moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)
		end
	end
end)

moduleApiTable.ChatBarDisabledEvent:connect(function(disabled)
	if canChat then
		ChatBar:SetEnabled(not disabled)
		if disabled then
			ChatBar:ReleaseFocus()
		end
	end
end)

moduleApiTable.ChatWindowSizeEvent:connect(function(size)
	ChatWindow.GuiObject.Size = size
end)

moduleApiTable.ChatWindowPositionEvent:connect(function(position)
	ChatWindow.GuiObject.Position = position
end)
