'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

var _Collection2 = require('./Collection');

var _Collection3 = _interopRequireDefault(_Collection2);

var _Injector = require('../tasks/Injector');

var _Injector2 = _interopRequireDefault(_Injector);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

/**
 * Parallel
 *
 * Executes tasks all at once
 *
 * @param {Function} complete function to be called on complete
 * @param {Function} recover function to be called on recover
 * @param {boolean} completeOnFirst completes on last or on first
 */
var Parallel = function (_Collection) {
  _inherits(Parallel, _Collection);

  /**
   * Parallel constructor
   *
   * @param {Function} complete function to be called on complete
   * @param {Function} recover function to be called on recover
   * @param {boolean} completeOnFirst completes on last or on first
   */
  function Parallel(complete, recover, completeOnFirst) {
    _classCallCheck(this, Parallel);

    var _this = _possibleConstructorReturn(this, (Parallel.__proto__ || Object.getPrototypeOf(Parallel)).call(this, complete, recover));

    _this._current = [];
    _this._passed = [];
    _this._completeOnFirst = completeOnFirst === true;
    return _this;
  }

  /**
   * Runs all tasks in the collection
   */


  _createClass(Parallel, [{
    key: 'run',
    value: function run() {
      _get(Parallel.prototype.__proto__ || Object.getPrototypeOf(Parallel.prototype), 'run', this).call(this);

      while (this._runCondition) {
        this._run(this.tasks.shift());
      }
    }

    /**
     * Runs a single task
     *
     * @protected
     */

  }, {
    key: '_run',
    value: function _run(task) {
      var _this2 = this;

      _Injector2.default.afterComplete(task, function (error, self) {
        _Injector2.default.resetAfterComplete(self, 'parallelAfterComplete');
        _this2._unchainTask(self);

        var index = _this2._current.indexOf(self);
        if (index !== -1) {
          _this2._current.splice(index, 1);
        }
        if (_this2._passed.indexOf(self) === -1) {
          _this2._passed.push(task);
        }

        if (_this2._completeOnFirst && _this2._passed.length === 1) {
          _this2._complete();
        } else if (!_this2._completeOnFirst && _this2._current.length === 0 && _this2.tasks.length === 0) {
          _this2._complete();
        }

        _this2._taskComplete();
      }, 'parallelAfterComplete');

      this._current.push(task);
      task.run();
    }

    /**
     * Returns the run condition
     *
     * @return {boolean} condition run condition
     * @protected
     */

  }, {
    key: '_taskComplete',


    /**
     * Performs on every task complete
     *
     * @protected
     */
    value: function _taskComplete() {}
    //


    /**
     * Stops all tasks in the collection
     *
     * @param {boolean} skip force the stopped task to be skipped
     * @return {boolean} stopped stopped status
     */

  }, {
    key: 'stop',
    value: function stop() {
      var _this3 = this;

      var skip = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;

      this._running = false;

      var result = true;

      this._current.forEach(function (item) {
        result = result || item.stop();

        if (skip) {
          _Injector2.default.resetAfterComplete(item, 'parallelAfterComplete');
          _this3._unchainTask(item);
        }
      });

      if (skip) {
        this._current = [];
      } else {
        while (this._current.length > 0) {
          this.tasks.push(this._current.shift());
        }
      }

      return result;
    }

    /**
     * Resets a stopped collection
     *
     * @return {boolean} reset reset status
     */

  }, {
    key: 'reset',
    value: function reset() {
      if (_get(Parallel.prototype.__proto__ || Object.getPrototypeOf(Parallel.prototype), 'reset', this).call(this)) {
        this._passed = [];

        return true;
      } else {
        return false;
      }
    }

    /**
     * Gets the current tasks
     *
     * @return {Array<Task>} task current tasks
     */

  }, {
    key: '_runCondition',
    get: function get() {
      return this.tasks.length > 0;
    }
  }, {
    key: 'current',
    get: function get() {
      return this._current;
    }

    /**
     * Gets the passed task(s)
     *
     * @return {Array<Task>} task passed task(s)
     */

  }, {
    key: 'passed',
    get: function get() {
      return this._passed;
    }
  }]);

  return Parallel;
}(_Collection3.default);

exports.default = Parallel;