var path = require('path');

var Builder = require('mip-builder');
var reporter = require('./build-reporter');

var amdProcessor = require('mip-processor-amd');
var AMDCompiler = amdProcessor.Compiler;
var AMDPacker = amdProcessor.Packer;
var LessProcessor = require('mip-processor-less');
var JSCompressor = require('mip-processor-jscompress');

var requireConfig = {
    baseUrl: path.resolve(__dirname, '..', 'src'),
    paths: {
        'zepto' : '../deps/zepto',
        'naboo' : '../deps/naboo',
        'spark' : '../deps/spark',
        'fetch-jsonp' : '../deps/fetch-jsonp'
    }
    
    // ,
    // packages: [
    //     {
    //         name: 'mip',
    //         main: 'mip',
    //         location: '../src'
    //     }
    // ]
};

// init processors
var amdCompiler = new AMDCompiler({
    config: requireConfig,
    files: [
        'src/**/*.js',
        'deps/naboo.js',
        'deps/zepto.js',
        'deps/spark.js',
        'deps/fetch-jsonp.js'
    ]
});

var testAmdCompiler = new AMDCompiler({
    config: {
        baseUrl: path.resolve(__dirname, '..', 'src'),
        paths: {
            'test': '../test',
            'zepto' : '../deps/zepto',
            'naboo' : '../deps/naboo',
            'spark' : '../deps/spark',
            'fetch-jsonp' : '../deps/fetch-jsonp'
        }
    },
    files: [
        'src/**/*.js',
        'deps/naboo.js',
        'deps/zepto.js',
        'deps/spark.js',
        'deps/fetch-jsonp.js',
        'test/**/*.js'
    ]
});


var amdPacker = new AMDPacker({
    config: requireConfig,
    modules: ['mip']
});

var lessProcessor = new LessProcessor({
    files: ['src/less/mip.less']
});

var jsCompressor = new JSCompressor();


var Combiner = require('./file-combiner');
var mainCombiner = new Combiner({
    sep: '\n\n',
    files: {
        'src/mip.js': [
            'src/prefix.js',
            'deps/promise.js',
            'deps/fetch.js',
            'deps/document-register-element.max.js',
            'deps/esl.js',
            'src/mip.js'
        ]
    }
});

var bootstrapper = {
    name: 'AddBootStrap',
    process: function (builder) {
        var file = builder.getFile('src/mip.js');
        var configFileContent = require('fs').readFileSync(
            path.resolve(__dirname, 'mip-bootstrap.js'), 'UTF-8'
        );

        file.setData(file.getData() + '\n\n' + configFileContent);
        return Promise.resolve();
    }
};

var outputFilter = {
    name: 'OutputFilter',
    files: ['**/*'],
    processFile: function (file) {
        if (file.outputPath !== 'src/mip.js'
            && file.outputPath !== 'deps/jquery.js'
            && file.outputPath !== 'src/less/mip.css'
        ) {
            file.outputPath = null;
        }
    }
};


var pathMapper = {
    name: 'PathMapper',
    files: ['**/*'],
    processFile: function (file) {
        if (file.outputPath && !/jquery.js/.test(file.outputPath)) {
            file.outputPath = file.outputPath.slice(file.outputPath.lastIndexOf('/') + 1);
        }
    }
};

// Processors
var processors = {
    'build': [
        amdCompiler,
        amdPacker,
        lessProcessor,
        mainCombiner,
        bootstrapper,
        jsCompressor,
        outputFilter,
        pathMapper
    ],
    'dev': [
        amdCompiler,
        amdPacker,
        lessProcessor,
        mainCombiner,
        bootstrapper,
        // jsCompressor,
        outputFilter,
        pathMapper
    ],
    'test': [
        testAmdCompiler,
        amdPacker,
        // lessProcessor,
        mainCombiner,
        bootstrapper
        // jsCompressor,
        // outputFilter,
        // pathMapper
    ]
};

var args = process.argv.slice(2);
// build, dev, test
var runType = args && args[0];
if (!runType || !(runType in processors)) {
    runType = 'build';
}

var builder = new Builder({
    dir: path.resolve(__dirname, '..'),
    processors: processors[runType],

    files: [
        '!**/.*',
        '!package.json',
        '!node_modules/**/*',
        '!.git/**/*',
        '!Makefile',
        '!karma.conf.js',
        '!.editorconfig',
        '!.gitignore',
        '!README.md',
        '!dist/**/*',
        '!examples/**/*',
        runType !== 'test' ? '!test/**/*' : '',
        '!extensions/**/*',
        '!tools/**/*'
    ],

    outputDir: path.resolve(__dirname, '..', 'dist'),
});

builder.setReporter(reporter);

// do build
builder.build()
    .catch(function (e) {
        console.log(e);
    });

@@ -336,24 +336,429 @@
 ompress');%0A%0A
+%0Avar runType = process.argv%5B2%5D;%0A// build, dev, test%0Aif (!runType %7C%7C (runType !== 'build' && runType !== 'dev' && runType !== 'test')) %7B%0A    runType = 'build';%0A%7D%0A%0A%0Avar packageMessage = require('../package.json');%0A%0Afunction addVersion(filePath) %7B%0A    return filePath.replace(/(mip)(%5C.)(js%7Ccss)$/, function (match, name, dot, ext) %7B%0A        return name + '-' + packageMessage.version + dot + ext;%0A    %7D);%0A%7D%0A%0A
 var requireC
@@ -3133,16 +3133,117 @@
 = null;%0A
+        %7D else if (runType === 'build') %7B%0A            file.outputPath = addVersion(file.outputPath);%0A
         
@@ -4165,163 +4165,8 @@
 %7D;%0A%0A
-var args = process.argv.slice(2);%0A// build, dev, test%0Avar runType = args && args%5B0%5D;%0Aif (!runType %7C%7C !(runType in processors)) %7B%0A    runType = 'build';%0A%7D%0A%0A
 var 

Add file version