/* global require, module */
var filterTemplates = require('broccoli-template');
var uglifyJavaScript = require('broccoli-uglify-js');
var compileES6 = require('broccoli-es6-concatenator');
// var compileSass = require('broccoli-sass');      // Uncomment for Sass support
var pickFiles = require('broccoli-static-compiler');
var env = require('broccoli-env').getEnv();

function preprocess (tree) {
  return filterTemplates(tree, {
    extensions: [
      'hbs',
      'handlebars'
    ],
    compileFunction: 'Ember.Handlebars.compile'
  });
}

module.exports = function (broccoli) {
  var app = broccoli.makeTree('app');
  var tests = broccoli.makeTree('tests');
  var publicFiles = broccoli.makeTree('public');
  var vendor = broccoli.makeTree('vendor');
  var styles = broccoli.makeTree('styles');

  app = pickFiles(app, {
    srcDir: '/',
    destDir: '<%= modulePrefix %>'
  });

  app = preprocess(app);

  styles = pickFiles(styles, {
    srcDir: '/',
    destDir: '<%= modulePrefix %>/styles' 
  });

  styles = preprocess(styles);

  tests = pickFiles(tests, {
    srcDir: '/',
    destDir: '<%= modulePrefix %>/tests'
  });

  tests = preprocess(tests);

  var sourceTrees = [
    app,
    // styles,          // Uncomment for Sass support
    vendor
  ];

  if (env !== 'production') {
    //sourceTrees.push(tests);
  }

  sourceTrees = sourceTrees.concat(broccoli.bowerTrees());

  var appAndDependencies = new broccoli.MergedTree(sourceTrees);

  var applicationJs = compileES6(appAndDependencies, {
    loaderFile: 'loader.js',
    ignoredModules: [
      'ember/resolver'
    ],
    inputFiles: [
      '<%= modulePrefix %>/**/*.js'
    ],
    legacyFilesToAppend: [
      'jquery.js',
      'handlebars.js',
      'ember.js',
      'ember-data.js',
      'ember-resolver.js'
    ],

    wrapInEval: env !== 'production',
    outputFile: '/assets/app.js'
  });

  // Uncomment for Sass support
  // var appCss = compileSass(sourceTrees, '<%= modulePrefix %>/styles/app.scss', '/assets/app.css');

  if (env === 'production') {
    applicationJs = uglifyJavaScript(applicationJs, {
      mangle: false,
      compress: false
    });
  }

  return [
    applicationJs,
    publicFiles,
    // appCss,          // Uncomment for Sass support
    styles
  ];
};

@@ -790,24 +790,68 @@
 e('styles');
+%0A  var config = broccoli.makeTree('config');
 %0A%0A  app = pi
@@ -1039,17 +1039,16 @@
 /styles'
- 
 %0A  %7D);%0A%0A
@@ -1199,24 +1199,183 @@
 ss(tests);%0A%0A
+  config = pickFiles(config, %7B%0A    srcDir: '/',%0A    files: %5B'environment.js', 'environments/' + env + '.js'%5D,%0A    destDir: '%3C%25= modulePrefix %25%3E/config'%0A  %7D);%0A%0A
   var source
@@ -1443,24 +1443,36 @@
 ass support%0A
+    config,%0A
     vendor%0A 
@@ -1861,24 +1861,24 @@
 .js'%0A    %5D,%0A
-
     legacyFi
@@ -1888,24 +1888,139 @@
 ToAppend: %5B%0A
+      '%3C%25= modulePrefix %25%3E/config/environment.js',%0A      '%3C%25= modulePrefix %25%3E/config/environments/' + env + '.js',%0A
       'jquer

Add config directory to Brocfile [Fixes #81]