"use strict";

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

var _path = _interopRequireDefault(require("path"));

var _memFs = _interopRequireDefault(require("mem-fs"));

var _memFsEditor = _interopRequireDefault(require("mem-fs-editor"));

var _chalk = _interopRequireDefault(require("chalk"));

var _lodash = require("lodash");

var _helpers = require("../utils/helpers");

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

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

var SeederGenerator =
/*#__PURE__*/
function () {
  function SeederGenerator(_ref) {
    var name = _ref.name,
        seederTemplate = _ref.seederTemplate,
        userSeedersFolderPath = _ref.userSeedersFolderPath;

    _classCallCheck(this, SeederGenerator);

    this._initOptions({
      seederTemplate: seederTemplate,
      userSeedersFolderPath: userSeedersFolderPath
    });

    this._initMemFs();

    this._initName(name);
  }
  /**
   * generate the new seeder
   */


  _createClass(SeederGenerator, [{
    key: "generate",
    value: function () {
      var _generate = _asyncToGenerator(
      /*#__PURE__*/
      regeneratorRuntime.mark(function _callee() {
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                this._validateSeederFileNotExists();

                this._copySeederTemplate();

                _context.next = 4;
                return this._commitMemFsChanges();

              case 4:
                return _context.abrupt("return", this.seederFileRelativePath);

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

      function generate() {
        return _generate.apply(this, arguments);
      }

      return generate;
    }()
    /**
     * Private
     */

  }, {
    key: "_initOptions",
    value: function _initOptions(_ref2) {
      var seederTemplate = _ref2.seederTemplate,
          userSeedersFolderPath = _ref2.userSeedersFolderPath;
      var userSeedersFolderName = (0, _helpers.getFolderNameFromPath)(userSeedersFolderPath);
      this.options = {
        seederTemplate: seederTemplate,
        userSeedersFolderName: userSeedersFolderName,
        userSeedersFolderPath: userSeedersFolderPath
      };
    }
  }, {
    key: "_initMemFs",
    value: function _initMemFs() {
      var store = _memFs["default"].create();

      this.fs = _memFsEditor["default"].create(store);
    }
  }, {
    key: "_initName",
    value: function _initName(name) {
      var _this$options = this.options,
          userSeedersFolderPath = _this$options.userSeedersFolderPath,
          userSeedersFolderName = _this$options.userSeedersFolderName; // set name

      this.name = name; // set seeder-name

      this.seederName = (0, _helpers.normalizeSeederName)(name); // set seeder-file-name

      this.seederFileName = (0, _helpers.normalizeSeederFileName)(name); // set seeder-file-path

      this.seederFilePath = _path["default"].join(userSeedersFolderPath, this.seederFileName); // set seeder-file-relative-path

      this.seederFileRelativePath = _path["default"].join(userSeedersFolderName, this.seederFileName);
    }
  }, {
    key: "_validateSeederFileNotExists",
    value: function _validateSeederFileNotExists() {
      if (this.fs.exists(this.seederFilePath)) {
        throw new Error("".concat(_chalk["default"].red('ERROR'), "\n         ").concat(this.seederFileRelativePath, " are already exists"));
      }
    }
  }, {
    key: "_commitMemFsChanges",
    value: function () {
      var _commitMemFsChanges2 = _asyncToGenerator(
      /*#__PURE__*/
      regeneratorRuntime.mark(function _callee2() {
        var _this = this;

        return regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                return _context2.abrupt("return", new Promise(function (resolve) {
                  _this.fs.commit(function () {
                    resolve();
                  });
                }));

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

      function _commitMemFsChanges() {
        return _commitMemFsChanges2.apply(this, arguments);
      }

      return _commitMemFsChanges;
    }()
  }, {
    key: "_copySeederTemplate",
    value: function _copySeederTemplate() {
      var seederName = this.seederName,
          seederFilePath = this.seederFilePath;
      var seederTemplate = this.options.seederTemplate;
      var templateArgs = {
        seederName: seederName
      };
      this.fs.copyTpl(seederTemplate, seederFilePath, templateArgs);
    }
  }]);

  return SeederGenerator;
}();

exports["default"] = SeederGenerator;