'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

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

function _load_fsExtra() {
  return _fsExtra = _interopRequireDefault(require('fs-extra'));
}

var _removeFilesAndEmptyDirsRecursively;

function _load_removeFilesAndEmptyDirsRecursively() {
  return _removeFilesAndEmptyDirsRecursively = _interopRequireDefault(require('../../../utils/fs/remove-files-and-empty-dirs-recursively'));
}

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

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 RemovePath = function () {
  function RemovePath(path) {
    var removeItsDirIfEmpty = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

    _classCallCheck(this, RemovePath);

    this.path = path;
    this.removeItsDirIfEmpty = removeItsDirIfEmpty;
  }

  _createClass(RemovePath, [{
    key: 'persistToFS',
    value: function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee() {
        return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                if (!this.removeItsDirIfEmpty) {
                  _context.next = 2;
                  break;
                }

                return _context.abrupt('return', (0, (_removeFilesAndEmptyDirsRecursively || _load_removeFilesAndEmptyDirsRecursively()).default)([this.path]));

              case 2:
                return _context.abrupt('return', (_fsExtra || _load_fsExtra()).default.remove(this.path));

              case 3:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function persistToFS() {
        return _ref.apply(this, arguments);
      }

      return persistToFS;
    }()
  }]);

  return RemovePath;
}();

exports.default = RemovePath;