/* jshint node: true */
/*jshint -W079 */
if (typeof define !== 'function') { /* istanbul ignore next */ var define = require('amdefine')(module); }
define([
	'require',
	'exports',
	'./remap',
	'./writeReport',
	'./node!istanbul/lib/store/memory',
	'./node!gulp-util',
	'./node!through2'
], function (require, exports, remap, writeReport, MemoryStore, gutil, through) {
	/* global Promise */
	var PluginError = gutil.PluginError;

	return function (opts) {
		opts = opts || {};
		return through.obj(function (file, enc, cb) {
			var p = [];
			opts.warn = function (message) {
				if (opts.fail) {
					return cb(new PluginError('remap-istanbul', message));
				}
				else {
					console.error(message);
				}
			};

			opts.sources = new MemoryStore();

			if (file.isNull()) {
				return cb(null, file);
			}

			if (file.isStream()) {
				return cb(new PluginError('remap-istanbul', 'Streaming not supported'));
			}

			var collector = remap(JSON.parse(file.contents.toString('utf8')), opts);
			
			var sources;
			if (Object.keys(opts.sources.map).length) {
				sources = opts.sources;
			}

			if (opts.reports) {
				for (var key in opts.reports) {
					p.push(writeReport(collector, key, opts.reportOpts || {}, opts.reports[key], sources));
				}
			}

			file.contents = new Buffer(JSON.stringify(collector.getFinalCoverage()));

			Promise.all(p).then(function () {
				cb(null, file);
			});
		});
	};
});

@@ -535,16 +535,37 @@
 p = %5B%5D;%0A
+%09%09%09if(!opts.warn) %7B%0A%09
 %09%09%09opts.
@@ -596,16 +596,17 @@
 ) %7B%0A%09%09%09%09
+%09
 if (opts
@@ -610,24 +610,25 @@
 pts.fail) %7B%0A
+%09
 %09%09%09%09%09return 
@@ -679,18 +679,20 @@
 ));%0A%09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%09else
@@ -699,16 +699,17 @@
  %7B%0A%09%09%09%09%09
+%09
 console.
@@ -732,15 +732,22 @@
 %09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%7D;
+%0A%09%09%09%7D
 %0A%0A%09%09
@@ -1024,19 +1024,16 @@
  opts);%0A
-%09%09%09
 %0A%09%09%09var 

Allow for warn option to be user specified