'use strict';

var ProcessorStore = function(processors) {
  this.processors = processors || {};
  var that = this;

  return function(extension, pathMatches, factory) {
    if(!pathMatches) {
      return {
        list: function(path) {
          return that.list(extension, path);
        },
        set: function(pathMatchesScoped, factoryScoped) {
          return that.set(extension, pathMatchesScoped, factoryScoped);
        },
        add: function(pathMatchesScoped, factoryScoped) {
          return that.add(extension, pathMatchesScoped, factoryScoped);
        }
      };
    }

    return that.set(extension, pathMatches, factory);
  };
};

// Lists all processors. Can be filtered by extension and matching path
ProcessorStore.prototype.list = function(extension, path) {
  if(!extension) {
    return this.processors;
  }
  if(!path) {
    return this.processors[extension];
  }

  var filteredProcessors = this.processors[extension] || [];
  filteredProcessors = filteredProcessors.filter(function(item) {
    return item.pathMatches === null || !!path.match(item.pathMatches);
  });

  return filteredProcessors;
};

// Overwrites all processors for a given file extension
ProcessorStore.prototype.set = function(extension, pathMatches, factory) {
  this.processors[extension] = [];

  this.add(extension, pathMatches, factory);

  return this;
};

// Adds a processor for the given file extension
var addProcessor = function(type, extension, pathMatches, factory) {
  pathMatches = pathMatches || null;
  if(typeof pathMatches === 'function') {
    factory = pathMatches;
    pathMatches = null;
  }

  this.processors[extension] = this.processors[extension] || [];
  this.processors[extension][type]({pathMatches: pathMatches, pipe: factory});

  return this;
};

ProcessorStore.prototype.add = ProcessorStore.prototype.push = function(extension, pathMatches, factory) {
  return addProcessor.call(this, 'push', extension, pathMatches, factory);
};

ProcessorStore.prototype.unshift = function(extension, pathMatches, factory) {
  return addProcessor.call(this, 'unshift', extension, pathMatches, factory);
};

module.exports = ProcessorStore;

@@ -551,32 +551,324 @@
 factoryScoped);%0A
+        %7D,%0A        push: function(pathMatchesScoped, factoryScoped) %7B%0A          return that.push(extension, pathMatchesScoped, factoryScoped);%0A        %7D,%0A        unshift: function(pathMatchesScoped, factoryScoped) %7B%0A          return that.unshift(extension, pathMatchesScoped, factoryScoped);%0A
         %7D%0A      

Allow chaining with new push/unshift methods on processors