module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    concat: {
      options: {
        process: function(src, filepath) {
          var unwraps = ['/js/'];

          var shouldWrap = true;
          for (var i = 0; i < unwraps.length; ++i) {
            if (filepath.indexOf(unwraps[i]) >= 0) {
              shouldWrap = false;
              break;
            }
          }

          if (shouldWrap) {
            return '// Source: ' + filepath + '\n' +
              '(function() {\n' + src + '\n})();\n';
          } else {
            return '// Source: ' + filepath + '\n' + src + '\n\n';
          }
        },
      },
      build: {
        src: ['../static/lib/**/*.js', '../static/js/**/*.js', '../static/dist/template-cache.js'],
        dest: '../static/dist/<%= pkg.name %>.js'
      }
    },

    cssmin: {
      '../static/dist/<%= pkg.name %>.css': ['../static/lib/**/*.css', '../static/css/**/*.css']
    },

    uglify: {
      options: {
        mangle: false,
        sourceMap: false
      },
      js_min: {
        files: {
          '../static/dist/<%= pkg.name %>.min.js': ['../static/dist/<%= pkg.name %>.js']
        }
      }
    },

    ngtemplates:  {
      options: {
        url: function(path) {
          return '/' + path.substr(3); // remove the ../
        },
        htmlmin: {
          collapseBooleanAttributes:      false,
          collapseWhitespace:             true,
          removeAttributeQuotes:          true,
          removeComments:                 true, // Only if you don't use comment directives!
          removeEmptyAttributes:          true,
          removeRedundantAttributes:      true,
          removeScriptTypeAttributes:     true,
          removeStyleLinkTypeAttributes:  true,
          keepClosingSlash:               true  // For inline SVG
        }
      },
      quay: {
        src: ['../static/partials/*.html', '../static/directives/*.html', '../static/directives/*.html'
              , '../static/directives/config/*.html', '../static/tutorial/*.html'],
        dest: '../static/dist/template-cache.js'
      }
    },

    cachebuster: {
        build: {
            options: {
                format: 'json',
                basedir: '../static/'
            },
            src: [ '../static/dist/template-cache.js', '../static/dist/<%= pkg.name %>.min.js',
                   '../static/dist/<%= pkg.name %>.css' ],
            dest: '../static/dist/cachebusters.json'
        }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-angular-templates');
  grunt.loadNpmTasks('grunt-cachebuster');

  // Default task(s).
  grunt.registerTask('default', ['ngtemplates', 'concat', 'cssmin', 'uglify', 'cachebuster']);
};