// Generated by CoffeeScript 1.6.3
(function() {
  var EventEmitterGrouped, TaskGroup, ambi, attach, events, inheritInto, _ref,
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
    __slice = [].slice;

  events = typeof window !== "undefined" && window !== null ? require('events-browser') : require('events');

  ambi = require('ambi');

  TaskGroup = require('taskgroup').TaskGroup;

  EventEmitterGrouped = (function(_super) {
    __extends(EventEmitterGrouped, _super);

    function EventEmitterGrouped() {
      _ref = EventEmitterGrouped.__super__.constructor.apply(this, arguments);
      return _ref;
    }

    EventEmitterGrouped.prototype.getListenerGroup = function() {
      var args, eventName, listeners, me, next, tasks, _i;
      eventName = arguments[0], args = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), next = arguments[_i++];
      me = this;
      listeners = this.listeners(eventName);
      tasks = new TaskGroup().once('complete', next);
      listeners.sort(function(a, b) {
        return (b.priority || 0) - (a.priority || 0);
      });
      listeners.forEach(function(listener) {
        if (listener.listener) {
          listener = [listener.bind(me), listener.listener];
        } else {
          listener = listener.bind(me);
        }
        return tasks.addTask(function(complete) {
          return ambi.apply(null, [listener].concat(__slice.call(args), [complete]));
        });
      });
      return tasks;
    };

    EventEmitterGrouped.prototype.off = function() {
      var args;
      args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      return this.removeListener.apply(this, args);
    };

    EventEmitterGrouped.prototype.emitSerial = function() {
      var args;
      args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      return this.getListenerGroup.apply(this, args).run();
    };

    EventEmitterGrouped.prototype.emitParallel = function() {
      var args;
      args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      return this.getListenerGroup.apply(this, args).setConfig({
        concurrency: 0
      }).run();
    };

    return EventEmitterGrouped;

  })(events.EventEmitter);

  attach = function(somethingElse) {
    var key, value, _ref1;
    _ref1 = EventEmitterGrouped.prototype;
    for (key in _ref1) {
      if (!__hasProp.call(_ref1, key)) continue;
      value = _ref1[key];
      somethingElse[key] = value;
    }
    return somethingElse;
  };

  inheritInto = function(somethingElse) {
    return util.inherits(somethingElse, EventEmitterGrouped);
  };

  module.exports = {
    EventEmitterGrouped: EventEmitterGrouped,
    attach: attach,
    inheritInto: inheritInto
  };

}).call(this);
