"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; }; }();

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

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

/**
 * Launcher
 *
 * Launcher class. Keeps original method and injections.
 *
 * @param {Function} original original function
 */
var Launcher = function () {
  /**
   * Task constructor
   *
   * @param {Function} original original function
   */
  function Launcher(original) {
    _classCallCheck(this, Launcher);

    this.original = original;
    this.before = [];
    this.after = [];

    this.launch = this.launch.bind(this);
  }

  /**
   * Launches
   *
   * Runs the original function and all the injections.
   * Tries to combine arguments and keeps the changes
   *
   * @param {any} any any arguments
   * @return {any} any any result from original function
   */


  _createClass(Launcher, [{
    key: "launch",
    value: function launch() {
      for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      var result = undefined;
      this.before.forEach(function (callback) {
        var result = callback.apply(undefined, _toConsumableArray(args));
        args = result !== undefined ? result : args;
      });
      if (this.original !== undefined) {
        result = this.original.apply(this, _toConsumableArray(args));
      }
      this.after.forEach(function (callback) {
        var result = callback.apply(undefined, _toConsumableArray(args));
        args = result !== undefined ? result : args;
      });
      return result;
    }
  }]);

  return Launcher;
}();

exports.default = Launcher;