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