local g=require(script.Parent.TestEnum)local R={}R.__index=R function R.new()local g={_stack={}}return setmetatable(g,R)end function R.getBeforeEachHooks(R)local c=g.NodeType.BeforeEach local P={}for g,R in ipairs(R._stack)do for g,R in ipairs(R[c])do table.insert(P,R)end end return P end function R.getAfterEachHooks(R)local c=g.NodeType.AfterEach local P={}for g,R in ipairs(R._stack)do for g,R in ipairs(R[c])do table.insert(P,1,R)end end return P end function R.popHooks(g)table.remove(g._stack,#g._stack)end function R.pushHooksFrom(c,R)assert(R~=nil)table.insert(c._stack,{[g.NodeType.BeforeAll]=c:_getHooksOfType(R.children,g.NodeType.BeforeAll),[g.NodeType.AfterAll]=c:_getHooksOfType(R.children,g.NodeType.AfterAll),[g.NodeType.BeforeEach]=c:_getHooksOfType(R.children,g.NodeType.BeforeEach);[g.NodeType.AfterEach]=c:_getHooksOfType(R.children,g.NodeType.AfterEach)})end function R.getBeforeAllHooks(R)return R._stack[#R._stack][g.NodeType.BeforeAll]end function R.getAfterAllHooks(R)return R._stack[#R._stack][g.NodeType.AfterAll]end function R._getHooksOfType(c,g,R)local P={}for g,c in ipairs(g)do if c.type==R then table.insert(P,c.callback)end end return P end return R
local TestEnum = require(script.Parent.TestEnum)

local LifecycleHooks = {}
LifecycleHooks.__index = LifecycleHooks

function LifecycleHooks.new()
	local self = {
		_stack = {},
	}
	return setmetatable(self, LifecycleHooks)
end

--[[
	Returns an array of `beforeEach` hooks in FIFO order
]]
function LifecycleHooks:getBeforeEachHooks()
	local key = TestEnum.NodeType.BeforeEach
	local hooks = {}

	for _, level in ipairs(self._stack) do
		for _, hook in ipairs(level[key]) do
			table.insert(hooks, hook)
		end
	end

	return hooks
end

--[[
	Returns an array of `afterEach` hooks in FILO order
]]
function LifecycleHooks:getAfterEachHooks()
	local key = TestEnum.NodeType.AfterEach
	local hooks = {}

	for _, level in ipairs(self._stack) do
		for _, hook in ipairs(level[key]) do
			table.insert(hooks, 1, hook)
		end
	end

	return hooks
end

--[[
	Pushes uncalled beforeAll and afterAll hooks back up the stack
]]
function LifecycleHooks:popHooks()
	table.remove(self._stack, #self._stack)
end

function LifecycleHooks:pushHooksFrom(planNode)
	assert(planNode ~= nil)

	table.insert(self._stack, {
		[TestEnum.NodeType.BeforeAll] = self:_getHooksOfType(planNode.children, TestEnum.NodeType.BeforeAll),
		[TestEnum.NodeType.AfterAll] = self:_getHooksOfType(planNode.children, TestEnum.NodeType.AfterAll),
		[TestEnum.NodeType.BeforeEach] = self:_getHooksOfType(planNode.children, TestEnum.NodeType.BeforeEach),
		[TestEnum.NodeType.AfterEach] = self:_getHooksOfType(planNode.children, TestEnum.NodeType.AfterEach),
	})
end

--[[
	Get the beforeAll hooks from the current level.
]]
function LifecycleHooks:getBeforeAllHooks()
	return self._stack[#self._stack][TestEnum.NodeType.BeforeAll]
end

--[[
	Get the afterAll hooks from the current level.
]]
function LifecycleHooks:getAfterAllHooks()
	return self._stack[#self._stack][TestEnum.NodeType.AfterAll]
end

function LifecycleHooks:_getHooksOfType(nodes, key)
	local hooks = {}

	for _, node in ipairs(nodes) do
		if node.type == key then
			table.insert(hooks, node.callback)
		end
	end

	return hooks
end

return LifecycleHooks
