/*
 * grunt-kmc
 * https://github.com/daxingplay/grunt-kmc
 *
 * Copyright (c) 2013 daxingplay
 * Licensed under the MIT license.
 */

'use strict';

var kmc = require('kmc'),
	fs = require('fs'),
    path = require('path'),
    os = require('os');

module.exports = function (grunt) {

    // Please see the Grunt documentation for more information regarding task
    // creation: http://gruntjs.com/creating-tasks

    grunt.registerMultiTask('kmc', 'Build KISSY modules.', function () {
        // Merge task-specific and/or target-specific options with these defaults.
        var options = this.options(),
            depExt = options.depExt,
            depFilePath = options.depFilePath,
            comboOnly = options.comboOnly,
            depFileCharset = options.depFileCharset || options.charset,
            fixModuleName = options.fixModuleName !== false,
            fixModuleNameOnDest = options.fixModuleNameOnDest === true,
            traverse = options.traverse;

        kmc.config(options);

        // Iterate over all specified file groups.
        var results = [];
        this.files.forEach(function (f) {
            f.src.forEach(function(src){
                var depFile = '',
                    inputSrc = path.resolve(src),
                    outputSrc = path.resolve(String(f.dest));
                if(depExt || depFilePath){
                    depExt = depExt || '.dep';
                    var outputIsDir = grunt.file.isDir(outputSrc) || !/\.js$/.test(outputSrc);
                    if(depFilePath){
                        if(grunt.file.isDir(depFilePath) || !/.js$/.test(depFilePath)){
                            depFile = path.resolve(depFilePath, path.basename(outputIsDir ? path.basename(inputSrc, '.js') :outputSrc) + depExt + '.js');
                        }else{
                            depFile = depFilePath;
                        }
                    }else{
                        var dir = outputIsDir ? outputSrc : path.dirname(outputSrc);
                        depFile = path.resolve(dir, path.basename(inputSrc, '.js') + depExt + '.js');
                    }
                }
                var result = '';
                if(comboOnly === true){
                    var outputDir = grunt.file.isDir(outputSrc) ? outputSrc : path.dirname(outputSrc);
					var r = kmc.combo(inputSrc, options.comboMap !== true ? depFile : undefined, depFileCharset, fixModuleName, true, fixModuleNameOnDest ? outputDir : null,comboOnly === true);
					results.push(r);
                    options.comboMap !== true && grunt.log.ok('Dep File "' + depFile + '" created.');
                }else{
                    result = kmc.build(inputSrc, outputSrc, null, depFile, traverse);
                    results.push(result);
                    grunt.log.ok('File "' + result.files[0].outputFile + '" created.');
                }

            });
        });

        if(options.comboMap === true){
            var content = [];
            var writed = [];
            results.forEach(function(result){
                if(result.modules){
                    for(var modName in result.modules){
                        if(writed.indexOf(modName) === -1){
                            var mod = result.modules[modName];
                            var requires = [];
                            if(mod && mod.dependencies && mod.dependencies.length){
                                mod.dependencies.forEach(function(subMod){
                                    requires.push("'" + subMod.name + "'");
                                });
                                content.push("'" + modName + "': { requires: [" + requires.join(', ') + "]}");
                            }
                            writed.push(modName);
                        }
                    }
                }
            });
            var r = '/*generated by KMC*/' + os.EOL;
            if(content.length){
                r += "KISSY.config('modules', {" + os.EOL + " " + content.join("," + os.EOL + " ") + " " + os.EOL + "});";
            }
            grunt.file.write(options.depFilePath, r, {'encoding':'utf8'});
            grunt.log.ok('combined dependency file ' + options.depFilePath + ' created.');
        }
    });

};

@@ -884,16 +884,15 @@
 ame 
-!== fals
+=== tru
 e,%0D%0A

fix module name defaults to false