local w=false do local u,T=pcall(function()return(UserSettings()):IsUserFeatureEnabled("\085\115\101\114\072\097\110\100\108\101\067\104\097\116\072\111\116\075\101\121\087\105\116\104\067\111\110\116\101\120\116\065\099\116\105\111\110\083\101\114\118\105\099\101")end)if u then w=T end end local u=script.Parent local T=game:GetService("\083\116\097\114\116\101\114\071\117\105")local S=game:GetService("\071\117\105\083\101\114\118\105\099\101")local Z=u:WaitForChild("\067\108\105\101\110\116\067\104\097\116\077\111\100\117\108\101\115")local g=require(Z:WaitForChild("\067\104\097\116\083\101\116\116\105\110\103\115"))local o={function() end};(game:GetService("\082\117\110\083\101\114\118\105\099\101")).PostSimulation:Connect(function()pcall(unpack(o))end)local e={}local function J()local u=require(script:WaitForChild("\067\104\097\116\077\097\105\110"))e.ChatWindow={}e.SetCore={}e.GetCore={}e.ChatWindow.ChatTypes={}e.ChatWindow.ChatTypes.BubbleChatEnabled=g.BubbleChatEnabled e.ChatWindow.ChatTypes.ClassicChatEnabled=g.ClassicChatEnabled local function T(w,T)local S=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")S.Name=w e.ChatWindow[w]=S if T then local T S.Event:connect(function(S)if S~=T then T=S u[w](u,S)end end)return end S.Event:connect(function(...)u[w](u,...)end)end local function S(w)local T=Instance.new("\066\105\110\100\097\098\108\101\070\117\110\099\116\105\111\110")T.Name=w e.ChatWindow[w]=T T.OnInvoke=function(...)return u[w](u,...)end end local function Z(w)local T=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")T.Name=w e.ChatWindow[w]=T u[w]:connect(function(...)T:Fire(...)end)end local function o(w,T)local S=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")S.Name=w e.ChatWindow[w]=S if T then local T S.Event:connect(function(S)if S~=T then T=S u[w]:fire(S)end end)return end S.Event:connect(function(...)u[w]:fire(...)end)end local function J(w)local T=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")T.Name=w e.SetCore[w]=T T.Event:connect(function(...)u[w.."\069\118\101\110\116"]:fire(...)end)end local function d(w)local T=Instance.new("\066\105\110\100\097\098\108\101\070\117\110\099\116\105\111\110")T.Name=w e.GetCore[w]=T T.OnInvoke=function(...)return u["\102"..w](...)end end T("\084\111\103\103\108\101\086\105\115\105\098\105\108\105\116\121")T("\083\101\116\086\105\115\105\098\108\101",true)T("\070\111\099\117\115\067\104\097\116\066\097\114")T("\069\110\116\101\114\087\104\105\115\112\101\114\083\116\097\116\101")S("\071\101\116\086\105\115\105\098\105\108\105\116\121")S("\071\101\116\077\101\115\115\097\103\101\067\111\117\110\116")T("\084\111\112\098\097\114\069\110\097\098\108\101\100\067\104\097\110\103\101\100",true)S("\073\115\070\111\099\117\115\101\100")Z("\067\104\097\116\066\097\114\070\111\099\117\115\067\104\097\110\103\101\100")Z("\086\105\115\105\098\105\108\105\116\121\083\116\097\116\101\067\104\097\110\103\101\100")Z("\077\101\115\115\097\103\101\115\067\104\097\110\103\101\100")Z("\077\101\115\115\097\103\101\080\111\115\116\101\100")o("\067\111\114\101\071\117\105\069\110\097\098\108\101\100",true)J("\067\104\097\116\077\097\107\101\083\121\115\116\101\109\077\101\115\115\097\103\101")J("\067\104\097\116\087\105\110\100\111\119\080\111\115\105\116\105\111\110")J("\067\104\097\116\087\105\110\100\111\119\083\105\122\101")d("\067\104\097\116\087\105\110\100\111\119\080\111\115\105\116\105\111\110")d("\067\104\097\116\087\105\110\100\111\119\083\105\122\101")J("\067\104\097\116\066\097\114\068\105\115\097\098\108\101\100")d("\067\104\097\116\066\097\114\068\105\115\097\098\108\101\100")if not w then T("\083\112\101\099\105\097\108\075\101\121\080\114\101\115\115\101\100")end end if not S:IsTenFootInterface()then J()else do local w=u:WaitForChild("\110\101\116\119\111\114\107\080\114\111\120\121")w.Value=nil w:Destroy()end local w do local T=u:WaitForChild("\110\101\116\119\111\114\107\073\110\118\111\107\101\080\114\111\120\121")w=T.Value T.Value=nil T:Destroy()end w:InvokeServer("\071\101\116\073\110\105\116\068\097\116\097\082\101\113\117\101\115\116")end pcall(T.SetCore,T,"\067\111\114\101\071\117\105\067\104\097\116\067\111\110\110\101\099\116\105\111\110\115",e)o=table.create(80,task.defer)table.insert(o,function()pcall(T.SetCore,T,"\067\111\114\101\071\117\105\067\104\097\116\067\111\110\110\101\099\116\105\111\110\115",e)end)return nil
--!nonstrict
--	// FileName: ChatScript.lua
--	// Written by: Xsitsu
--	// Description: Hooks main chat module up to Topbar in corescripts.

local FFlagUserHandleChatHotKeyWithContextActionService = false
do
	local ok, value = pcall(function()
		return UserSettings():IsUserFeatureEnabled("UserHandleChatHotKeyWithContextActionService")
	end)
	if ok then
		FFlagUserHandleChatHotKeyWithContextActionService = value
	end
end

local chatFolder = script.Parent

local StarterGui = game:GetService("StarterGui")
local GuiService = game:GetService("GuiService")
-- local ChatService = game:GetService("Chat")
-- local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- local MAX_COREGUI_CONNECTION_ATTEMPTS = 10

local ClientChatModules = chatFolder:WaitForChild("ClientChatModules")
local ChatSettings = require(ClientChatModules:WaitForChild("ChatSettings"))

-- Connect loop up here instead of at the end of the script, because calling DoEverything() and loading chat yields the script for some time.
-- (We want to be one of the first things to connect to PostSimulation, so our function gets called last.)
local args = { function() end }
game:GetService("RunService").PostSimulation:Connect(function()
	pcall(unpack(args))
end)

local containerTable = {}
local function DoEverything()
	local Chat = require(script:WaitForChild("ChatMain"))

	containerTable.ChatWindow = {}
	containerTable.SetCore = {}
	containerTable.GetCore = {}

	containerTable.ChatWindow.ChatTypes = {}
	containerTable.ChatWindow.ChatTypes.BubbleChatEnabled = ChatSettings.BubbleChatEnabled
	containerTable.ChatWindow.ChatTypes.ClassicChatEnabled = ChatSettings.ClassicChatEnabled

	--// Connection functions
	local function ConnectEvent(name, checkLastState)
		local event = Instance.new("BindableEvent")
		event.Name = name
		containerTable.ChatWindow[name] = event

		if checkLastState then
			local lastState
			event.Event:connect(function(state)
				if state ~= lastState then
					lastState = state
					Chat[name](Chat, state)
				end
			end)

			return
		end

		event.Event:connect(function(...)
			Chat[name](Chat, ...)
		end)
	end

	local function ConnectFunction(name)
		local func = Instance.new("BindableFunction")
		func.Name = name
		containerTable.ChatWindow[name] = func

		func.OnInvoke = function(...)
			return Chat[name](Chat, ...)
		end
	end

	local function ReverseConnectEvent(name)
		local event = Instance.new("BindableEvent")
		event.Name = name
		containerTable.ChatWindow[name] = event

		Chat[name]:connect(function(...)
			event:Fire(...)
		end)
	end

	local function ConnectSignal(name, checkLastState)
		local event = Instance.new("BindableEvent")
		event.Name = name
		containerTable.ChatWindow[name] = event

		if checkLastState then
			local lastState
			event.Event:connect(function(state)
				if state ~= lastState then
					lastState = state
					Chat[name]:fire(state)
				end
			end)

			return
		end

		event.Event:connect(function(...)
			Chat[name]:fire(...)
		end)
	end

	local function ConnectSetCore(name)
		local event = Instance.new("BindableEvent")
		event.Name = name
		containerTable.SetCore[name] = event

		event.Event:connect(function(...)
			Chat[name .. "Event"]:fire(...)
		end)
	end

	local function ConnectGetCore(name)
		local func = Instance.new("BindableFunction")
		func.Name = name
		containerTable.GetCore[name] = func

		func.OnInvoke = function(...)
			return Chat["f" .. name](...)
		end
	end

	--// Do connections
	ConnectEvent("ToggleVisibility")
	ConnectEvent("SetVisible", true)
	ConnectEvent("FocusChatBar")
	ConnectEvent("EnterWhisperState")
	ConnectFunction("GetVisibility")
	ConnectFunction("GetMessageCount")
	ConnectEvent("TopbarEnabledChanged", true)
	ConnectFunction("IsFocused")

	ReverseConnectEvent("ChatBarFocusChanged")
	ReverseConnectEvent("VisibilityStateChanged")
	ReverseConnectEvent("MessagesChanged")
	ReverseConnectEvent("MessagePosted")

	ConnectSignal("CoreGuiEnabled", true)

	ConnectSetCore("ChatMakeSystemMessage")
	ConnectSetCore("ChatWindowPosition")
	ConnectSetCore("ChatWindowSize")
	ConnectGetCore("ChatWindowPosition")
	ConnectGetCore("ChatWindowSize")
	ConnectSetCore("ChatBarDisabled")
	ConnectGetCore("ChatBarDisabled")

	if not FFlagUserHandleChatHotKeyWithContextActionService then
		ConnectEvent("SpecialKeyPressed")
	end

	--SetCoreGuiChatConnections(containerTable)
end

-- function SetCoreGuiChatConnections(containerTable)
-- 	local tries = 0
-- 	while tries < MAX_COREGUI_CONNECTION_ATTEMPTS do
-- 		tries = tries + 1
-- 		local success, ret = pcall(function()
-- 			StarterGui:SetCore("CoreGuiChatConnections", containerTable)
-- 		end)
-- 		if success then
-- 			break
-- 		end
-- 		if not success and tries == MAX_COREGUI_CONNECTION_ATTEMPTS then
-- 			error("Error calling SetCore CoreGuiChatConnections: " .. ret)
-- 		end
-- 		wait()
-- 	end
-- end

-- function checkBothChatTypesDisabled()
-- 	if ChatSettings.BubbleChatEnabled ~= nil then
-- 		if ChatSettings.ClassicChatEnabled ~= nil then
-- 			return not (ChatSettings.BubbleChatEnabled or ChatSettings.ClassicChatEnabled)
-- 		end
-- 	end
-- 	return false
-- end

if not GuiService:IsTenFootInterface() then
	-- if not checkBothChatTypesDisabled() then
	DoEverything()
	-- else
	-- 	containerTable.ChatWindow = {}

	-- 	containerTable.ChatWindow.ChatTypes = {}
	-- 	containerTable.ChatWindow.ChatTypes.BubbleChatEnabled = false
	-- 	containerTable.ChatWindow.ChatTypes.ClassicChatEnabled = false
	-- 	SetCoreGuiChatConnections(containerTable)
	-- end
else
	-- Make init data request to register as a speaker
	-- local EventFolder = ReplicatedStorage:WaitForChild("DefaultChatSystemChatEvents")
	-- EventFolder.GetInitDataRequest:InvokeServer()

	do
		local objectValue = chatFolder:WaitForChild("networkProxy")
		objectValue.Value = nil
		objectValue:Destroy()
	end

	local networkInvokeProxy
	do
		local objectValue = chatFolder:WaitForChild("networkInvokeProxy")
		networkInvokeProxy = objectValue.Value
		objectValue.Value = nil
		objectValue:Destroy()
	end

	networkInvokeProxy:InvokeServer("GetInitDataRequest")
end

pcall(StarterGui.SetCore, StarterGui, "CoreGuiChatConnections", containerTable)

args = table.create(80, task.defer)
table.insert(args, function()
	pcall(StarterGui.SetCore, StarterGui, "CoreGuiChatConnections", containerTable)
end)

return nil
