'use strict';

var async = require('async');
var os = require('os');

module.exports = function(grunt) {

  var zopfli = require('./lib/zopfli')(grunt);

  grunt.registerMultiTask('zopfli', 'Compress files.', function() {
    zopfli.options = this.options({
      mode: null,
      extension: null,
      limit: os.cpus().length,
      zopfliOptions: {}
    });

    var done = this.async();
    var promises = [];

    this.files.forEach(function(filePair) {
      filePair.src.forEach(function(src) {
        if (grunt.file.isDir(src)) {
          return;
        }

        if (zopfli.options.mode === null) {
          zopfli.options.mode = zopfli.detectMode(filePair.dest, zopfli.options.extension);
        }

        if (zopfli.options.extension === null) {
          zopfli.options.extension = zopfli.detectExtension(zopfli.options.mode);
        }

        if (grunt.util._.include(['gzip', 'zlib', 'deflate'], zopfli.options.mode) === false) {
          grunt.fail.warn('Mode ' + zopfli.options.mode + ' is not supported.');
        }
        promises.push(function(cb) {
          zopfli.compressFile(src, filePair.dest, zopfli.options.mode, zopfli.options.extension, zopfli.options.zopfliOptions, cb);
        });
      });
    });

    async.parallelLimit(promises, zopfli.options.limit, function(error) {
      done(!error);
    });

  });
};

@@ -866,29 +866,8 @@
  if 
-(grunt.util._.include
 (%5B'g
@@ -890,18 +890,25 @@
 eflate'%5D
-, 
+.indexOf(
 zopfli.o
@@ -928,13 +928,10 @@
 === 
-false
+-1
 ) %7B%0A

improve compatibility with grunt 1.x.x by using indexOf() instead of deprecated grunt.util._