var mdeps = require("module-deps"),
through   = require("through"),
async     = require("async"),
path      = require("path"),
fs        = require("fs"),
uglify = require("uglify-js"),
builtins = require("builtins");


function bundle (input, complete) {

  async.waterfall([

    /**
     */

    function resolveDeps (next) {


      var deps = [];


      var md = mdeps({
        xtransform: function (tr, file) {

          var buffer = [];
          console.log(tr);


          return through(write, end);

          var self = this;


          function write (chunk) {
            buffer.push(chunk.toString());
            // console.log(chunk.toString(""))
            console.log(tr);
          }

          function end() {
            var content = buffer.join("");
            // console.log(content);


            /*var source = uglify.parse(content, {
              strict: false
            });
            source.figure_out_scope();
            source.compute_char_frequency();
            source.mangle_names();

            // TODO - production
            content = source.print_to_string({
              ascii_only: true,
              quote_keys: true
            });*/

            this.queue(content);
            this.queue(null);
          }

        },
        filter: function (id) {
          return !~builtins.indexOf(id);
        }
      });
      md.pipe(through(function (chunk) {
        deps.push(chunk);
      }, function () {
        next(null, deps);
      }));

      md.end({ file: input })

    },

    function bundleScript (deps, next) {
      var buffer = [loader.toString()];


       var dbuffer = [];


      for (var i = deps.length; i--;) {
        var dep = deps[i];


        var req = "function (require, module, exports) { \n" + dep.source + "}"


        dbuffer.push(JSON.stringify(dep.id) + ": ["+req+", "+!!dep.entry+", "+JSON.stringify(dep.deps)+"]")
      }


      buffer = ["(" + loader.toString() + ").call(null, {"+dbuffer.join(",")+"})"].toString();

      next(null, buffer);
    }
  ], complete)
}


var loader = function (deps) {

  var darr = [];

  for (var key in deps) {
    darr.push(deps[key]);
  }

  function initModule (dep) {
    if (dep.module) return dep.module.exports;
    dep.module = { exports: {} };

    dep[0](function (path) {
      var rdep = deps[dep[2][path]];
      var exports = rdep ? rdep.module ? rdep.module.exports : initModule(rdep) : global.require(path);
      return exports;
    }, dep.module, dep.module.exports);

    return dep.module.exports;
  }

  darr.forEach(initModule);

  return darr.filter(function (dep) {
    return dep[1];
  }).map(function (dep) {
    return dep.module || {};
  }).pop();
}

module.exports = bundle;

@@ -1621,25 +1621,24 @@
 )%5D;%0A%0A%0A      
- 
 var dbuffer 
@@ -1639,24 +1639,24 @@
 uffer = %5B%5D;%0A
+
 %0A%0A      for 
@@ -1775,17 +1775,259 @@
 ) %7B %5Cn%22 
-+
+%0A        if (dep.id.toLowerCase().indexOf(%22.json%22, dep.id.length - 5) !== -1) %7B%0A          //this is the result of require(%22someFile.json%22), so we need to export it from the function%0A          req += %22module.exports = %22%0A        %7D%0A        req +=
  dep.sou
@@ -2035,16 +2035,17 @@
 ce + %22%7D%22
+;
 %0A%0A%0A     

Add JSON support for require statments.