var postcss = require('postcss');
var minimatch = require('minimatch');
var Promise = require('promise');
var path = require('path');

module.exports = main;

function main(options) {

  var options = options || {};
  var pluginsConfig = options.plugins;
  var plugins = [];

  // Require each plugin, pass it it’s options
  // and add it to the plugins array.
  Object.keys(pluginsConfig).forEach(function(pluginName) {
    var value = pluginsConfig[pluginName];
    if (value === false) return;
    var pluginOptions = value === true ? {} : value;
    var plugin = require(pluginName);
    plugins.push(plugin(pluginOptions));
  });

  var map = normalizeMapOptions(options.map);

  var processor = postcss(plugins);

  return function (files, metalsmith, done) {
    var styles = Object.keys(files).filter(minimatch.filter("*.css", { matchBase: true }));

    if(styles.length == 0) {
      done();
      return;
    }

    var promises = [];

    styles.forEach(function (file) {
      var contents = files[file].contents.toString();
      var absolutePath = path.resolve(metalsmith.source(), file);

      var promise = processor
        .process(contents, {
          from: absolutePath,
          to: absolutePath,
          map: map
        })
        .then(function (result) {
          files[file].contents = new Buffer(result.css);

           if (result.map) {
             files[file + '.map'] = {
               contents: new Buffer(JSON.stringify(result.map)),
               mode: files[file].mode,
               stats: files[file].stats
             };
           }
        });

        promises.push(promise);
    });

    Promise.all(promises)
      .then(function(results) {
        done();
      })
      .catch(function(error) {
        done(new Error("Error during postcss processing: " + JSON.stringify(error)));
      });

  }
}

function normalizeMapOptions(map) {
  if (!map) return undefined;

  return {
    inline: map === true ? true : map.inline,
    prev: false, // Not implemented yet
    sourcesContent: undefined,  // Not implemented yet
    annotation: undefined // Not implemented yet
  };
}

@@ -354,16 +354,393 @@
  array.%0A
+  if (Array.isArray(pluginsConfig)) %7B%0A    pluginsConfig.forEach(function (plugin) %7B%0A      var pkg;%0A      var config = %7B%7D;%0A      if (typeof plugin === 'string') %7B%0A        pkg = plugin;%0A      %7D else %7B%0A        pkg = plugin%5B0%5D;%0A        config = plugin%5B1%5D;%0A      %7D%0A      if (config === false) %7B%0A        return;%0A      %7D%0A      plugins.push(require(pkg)(config));%0A    %7D);%0A  %7D else %7B%0A  
   Object
@@ -776,16 +776,17 @@
 function
+ 
 (pluginN
@@ -788,24 +788,26 @@
 uginName) %7B%0A
+  
     var valu
@@ -837,24 +837,26 @@
 nName%5D;%0A    
+  
 if (value ==
@@ -872,24 +872,26 @@
 return;%0A    
+ 
+ 
 var pluginOp
@@ -935,21 +935,23 @@
     
-var
+ 
  plugin
- = 
+s.push(
 requ
@@ -969,33 +969,8 @@
 ame)
-;%0A    plugins.push(plugin
 (plu
@@ -983,21 +983,27 @@
 ions));%0A
+  
   %7D);
+%0A  %7D
 %0A%0A  var 

add array syntax support