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