"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

function _path() {
  const data = _interopRequireDefault(require("path"));

  _path = function () {
    return data;
  };

  return data;
}

function _fsExtra() {
  const data = _interopRequireDefault(require("fs-extra"));

  _fsExtra = function () {
    return data;
  };

  return data;
}

function _pMapSeries() {
  const data = _interopRequireDefault(require("p-map-series"));

  _pMapSeries = function () {
    return data;
  };

  return data;
}

function _removeEmptyDir() {
  const data = _interopRequireDefault(require("./remove-empty-dir"));

  _removeEmptyDir = function () {
    return data;
  };

  return data;
}

function _logger() {
  const data = _interopRequireDefault(require("../../logger/logger"));

  _logger = function () {
    return data;
  };

  return data;
}

/**
 * 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
 */
var _default =
/*#__PURE__*/
function () {
  var _removeFilesAndEmptyDirsRecursively = (0, _bluebird().coroutine)(function* (filesPaths) {
    const dirs = filesPaths.map(filePath => _path().default.dirname(filePath));
    const deleteP = filesPaths.map(filePath => _fsExtra().default.remove(filePath));

    _logger().default.info(`remove-files-and-empty-dirs-recursively deleting the following paths: ${filesPaths.join(', ')}`);

    yield Promise.all(deleteP); // Sorting it to make sure we will delete the inner dirs first

    const sortedDirs = dirs.sort().reverse();
    yield (0, _pMapSeries().default)(sortedDirs, dir => (0, _removeEmptyDir().default)(dir));
    return true;
  });

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

  return removeFilesAndEmptyDirsRecursively;
}();

exports.default = _default;