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