var babel       = require('babel');
var path        = require('path');
var fs          = require('fs');
var async       = require('async');
var mkdirp      = require('mkdirp');

var getOptions  = require('./babel-options');
var fileName    = path.join(__dirname, '../modules/Router5.js');

function buildFactory(module, dest) {
    return function buildCommonJsModuel(done) {
        babel.transformFile(fileName, getOptions(module), function (err, result) {
            if (!err) fs.writeFile(path.join(__dirname, '..', dest), result.code, done);
            else done(err);
        });
    };
}

/**
 * Bower build
 *
 * - Flat concat all modules (router5, route-node, path-parser)
 * - Only one 'use strict', _createClass and _classCallCheck
 * - Global variables registration
 */
function buildBundle(done) {
    function transform(fileToTransform) {
        return function (done) {
            babel.transformFile(fileToTransform, {modules: 'ignore'}, done)
        }
    }

    var pathParser = path.join(__dirname, '../node_modules/route-node/node_modules/path-parser/modules/Path.js');
    var routeNode  = path.join(__dirname, '../node_modules/route-node/modules/RouteNode.js');

    async.parallel([
        fs.readFile.bind(fs, path.join(__dirname, '../LICENSE')),
        transform(pathParser),
        transform(routeNode),
        transform(fileName),
    ], function (err, results) {
        // License
        var license = results[0].toString().trim().split('\n').map(function (line) {
            return ' * ' + line;
        }).join('\n');
        license = '/**\n * @license\n' + license + '\n */';

        var pathParserSrc = results[1].code.trim();
        var routeNodeSrc = results[2].code.trim();
        var router5Src = results[3].code.trim();

        var classesSrc = pathParserSrc.replace(/'use strict';\n/, '') +
            (routeNodeSrc + router5Src)
                .replace(/'use strict';\n/, '')
                .replace(/\nvar _createClass(?:.*)\n/, '')
                .replace(/\nfunction _classCallCheck(?:.*)\n/, '');

        var globalVars = '\n' +
            'window.RouteNode = RouteNode;\n' +
            'window.Router5 = Router5;\n';

        var code = license + '\n(function () {\n\'use strict\';\n' + classesSrc + '\n' + globalVars + '\n}());\n';

        fs.writeFile(path.join(__dirname, '../dist/browser/router5.js'), code, done);
    })
}


async.parallel([
    buildFactory('common', 'dist/commonjs/router5.js'),
    buildFactory('umd',    'dist/umd/router5.js'),
    buildFactory('ignore', 'dist/test/router5.js'),
    buildBundle
], function (err) {
    if (err) console.log(err);
    process.exit(err ? 1 : 0);
});



@@ -2391,17 +2391,54 @@
   %7D)%0A%7D%0A%0A
-%0A
+mkdirp('dist/test', function () %7B%0A    
 async.pa
@@ -2446,24 +2446,28 @@
 allel(%5B%0A    
+ 
+   
 buildFactory
@@ -2502,32 +2502,36 @@
 uter5.js'),%0A    
+  
+  
 buildFactory('um
@@ -2561,24 +2561,28 @@
 5.js'),%0A    
+   
+ 
 buildFactory
@@ -2613,24 +2613,28 @@
 uter5.js'),%0A
+    
     buildBun
@@ -2637,16 +2637,20 @@
 dBundle%0A
+    
 %5D, funct
@@ -2657,24 +2657,28 @@
 ion (err) %7B%0A
+    
     if (err)
@@ -2700,16 +2700,20 @@
 r);%0A    
+ 
+   
 process.
@@ -2731,14 +2731,24 @@
 1 : 0);%0A
-%7D);
+    %7D);%0A%7D);%0A%0A
 %0A%0A%0A

create dist/test dir before build