'use strict';

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

var _regenerator;

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

var _bluebird;

function _load_bluebird() {
  return _bluebird = require('bluebird');
}

var _logger;

function _load_logger() {
  return _logger = _interopRequireDefault(require('../../logger/logger'));
}

var _bitIds;

function _load_bitIds() {
  return _bitIds = _interopRequireDefault(require('../../bit-id/bit-ids'));
}

var _constants;

function _load_constants() {
  return _constants = require('../../constants');
}

var _consumer;

function _load_consumer() {
  return _consumer = _interopRequireDefault(require('../consumer'));
}

var _dataToPersist;

function _load_dataToPersist() {
  return _dataToPersist = _interopRequireDefault(require('../component/sources/data-to-persist'));
}

var _removePath;

function _load_removePath() {
  return _removePath = _interopRequireDefault(require('../component/sources/remove-path'));
}

var _dists;

function _load_dists() {
  return _dists = _interopRequireDefault(require('../component/sources/dists'));
}

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

exports.default = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(consumer, bitIds, deleteFilesForAuthor) {
    var filesToDelete, getFilesToDelete;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            getFilesToDelete = function getFilesToDelete() {
              var dataToPersist = new (_dataToPersist || _load_dataToPersist()).default();
              bitIds.forEach(function (id) {
                var ignoreVersion = id.isLocal() || !id.hasVersion();
                var componentMap = consumer.bitMap.getComponentIfExist(id, { ignoreVersion });
                if (!componentMap) {
                  (_logger || _load_logger()).default.warn(`deleteComponentsFiles was unable to delete ${id.toString()} because the id is missing from bitmap`);
                  return null;
                }
                if (componentMap.origin === (_constants || _load_constants()).COMPONENT_ORIGINS.IMPORTED || componentMap.origin === (_constants || _load_constants()).COMPONENT_ORIGINS.NESTED) {
                  // $FlowFixMe rootDir is set for non authored
                  var rootDir = componentMap.rootDir;
                  dataToPersist.removePath(new (_removePath || _load_removePath()).default(rootDir, true));
                  if (!consumer.shouldDistsBeInsideTheComponent()) {
                    var distDir = (_dists || _load_dists()).default.getDistDirWhenDistIsOutsideCompDir(consumer.config, rootDir);
                    dataToPersist.removePath(new (_removePath || _load_removePath()).default(distDir, true));
                  }
                } else if (componentMap.origin === (_constants || _load_constants()).COMPONENT_ORIGINS.AUTHORED && deleteFilesForAuthor) {
                  var filesToRemove = componentMap.getAllFilesPaths().map(function (f) {
                    return new (_removePath || _load_removePath()).default(f);
                  });
                  dataToPersist.removeManyPaths(filesToRemove);
                }
                return null;
              });
              return dataToPersist;
            };

            (_logger || _load_logger()).default.debug(`deleteComponentsFiles, ids: ${bitIds.toString()}`);
            filesToDelete = getFilesToDelete();

            filesToDelete.addBasePath(consumer.getPath());
            return _context.abrupt('return', filesToDelete.persistAllToFS());

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

  function deleteComponentsFiles(_x, _x2, _x3) {
    return _ref.apply(this, arguments);
  }

  return deleteComponentsFiles;
}();