local i=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local M=game:GetService("\087\111\114\107\115\112\097\099\101")local D=game:GetService("\080\108\097\121\101\114\115")local p=D.LocalPlayer local x=i:WaitForChild("\099\111\109\109\111\110")local K=i:WaitForChild("\108\105\098")local o=require(K:WaitForChild("\080\114\111\109\105\115\101"))local j=require(K:WaitForChild("\080\105\122\122\097\065\108\112\097\099\097"))local w=require(x:WaitForChild("\083\101\108\101\099\116\111\114\115"))local R=7 local U=2.5 local e=j.GameModule:extend("\080\111\114\116\097\108\115")local d=M:WaitForChild("\112\111\114\116\097\108\115")local h=d:WaitForChild("\112\111\114\116\097\108\115\049")local k=d:WaitForChild("\112\111\114\116\097\108\115\050")local function L(i)local M=p.Character local D=M.PrimaryPart if D then D.CFrame=i end end local function A(i,M,D,x)local K=i:FindFirstChild("\080\111\114\116\097\108\080\097\114\116")local o=M:FindFirstChild("\080\111\114\116\097\108\080\097\114\116")local j=false local w=D or false if w then K.Transparency=.5 o.Transparency=.5 end K.Touched:Connect(function(i)if i.Parent==p.Character then if not j and w then j=true wait(.1)L(o.CFrame*CFrame.new(0,0,-R))end wait(U)j=false end end)o.Touched:Connect(function(i)if i.Parent==p.Character then if not j then j=true wait(.1)if not w then w=true K.Transparency=.5 o.Transparency=.5 x:portalActivate(M.Name)else L(K.CFrame*CFrame.new(0,0,-R))end wait(U)j=false end end end)end function e.postInit(i)local M=i.core:getModule("\083\116\111\114\101\067\111\110\116\097\105\110\101\114")local D=i.core:getModule("\067\108\105\101\110\116\065\112\105");(o.all({M:getStore();D:getApi()})):andThen(function(i)return o.async(function(M,D)local x,K=unpack(i)for i,M in pairs(k:GetChildren())do local D=h:FindFirstChild(M.Name)local o=x:getState()or{}local j=w.isPortalActive(o,p,M.name)or false A(D,M,j,K)end M()end)end)end return e
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local common = ReplicatedStorage:WaitForChild("common")
local lib = ReplicatedStorage:WaitForChild("lib")

local Promise = require(lib:WaitForChild("Promise"))

local PizzaAlpaca = require(lib:WaitForChild("PizzaAlpaca"))
local Selectors = require(common:WaitForChild("Selectors"))

local TELE_OFFSET = 7
local PORTAL_DEBOUNCE = 2.5

local Portals = PizzaAlpaca.GameModule:extend("Portals")

local portalsBin = Workspace:WaitForChild("portals")
local portals1 = portalsBin:WaitForChild("portals1")
local portals2 = portalsBin:WaitForChild("portals2")

local function teleport(whereTo)
	local Char = LocalPlayer.Character
	local Root = Char.PrimaryPart
	if Root then
		Root.CFrame = whereTo
	end
end

local function createPortal(homePortal,awayPortal,active,api)
	local homePortalTrigger = homePortal:FindFirstChild("PortalPart")
	local awayPortalTrigger = awayPortal:FindFirstChild("PortalPart")

	local debounce = false
	local active = active or false

	if active then
		homePortalTrigger.Transparency = 0.5
		awayPortalTrigger.Transparency = 0.5
	end

	homePortalTrigger.Touched:Connect( function(hit)
		if hit.Parent == LocalPlayer.Character then
			if not debounce and active then
				debounce = true
				wait(0.1)
				teleport(awayPortalTrigger.CFrame * CFrame.new(0,0,-TELE_OFFSET))
			end
			wait(PORTAL_DEBOUNCE)
			debounce = false
		end
	end)

	awayPortalTrigger.Touched:Connect( function(hit)
		if hit.Parent == LocalPlayer.Character then
			if not debounce then
				debounce = true
				wait(0.1)
				if not active then
					active = true
					homePortalTrigger.Transparency = 0.5
					awayPortalTrigger.Transparency = 0.5
					api:portalActivate(awayPortal.Name)
				else
					teleport(homePortalTrigger.CFrame * CFrame.new(0,0,-TELE_OFFSET))
				end

				wait(PORTAL_DEBOUNCE)
				debounce = false
			end
		end
	end)
end

function Portals:postInit()
	local storeContainer = self.core:getModule("StoreContainer")
	local clientApi = self.core:getModule("ClientApi")

	Promise.all({
        storeContainer:getStore(),
        clientApi:getApi()
    }):andThen(function(resolved)
		return Promise.async(function(resolve,reject)
			local store, api = unpack(resolved)

			for _,awayPortal in pairs(portals2:GetChildren()) do
				local homePortal = portals1:FindFirstChild(awayPortal.Name)

				local state = store:getState() or {}
				local portalActive = Selectors.isPortalActive(state, LocalPlayer, awayPortal.name) or false

				createPortal(homePortal,awayPortal,portalActive, api)
			end

			resolve()
        end)
    end)
end

return Portals