"use strict";


/*global nodeca, _*/


// stdlib
var fs    = require('fs');
var path  = require('path');


// 3rd-party
var Mincer      = require('mincer');
var UglifyJS    = require('uglify-js');
var Csso        = require('csso');
var connect     = require('connect');
var nib         = require('nib');


// NLib
var HashTree    = require('nlib').Support.HashTree;


function get_compilable(filters, env) {
  var result = [];

  env.eachLogicalPath(filters, function (logicalPath) {
    result.push(logicalPath);
  });

  return result;
}


module.exports = function (next) {
  var files, env, manifest, assets_root;

  assets_root = path.resolve(__dirname, '../../public/assets');
  env         = new Mincer.Environment(nodeca.runtime.assets_path);
  manifest    = new Mincer.Manifest(env, assets_root);
  files       = ['app.js', 'app.css', 'modernizr.custom.js'];


  // function that mathces any non-js or non-css files
  files.push(function nonAsset(logicalPath) {
    var extname = path.extname(logicalPath);
    return (!/\.(js|css)$/.test(extname));
  });


  // Provide some helpers to EJS and Stylus
  env.registerHelper({
    asset_path: function (pathname) {
      var asset = this.environment.findAsset(pathname);
      return asset ? ("/assets/" + asset.digestPath) : null;
    },
    version: function () {
      return nodeca.runtime.version;
    },
    env: function () {
      return nodeca.runtime.env;
    },
    config: function (part) {
      return !part ? nodeca.config : HashTree.get(nodeca.config, part);
    }
  });


  // Add some funky stuff to Stylus
  Mincer.StylusEngine.registerConfigurator(function (style) {
    style.use(nib());
  });


  // fill in paths
  env.appendPath('assets/css');
  env.appendPath('assets/embedded_fonts');
  env.appendPath('assets/js');
  env.appendPath('assets/vendor');
  env.appendPath('system');


  // init JS compressor for non-dev setups only
  if ('development' !== nodeca.runtime.env) {
    env.jsCompressor = function (data, callback) {
      try {
        var ast = UglifyJS.parser.parse(data);

        ast = UglifyJS.uglify.ast_mangle(ast);
        ast = UglifyJS.uglify.ast_squeeze(ast);

        callback(null, UglifyJS.uglify.gen_code(ast));
      } catch (err) {
        callback(err);
      }
    };

    env.cssCompressor = function (data, callback) {
      try {
        callback(null, Csso.justDoIt(data));
      } catch (err) {
        callback(err);
      }
    };
  }


  Mincer.logger.use(console);


  // compile manifest
  manifest.compile(get_compilable(files, env), function (err, data) {
    if (err) {
      next(err);
      return;
    }

    nodeca.runtime.assets_mincer    = env;
    nodeca.runtime.assets_server    = Mincer.createServer(env, data);

    next();
  });
};

@@ -748,63 +748,8 @@
 h);%0A
-  manifest    = new Mincer.Manifest(env, assets_root);%0A
   fi
@@ -2422,24 +2422,197 @@
 console);%0A%0A%0A
+  // once environment is configured,%0A  // it can be replaced with 'static' cache version%0A  env         = env.index;%0A  manifest    = new Mincer.Manifest(env, assets_root);%0A%0A%0A
   // compile
@@ -2621,16 +2621,16 @@
 anifest%0A
-
   manife
@@ -2777,19 +2777,16 @@
 _mincer 
-   
 = env;%0A 
@@ -2817,19 +2817,16 @@
 _server 
-   
 = Mincer

Use cached version of index