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