--[=[
	Starts the service bag and all services
]=]

function ServiceBag:Start()
	assert(self._serviceTypesToStart, "Already started")
	assert(not self._initializing, "Still initializing")

	while next(self._serviceTypesToStart) do
		local serviceType = table.remove(self._serviceTypesToStart)
		local service = assert(self._services[serviceType], "No service")
		local serviceName = self:_getServiceName(serviceType)

		if service.Start then
			local current
			task.spawn(function()
				debug.setmemorycategory(serviceName)
				current = coroutine.running()
				service:Start()
			end)

			local isDead = coroutine.status(current) == "dead"
			if not isDead then
				error(("Starting service %q yielded"):format(serviceName))
			end
		end
	end

	self._serviceTypesToStart = nil
end

function ServiceBag:_getServiceName(serviceType)
	local serviceName
	pcall(function()
		serviceName = serviceType.ServiceName
	end)
	if type(serviceName) == "string" then
		return serviceName
	end

	return tostring(serviceType)
end
