'use strict';

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

var _regenerator;

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

var _path;

function _load_path() {
  return _path = _interopRequireDefault(require('path'));
}

var _fsExtra;

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

var _pMapSeries;

function _load_pMapSeries() {
  return _pMapSeries = _interopRequireDefault(require('p-map-series'));
}

var _;

function _load_() {
  return _ = require('..');
}

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

/**
 * This function will remove the list of files from fs
 * Then go to the folder of each file and remove it as well if it's empty
 */
exports.default = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(filesPaths) {
    var deleteP, dirs, sortedDirs, deleteDirsP;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            deleteP = [];
            dirs = [];

            filesPaths.forEach(function (filePath) {
              dirs.push((_path || _load_path()).default.dirname(filePath));
              deleteP.push((_fsExtra || _load_fsExtra()).default.remove(filePath));
            });
            _context.next = 5;
            return Promise.all(deleteP);

          case 5:
            // Sorting it to make sure we will delete the inner dirs first
            sortedDirs = dirs.sort().reverse();
            deleteDirsP = (0, (_pMapSeries || _load_pMapSeries()).default)(sortedDirs, (_ || _load_()).removeEmptyDir);
            _context.next = 9;
            return deleteDirsP;

          case 9:
            return _context.abrupt('return', true);

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

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

  return removeFilesAndEmptyDirsRecursively;
}();