-- ROBLOX FIXME Luau: should infer this function a Plugin | nil, with no warnings

local function findPlugin(plugins: Plugins, val: any): Plugin | nil
	for _, p in ipairs(plugins) do
		local ok, ret = pcall(p.test, val)
		if not ok then
			error(PrettyFormatPluginError(ret))
		elseif ret then
			return p
		end
	end

	return nil
end

function printer(
	val: any,
	config: Config,
	indentation: string,
	depth: number,
	refs: Refs,
	hasCalledToJSON: boolean?
): string
	local plugin_ = findPlugin(config.plugins, val)
	if plugin_ ~= nil then
		return printPlugin(plugin_, val, config, indentation, depth, refs)
	end

	local basicResult = printBasicValue(val, config.printFunctionName, config.escapeRegex, config.escapeString)
	if basicResult ~= nil then
		return basicResult
	end

	return printComplexValue(val, config, indentation, depth, refs, hasCalledToJSON)
end
