const funnel = require('broccoli-funnel');
const concat = require('broccoli-concat');
const mergeTrees = require('broccoli-merge-trees');
const esTranspiler = require('broccoli-babel-transpiler');
const env = process.env.ENV;
const pkg = require('./package.json');

const transpile = function (tree) {
  return esTranspiler(tree, {
    stage: 0,
    moduleIds: true,
    modules: 'amd',
    loose: ['es6.classes', 'es6.modules'],

    // Transforms /index.js files to use their containing directory name
    getModuleId: function (name) {
      name = pkg.name + '/' + name;
      return name.replace(/\/index$/, '');
    },

    // Fix relative imports inside /index's
    resolveModuleSource: function (source, filename) {
      var match = filename.match(/(.+)\/index\.\S+$/i);

      // is this an import inside an /index file?
      if (match) {
        var path = match[1];
        return source
          .replace(/^\.\//, path + '/')
          .replace(/^\.\.\//, '');
      } else {
        return source;
      }
    }
  });
};

const js = transpile('lib');

const main = concat(js, {
  inputFiles: [
    '**/*.js'
  ],
  outputFile: '/' + pkg.name + '.js'
});

const trees = [main];

if (env === 'test') {
  const test = concat(transpile('tests'), {
    inputFiles: [
      '**/*-test.js'
    ],
    outputFile: '/' + pkg.name + '-tests.js'
  });
  const qunit = funnel('bower_components/qunit/qunit', {
    destDir: 'assets'
  });
  const loader = mergeTrees([
    funnel('bower_components/loader.js', {
      destDir: 'assets',
      include: ['loader.js']
    }),
    funnel('tests', {
      destDir: 'assets',
      include: ['test-loader.js']
    })
  ]);
  const index = funnel('tests', {
    include: ['index.html']
  });
  trees.push(test, qunit, index, loader);
}

module.exports = mergeTrees(trees);

@@ -1,13 +1,49 @@
-const
+/* global require, module, process */%0Avar
  funnel 
@@ -72,21 +72,19 @@
 nnel');%0A
-const
+var
  concat 
@@ -113,21 +113,19 @@
 ncat');%0A
-const
+var
  mergeTr
@@ -163,21 +163,19 @@
 rees');%0A
-const
+var
  esTrans
@@ -220,21 +220,19 @@
 iler');%0A
-const
+var
  env = p
@@ -247,21 +247,19 @@
 nv.ENV;%0A
-const
+var
  pkg = r
@@ -285,21 +285,19 @@
 son');%0A%0A
-const
+var
  transpi
@@ -1056,21 +1056,19 @@
 %7D);%0A%7D;%0A%0A
-const
+var
  js = tr
@@ -1084,21 +1084,19 @@
 lib');%0A%0A
-const
+var
  main = 
@@ -1185,21 +1185,19 @@
 s'%0A%7D);%0A%0A
-const
+var
  trees =
@@ -1230,21 +1230,19 @@
 t') %7B%0A  
-const
+var
  test = 
@@ -1369,21 +1369,19 @@
   %7D);%0A  
-const
+var
  qunit =
@@ -1452,21 +1452,19 @@
   %7D);%0A  
-const
+var
  loader 
@@ -1683,13 +1683,11 @@
 ;%0A  
-const
+var
  ind

add globals and const -> var