'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; }

/**
 * Sequence
 *
 * Executes tasks one by one
 *
 * @param {Function} complete function to be called on complete
 * @param {Function} recover function to be called on recover
 */
var Sequence = function (_Collection) {
  _inherits(Sequence, _Collection);

  /**
   * Sequence constructor
   *
   * @param {Function} complete function to be called on complete
   * @param {Function} recover function to be called on recover
   */
  function Sequence(complete, recover) {
    _classCallCheck(this, Sequence);

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

    _this._current = undefined;
    _this._passed = [];
    _this._stopped = false;
    return _this;
  }

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


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

      this._stopped = false;

      this._next();
    }

    /**
     * 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 skip = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;

      this._stopped = true;
      this._running = false;

      var current = this.current;
      if (current !== undefined) {
        if (skip === true) {
          this._resetCurrent(true, true);
        } else {
          if (this.tasks.indexOf(current) === -1) {
            this.unshift(current);
          }
        }

        return current.stop() || this.tasks.length > 0;
      } else {
        return false;
      }
    }

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

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

        return true;
      } else {
        return false;
      }
    }

    /**
     * Runs next in the sequence
     *
     * @protected
     */

  }, {
    key: '_next',
    value: function _next() {
      if (this._stopped) return;

      if (this.tasks.length > 0) {
        this.__next.run();
      } else {
        this._complete();
      }
    }

    /**
     * Complete
     *
     * Runs complete task
     *
     * @protected
     */

  }, {
    key: '_complete',
    value: function _complete() {
      this._resetCurrent(true);

      _get(Sequence.prototype.__proto__ || Object.getPrototypeOf(Sequence.prototype), '_complete', this).call(this);
    }

    /**
     * Recover
     *
     * Runs recover task
     *
     * @protected
     */

  }, {
    key: '_recover',
    value: function _recover(error) {
      if (this._stopped) return;

      if (this.tasks.length > 0) {
        var task = this.__next;
        task.recover(error);
        if (!task.running && !task.done) {
          this._resetCurrent(true);

          _get(Sequence.prototype.__proto__ || Object.getPrototypeOf(Sequence.prototype), '_recover', this).call(this, error);
        }
      } else {
        this._complete();
      }
    }

    /**
     * Resets current
     *
     * @protected
     */

  }, {
    key: '_resetCurrent',
    value: function _resetCurrent() {
      var unchain = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
      var resetAfterComplete = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

      if (this._current !== undefined) {
        if (unchain) {
          this._unchainTask(this._current);
        }
        if (resetAfterComplete) {
          _Injector2.default.resetAfterComplete(this._current, 'sequenceAfterComplete');
        }
        this._current = undefined;
      }
    }

    /**
     * Gets next task from the loop
     *
     * @return {Task} task next task
     * @protected
     */

  }, {
    key: '__next',
    get: function get() {
      var _this2 = this;

      this._current = this.tasks.length > 0 ? _Injector2.default.afterComplete(this.tasks.shift(), function (error) {
        _Injector2.default.resetAfterComplete(_this2._current, 'sequenceAfterComplete');

        if (_this2._passed.length === 0 || _this2._passed.indexOf(_this2._current) === -1) {
          _this2._passed.push(_this2._current);
        }

        _this2._resetCurrent(true);

        if (error === undefined) {
          _this2._next();
        } else {
          _this2._recover(error);
        }
      }, 'sequenceAfterComplete') : undefined;

      return this._current;
    }

    /**
     * Gets the current task
     *
     * @return {Task} task current task
     */

  }, {
    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 Sequence;
}(_Collection3.default);

exports.default = Sequence;