local Z=require(script.Parent.TestEnum)local b={}b.__index=b function b.new()local Z={_stack={}}return setmetatable(Z,b)end function b.getBeforeEachHooks(b)local l=Z.NodeType.BeforeEach local m={}for Z,b in ipairs(b._stack)do for Z,b in ipairs(b[l])do table.insert(m,b)end end return m end function b.getAfterEachHooks(b)local l=Z.NodeType.AfterEach local m={}for Z,b in ipairs(b._stack)do for Z,b in ipairs(b[l])do table.insert(m,1,b)end end return m end function b.popHooks(Z)table.remove(Z._stack,#Z._stack)end function b.pushHooksFrom(l,b)assert(b~=nil)table.insert(l._stack,{[Z.NodeType.BeforeAll]=l:_getHooksOfType(b.children,Z.NodeType.BeforeAll),[Z.NodeType.AfterAll]=l:_getHooksOfType(b.children,Z.NodeType.AfterAll),[Z.NodeType.BeforeEach]=l:_getHooksOfType(b.children,Z.NodeType.BeforeEach),[Z.NodeType.AfterEach]=l:_getHooksOfType(b.children,Z.NodeType.AfterEach)})end function b.getBeforeAllHooks(b)return b._stack[#b._stack][Z.NodeType.BeforeAll]end function b.getAfterAllHooks(b)return b._stack[#b._stack][Z.NodeType.AfterAll]end function b._getHooksOfType(l,Z,b)local m={}for Z,l in ipairs(Z)do if l.type==b then table.insert(m,l.callback)end end return m end return b
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
