local p={}do function p.Signal()local p={}local R=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local A=nil local c=nil function p.fire(p,...)A={...}c=select("\035",...)R:Fire()end function p.connect(e,p)if not p then error("\099\111\110\110\101\099\116\040\110\105\108\041",2)end return R.Event:connect(function()p(unpack(A,1,c))end)end function p.wait(p)R.Event:wait()assert(A,"\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(A,1,c)end return p end end function SetVisibility(p)ChatWindow:SetVisible(p)moduleApiTable.VisibilityStateChanged:fire(p)moduleApiTable.Visible=p if moduleApiTable.IsCoreGuiEnabled then if p then InstantFadeIn()else InstantFadeOut()end end end do moduleApiTable.TopbarEnabled=true moduleApiTable.MessageCount=0 moduleApiTable.Visible=true moduleApiTable.IsCoreGuiEnabled=true function moduleApiTable.ToggleVisibility(p)SetVisibility(not ChatWindow:GetVisible())end function moduleApiTable.SetVisible(R,p)if ChatWindow:GetVisible()~=p then SetVisibility(p)end end function moduleApiTable.FocusChatBar(p)ChatBar:CaptureFocus()end function moduleApiTable.EnterWhisperState(R,p)ChatBar:EnterWhisperState(p)end function moduleApiTable.GetVisibility(p)return ChatWindow:GetVisible()end function moduleApiTable.GetMessageCount(p)return p.MessageCount end function moduleApiTable.TopbarEnabledChanged(R,p)R.TopbarEnabled=p R.CoreGuiEnabled:fire((game:GetService("\083\116\097\114\116\101\114\071\117\105")):GetCoreGuiEnabled(Enum.CoreGuiType.Chat))end function moduleApiTable.IsFocused(R,p)return ChatBar:IsFocused()end moduleApiTable.ChatBarFocusChanged=p.Signal()moduleApiTable.VisibilityStateChanged=p.Signal()moduleApiTable.MessagesChanged=p.Signal()moduleApiTable.MessagePosted=p.Signal()moduleApiTable.CoreGuiEnabled=p.Signal()moduleApiTable.ChatMakeSystemMessageEvent=p.Signal()moduleApiTable.ChatWindowPositionEvent=p.Signal()moduleApiTable.ChatWindowSizeEvent=p.Signal()moduleApiTable.ChatBarDisabledEvent=p.Signal()function moduleApiTable.fChatWindowPosition(p)return ChatWindow.GuiObject.Position end function moduleApiTable.fChatWindowSize(p)return ChatWindow.GuiObject.Size end function moduleApiTable.fChatBarDisabled(p)return not ChatBar:GetEnabled()end function moduleApiTable.SpecialKeyPressed(A,p,R)if p==Enum.SpecialKey.ChatHotkey then if canChat then DoChatBarFocus()end end end end moduleApiTable.CoreGuiEnabled:connect(function(p)moduleApiTable.IsCoreGuiEnabled=p p=p and(moduleApiTable.TopbarEnabled or ChatSettings.ChatOnWithTopBarOff)ChatWindow:SetCoreGuiEnabled(p)if not p then ChatBar:ReleaseFocus()InstantFadeOut()else InstantFadeIn()end end)function trimTrailingSpaces(p)local R=#p while R>0 do if p:find("\094\037\115",R)then R=R-1 else break end end return p:sub(1,R)end moduleApiTable.ChatMakeSystemMessageEvent:connect(function(p)if p.Text and type(p.Text)=="\115\116\114\105\110\103"then while not DidFirstChannelsLoads do wait()end local R=ChatSettings.GeneralChannelName local A=ChatWindow:GetChannel(R)if A then local c={ID=-1;FromSpeaker=nil;SpeakerUserId=0;OriginalChannel=R,IsFiltered=true;MessageLength=string.len(p.Text),Message=trimTrailingSpaces(p.Text),MessageType=ChatConstants.MessageTypeSetCore,Time=os.time(),ExtraData=p}A:AddMessageToChannel(c)ChannelsBar:UpdateMessagePostedInChannel(R)moduleApiTable.MessageCount=moduleApiTable.MessageCount+1 moduleApiTable.MessagesChanged:fire(moduleApiTable.MessageCount)end end end)moduleApiTable.ChatBarDisabledEvent:connect(function(p)if canChat then ChatBar:SetEnabled(not p)if p then ChatBar:ReleaseFocus()end end end)moduleApiTable.ChatWindowSizeEvent:connect(function(p)ChatWindow.GuiObject.Size=p end)moduleApiTable.ChatWindowPositionEvent:connect(function(p)ChatWindow.GuiObject.Position=p end)

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:EnterWhisperState(player)
		ChatBar:EnterWhisperState(player)
	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)
