'use strict';

var isSvg = require('is-svg');
var SVGO = require('svgo');
var through = require('through2');

module.exports = function (opts) {
	opts = opts || {};

	return through.ctor({objectMode: true}, function (file, enc, cb) {
		if (file.isNull()) {
			cb(null, file);
			return;
		}

		if (file.isStream()) {
			cb(new Error('Streaming is not supported'));
			return;
		}

		if (!isSvg(file.contents)) {
			cb(null, file);
			return;
		}

		try {
			var svgo = new SVGO({
				multipass: opts.multipass || false,
				plugins: opts.plugins || []
			});

			svgo.optimize(file.contents.toString('utf8'), function (res) {
				if (res.data && res.data.length) {
					res.data = res.data.replace(/&(?!amp;)/g, '&amp;');
				}

				res.data = new Buffer(res.data);

				if (res.data.length < file.contents.length) {
					file.contents = res.data;
				}

				cb(null, file);
			});
		} catch (err) {
			cb(err);
		}
	});
};

@@ -615,24 +615,73 @@
 ion (res) %7B%0A
+%09%09%09%09if (!res.data) %7B%0A%09%09%09%09%09cb(null, file);%0A%09%09%09%09%7D%0A%0A
 %09%09%09%09if (res.

Return the original file if SVGO fails