/*
 * grunt-labeled-merge
 * https://github.com/allan/grunt-labeled-merge
 *
 * Copyright (c) 2015 atask
 * Licensed under the MIT license.
 */

'use strict';

var mergeMeta = require('./lib/merge-meta'),
    debug = require('debug')('labeled-merge'),
    eol = require('os').EOL;

module.exports = function(grunt) {
    // Please see the Grunt documentation for more information regarding task
    // creation: http://gruntjs.com/creating-tasks
    
    grunt.registerMultiTask('labeled_merge', 'Merges folders without overwriting files.', function() {
        var done = this.async(),
            self = this,
            currentPromise;

        // Iterate over all specified file groups.
        this.files.forEach(function(mapping) {
            mapping.src.forEach(function(srcFolder) {
                var srcPromise = mergeMeta.merge(mapping.dest, srcFolder, self.options);
                debug(
                    'Processing: ' + eol +
                    '\t' + 'srcFolder :[' + srcFolder + ']' + eol +
                    '\t' + 'mapping.dest :[' + mapping.dest + ']'
                );
                if (currentPromise) {
                    currentPromise.then(function chainNextSrc() {
                        return srcPromise;
                    });
                }
                currentPromise = srcPromise;
            });
        });

        currentPromise
            .then(done)
            .catch(grunt.fail);

    });
};

@@ -621,75 +621,83 @@
     
-currentPromise;%0A%0A        // Iterate over all specified file groups.
+flatMappings = %5B%5D,%0A            currentPromise;%0A%0A        // flatten mappings
 %0A   
@@ -814,85 +814,259 @@
     
-var srcPromise = mergeMeta.merge(mapping.dest, srcFolder, self.options);%0A    
+flatMappings.push(%7B%0A                    src: srcFolder,%0A                    dest: mapping.dest%0A                %7D);%0A            %7D);%0A        %7D);%0A%0A        // Iterate over all available mappings, in order.%0A        flatMappings.forEach(function(mapping) %7B%0A
     
@@ -1080,20 +1080,16 @@
  debug(%0A
-    
         
@@ -1135,28 +1135,24 @@
         
-    
 '%5Ct' + '
 srcFolde
@@ -1147,34 +1147,37 @@
  + '
-srcFolder :%5B' + srcFolder 
+mapping.src :%5B' + mapping.src
 + '%5D
@@ -1178,36 +1178,32 @@
 rc+ '%5D' + eol +%0A
-    
                 
@@ -1264,72 +1264,278 @@
     
+);%0A
     
-);%0A                if (currentPromise) %7B%0A                   
+        if (!currentPromise) %7B%0A                // start merges chain%0A                currentPromise = mergeMeta.merge(mapping.dest, mapping.src, self.options);%0A            %7D else %7B%0A                // append next merge to the chain%0A                currentPromise =
  cur
@@ -1573,11 +1573,15 @@
 Next
-Src
+Mapping
 () %7B
@@ -1605,118 +1605,149 @@
     
-    return srcPromise;%0A                    %7D);%0A                %7D%0A                currentPromise = srcPromise;%0A
+var dest = mapping.dest,%0A                        src = mapping.src;%0A                    return mergeMeta.merge(dest, src, self.options);%0A    
     
@@ -1750,32 +1750,46 @@
             %7D);%0A
+            %7D%0A
         %7D);%0A%0A   

fix promise chaining