text: 'use strict';
  
  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 _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
  
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  
  var fs = require('fs-extra'); /**
                                 * Copyright (c) 2017 - present, Botfuel (https://www.botfuel.io).
                                 *
                                 * Licensed under the Apache License, Version 2.0 (the "License");
                                 * you may not use this file except in compliance with the License.
                                 * You may obtain a copy of the License at
                                 *
                                 * http://www.apache.org/licenses/LICENSE-2.0
                                 *
                                 * Unless required by applicable law or agreed to in writing, software
                                 * distributed under the License is distributed on an "AS IS" BASIS,
                                 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
                                 * See the License for the specific language governing permissions and
                                 * limitations under the License.
                                 */
  
  var logger = require('logtown')('MiddlewareManager');
  
  /**
   * Manages the middlewares.
   */
  var MiddlewareManager = function () {
    function MiddlewareManager(bot) {
      var inMiddlewares = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
      var outMiddlewares = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  
      _classCallCheck(this, MiddlewareManager);
  
      logger.debug('constructor', inMiddlewares, outMiddlewares);
      this.inMiddlewares = inMiddlewares;
      this.outMiddlewares = outMiddlewares;
  
      if (bot.config) {
        var middlewarePath = bot.config.path + '/src/middlewares.js';
        if (fs.pathExistsSync(middlewarePath)) {
          var middleware = require(middlewarePath);
          this.inMiddlewares = middleware.in || this.inMiddlewares;
          this.outMiddlewares = middleware.out || this.outMiddlewares;
        }
      }
      logger.debug('constructor', this.inMiddlewares, this.outMiddlewares);
    }
  
    /**
     * Executes the in middlewares.
     * @param {Object} callback - this function is executed when middlewares complete
     */
  
  
    _createClass(MiddlewareManager, [{
      key: 'in',
      value: function () {
        var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(context, callback) {
          var _this = this;
  
          return regeneratorRuntime.wrap(function _callee2$(_context2) {
            while (1) {
              switch (_context2.prev = _context2.next) {
                case 0:
                  _context2.next = 2;
                  return this.inRun(context, callback, 0, _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
                    return regeneratorRuntime.wrap(function _callee$(_context) {
                      while (1) {
                        switch (_context.prev = _context.next) {
                          case 0:
                          case 'end':
                            return _context.stop();
                        }
                      }
                    }, _callee, _this);
                  })));
  
                case 2:
                case 'end':
                  return _context2.stop();
              }
            }
          }, _callee2, this);
        }));
  
        function _in(_x3, _x4) {
          return _ref.apply(this, arguments);
        }
  
        return _in;
      }()
  
      /**
       * Executes the in middlewares starting from a given index.
       * @private
       * @param callback - this function is executed only when middlewares complete
       * @param done - this function is executed at the end,
       * independently of the completion of the middlewares
       */
  
    }, {
      key: 'inRun',
      value: function () {
        var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(context, callback, index, done) {
          var _this2 = this;
  
          var middleware, _next;
  
          return regeneratorRuntime.wrap(function _callee4$(_context4) {
            while (1) {
              switch (_context4.prev = _context4.next) {
                case 0:
                  if (!(this.inMiddlewares.length === index)) {
                    _context4.next = 9;
                    break;
                  }
  
                  logger.debug('inRun: calling callback');
                  _context4.next = 4;
                  return callback();
  
                case 4:
                  logger.debug('inRun: calling done');
                  _context4.next = 7;
                  return done();
  
                case 7:
                  _context4.next = 14;
                  break;
  
                case 9:
                  middleware = this.inMiddlewares[index];
  
                  _next = function () {
                    var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(d) {
                      return regeneratorRuntime.wrap(function _callee3$(_context3) {
                        while (1) {
                          switch (_context3.prev = _context3.next) {
                            case 0:
                              return _context3.abrupt('return', _this2.inRun(context, callback, index + 1, d || done));
  
                            case 1:
                            case 'end':
                              return _context3.stop();
                          }
                        }
                      }, _callee3, _this2);
                    }));
  
                    return function _next(_x9) {
                      return _ref4.apply(this, arguments);
                    };
                  }();
  
                  logger.debug('inRun: calling middleware');
                  _context4.next = 14;
                  return middleware(context, _next, done);
  
                case 14:
                case 'end':
                  return _context4.stop();
              }
            }
          }, _callee4, this);
        }));
  
        function inRun(_x5, _x6, _x7, _x8) {
          return _ref3.apply(this, arguments);
        }
  
        return inRun;
      }()
  
      /**
       * Executes the out middlewares.
       * @param callback - this function is executed when middlewares complete
       */
  
    }, {
      key: 'out',
      value: function () {
        var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6(context, callback) {
          var _this3 = this;
  
          var done;
          return regeneratorRuntime.wrap(function _callee6$(_context6) {
            while (1) {
              switch (_context6.prev = _context6.next) {
                case 0:
                  done = function () {
                    var _ref6 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() {
                      return regeneratorRuntime.wrap(function _callee5$(_context5) {
                        while (1) {
                          switch (_context5.prev = _context5.next) {
                            case 0:
                            case 'end':
                              return _context5.stop();
                          }
                        }
                      }, _callee5, _this3);
                    }));
  
                    return function done() {
                      return _ref6.apply(this, arguments);
                    };
                  }();
  
                  _context6.next = 3;
                  return this.outRun(context, callback, 0, done);
  
                case 3:
                case 'end':
                  return _context6.stop();
              }
            }
          }, _callee6, this);
        }));
  
        function out(_x10, _x11) {
          return _ref5.apply(this, arguments);
        }
  
        return out;
      }()
  
      /**
       * Executes the out middlewares starting from a given index.
       * @private
       * @param callback - this function is executed only when middlewares complete
       * @param done - this function is executed at the end,
       * independently of the completion of the middlewares
       */
  
    }, {
      key: 'outRun',
      value: function () {
        var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8(context, callback, index, done) {
          var _this4 = this;
  
          var middleware, _next2;
  
          return regeneratorRuntime.wrap(function _callee8$(_context8) {
            while (1) {
              switch (_context8.prev = _context8.next) {
                case 0:
                  if (!(this.outMiddlewares.length === index)) {
                    _context8.next = 9;
                    break;
                  }
  
                  logger.debug('outRun: calling callback');
                  _context8.next = 4;
                  return callback();
  
                case 4:
                  logger.debug('outRun: calling done');
                  _context8.next = 7;
                  return done();
  
                case 7:
                  _context8.next = 14;
                  break;
  
                case 9:
                  middleware = this.outMiddlewares[index];
  
                  _next2 = function () {
                    var _ref8 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7(d) {
                      return regeneratorRuntime.wrap(function _callee7$(_context7) {
                        while (1) {
                          switch (_context7.prev = _context7.next) {
                            case 0:
                              return _context7.abrupt('return', _this4.outRun(context, callback, index + 1, d || done));
  
                            case 1:
                            case 'end':
                              return _context7.stop();
                          }
                        }
                      }, _callee7, _this4);
                    }));
  
                    return function _next2(_x16) {
                      return _ref8.apply(this, arguments);
                    };
                  }();
  
                  logger.debug('outRun: calling middleware');
                  _context8.next = 14;
                  return middleware(context, _next2, done);
  
                case 14:
                case 'end':
                  return _context8.stop();
              }
            }
          }, _callee8, this);
        }));
  
        function outRun(_x12, _x13, _x14, _x15) {
          return _ref7.apply(this, arguments);
        }
  
        return outRun;
      }()
    }]);
  
    return MiddlewareManager;
  }();
  
  module.exports = MiddlewareManager;