"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

// Seeder is an Abstract base class
// in order to use Seeder you need to
// extend Seeder into your own class and implement async run() method
var Seeder =
/*#__PURE__*/
function () {
  function Seeder() {
    _classCallCheck(this, Seeder);

    // Abstract class can not be constructed.
    // Seeder class should be extended.
    if (this.constructor === Seeder) {
      throw new TypeError('Can not construct abstract class.');
    } // Check if run method are implemented.


    if (this.run === Seeder.prototype.run) {
      throw new TypeError('Please implement abstract method run.');
    }
  }

  _createClass(Seeder, [{
    key: "seed",
    value: function () {
      var _seed = _asyncToGenerator(
      /*#__PURE__*/
      regeneratorRuntime.mark(function _callee() {
        var results;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return this.beforeRun();

              case 2:
                results = null;
                _context.next = 5;
                return this.shouldRun();

              case 5:
                if (!_context.sent) {
                  _context.next = 9;
                  break;
                }

                _context.next = 8;
                return this.run();

              case 8:
                results = _context.sent;

              case 9:
                return _context.abrupt("return", this.getStats(results));

              case 10:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function seed() {
        return _seed.apply(this, arguments);
      }

      return seed;
    }()
  }, {
    key: "shouldRun",
    value: function () {
      var _shouldRun = _asyncToGenerator(
      /*#__PURE__*/
      regeneratorRuntime.mark(function _callee2() {
        return regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                return _context2.abrupt("return", true);

              case 1:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }));

      function shouldRun() {
        return _shouldRun.apply(this, arguments);
      }

      return shouldRun;
    }()
  }, {
    key: "beforeRun",
    value: function () {
      var _beforeRun = _asyncToGenerator(
      /*#__PURE__*/
      regeneratorRuntime.mark(function _callee3() {
        return regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3);
      }));

      function beforeRun() {
        return _beforeRun.apply(this, arguments);
      }

      return beforeRun;
    }()
  }, {
    key: "run",
    value: function () {
      var _run = _asyncToGenerator(
      /*#__PURE__*/
      regeneratorRuntime.mark(function _callee4() {
        return regeneratorRuntime.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                throw new TypeError("Need to implement ".concat(this.constructor.name, " async run() function"));

              case 1:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4, this);
      }));

      function run() {
        return _run.apply(this, arguments);
      }

      return run;
    }()
  }, {
    key: "getStats",
    value: function getStats(results) {
      if (Array.isArray(results)) {
        return {
          run: true,
          created: results.length
        };
      }

      return {
        run: false,
        created: 0
      };
    }
  }], [{
    key: "extend",
    value: function extend() {
      var userSeederMethods = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

      var UserSeeder =
      /*#__PURE__*/
      function (_Seeder) {
        _inherits(UserSeeder, _Seeder);

        function UserSeeder() {
          _classCallCheck(this, UserSeeder);

          return _possibleConstructorReturn(this, _getPrototypeOf(UserSeeder).apply(this, arguments));
        }

        return UserSeeder;
      }(Seeder); // Add methods to the user seeder


      Object.keys(userSeederMethods).forEach(function (key) {
        UserSeeder.prototype[key] = userSeederMethods[key];
      });
      return UserSeeder;
    }
  }]);

  return Seeder;
}();

var _default = Seeder;
exports["default"] = _default;