--[[*
 * This class is a perf optimization for sorting the list of currently
 * running tests. It tries to keep tests in the same positions without
 * shifting the whole list.
 ]]

type CurrentTestList = {
	add: (self: CurrentTestList, testPath: Config_Path, config: Config_ProjectConfig) -> (),
	delete: (self: CurrentTestList, testPath: Config_Path) -> (),
	get: (self: CurrentTestList) -> Array<{
		testPath: Config_Path,
		config: Config_ProjectConfig,
	}>,
}

local CurrentTestList = {}
CurrentTestList.__index = CurrentTestList

function CurrentTestList.new(): CurrentTestList
	local self = setmetatable({}, CurrentTestList)
	self._array = {}
	return (self :: any) :: CurrentTestList
end

function CurrentTestList:add(testPath: Config_Path, config: Config_ProjectConfig): ()
	local index = Array.indexOf(self._array, nil)
	local record = { config = config, testPath = testPath }
	if index ~= -1 then
		self._array[index] = record
	else
		table.insert(self._array, record)
	end
end

function CurrentTestList:delete(testPath: Config_Path): ()
	local record = Array.find(self._array, function(record)
		return record ~= nil and record.testPath == testPath
	end)
	self._array[Array.indexOf(self._array, Boolean.toJSBoolean(record) and record or nil)] = nil
end

function CurrentTestList:get(): Array<{
	testPath: Config_Path,
	config: Config_ProjectConfig,
}>
	return self._array
end

type Cache = {
	content: string,
	clear: string,
}
