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