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