'use strict';

/**
 * gulp-svg-sprite is a Gulp plugin for creating SVG sprites
 *
 * @see https://github.com/jkphl/gulp-svg-sprite
 *
 * @author Joschi Kuphal <joschi@kuphal.net> (https://github.com/jkphl)
 * @copyright © 2015 Joschi Kuphal
 * @license MIT https://raw.github.com/jkphl/gulp-svg-sprite/master/LICENSE.txt
 */

var through2 						= require('through2'),
	gutil							= require('gulp-util'),
	SVGSpriter						= require('svg-sprite'),
	PluginError						= gutil.PluginError,
	
	PLUGIN_NAME						= 'gulp-svg-sprite';

/**
 * Plugin level function
 * 
 * @param {Object} config			SVGSpriter main configuration
 */
function gulpSVGSprite(config) {

	// Extend plugin error
	function extendError(pError, error) {
		if (error && (typeof error === 'object')) {
			['name', 'errno'].forEach(function(property) {
				if (property in error) {
					this[property]	= error[property];
				}
			}, pError);
		}

		return pError;
	}
	
	// Instanciate spriter instance
	var spriter						= new SVGSpriter(config);
	
	// Intercept error log and convert to plugin errors
	spriter.config.log.error		= function(message, error) {
		this.emit('error', extendError(new PluginError(PLUGIN_NAME, message), error));
	};
	
	return through2.obj(function (file, enc, cb) {
		var error					= null;
		try {
			spriter.add(file);
		} catch(e) {
			error					= (!e.plugin || (e.plugin !== PLUGIN_NAME)) ? extendError(new PluginError(PLUGIN_NAME, e.message), e) : e;
		}
		return cb(error);
		
	}, function(cb) {
		var stream 					= this;
		spriter.compile(function(error, result /*, data*/){
			if (error) {
				stream.emit('error', new PluginError(PLUGIN_NAME, error));
			} else {
				for (var mode in result) {
					for (var resource in result[mode]) {
						stream.push(result[mode][resource]);
					}
				}
			}
			cb();
		});
	});
}

module.exports			= gulpSVGSprite;
@@ -480,18 +480,17 @@
 nError,%0A
-%09
 %0A
+
 %09PLUGIN_
@@ -552,17 +552,16 @@
 ction%0A *
- 
 %0A * @par
@@ -922,18 +922,17 @@
 ror;%0A%09%7D%0A
-%09
 %0A
+
 %09// Inst
@@ -1000,18 +1000,17 @@
 onfig);%0A
-%09
 %0A
+
 %09// Inte
@@ -1195,18 +1195,17 @@
 ));%0A%09%7D;%0A
-%09
 %0A
+
 %09return 
@@ -1461,11 +1461,9 @@
 r);%0A
-%09%09
 %0A
+
 %09%7D, 
@@ -1547,19 +1547,14 @@
 sult
- /*
 , data
-*/
 )%7B%0A%09
@@ -1643,16 +1643,55 @@
  else %7B%0A
+%09%09%09%09if (data.css.shapes.length %3E 0) %7B%0A%09
 %09%09%09%09for 
@@ -1709,24 +1709,25 @@
 n result) %7B%0A
+%09
 %09%09%09%09%09for (va
@@ -1758,24 +1758,25 @@
 e%5D) %7B%0A%09%09%09%09%09%09
+%09
 stream.push(
@@ -1800,16 +1800,24 @@
 urce%5D);%0A
+%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%7D%0A%09
@@ -1849,16 +1849,16 @@
 %09%7D);%0A%7D%0A%0A
-
 module.e
@@ -1882,8 +1882,9 @@
 GSprite;
+%0A

Resolve issue #14 empty SVG