'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 _consumerComponent;

function _load_consumerComponent() {
  return _consumerComponent = _interopRequireDefault(require('../component/consumer-component'));
}

var _bitJson;

function _load_bitJson() {
  return _bitJson = _interopRequireDefault(require('../bit-json'));
}

var _utils;

function _load_utils() {
  return _utils = require('../../utils');
}

var _generalError;

function _load_generalError() {
  return _generalError = _interopRequireDefault(require('../../error/general-error'));
}

var _bitMap;

function _load_bitMap() {
  return _bitMap = _interopRequireDefault(require('../bit-map'));
}

var _configDir;

function _load_configDir() {
  return _configDir = _interopRequireDefault(require('../bit-map/config-dir'));
}

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

exports.default = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(component, consumerPath, bitMap, configDir) {
    var force = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
    var componentMap, componentDir, resolvedConfigDir, deleteCompilerFilesP, deleteTesterFilesP, bitJsonDir, bitJsonDirFullPath;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            componentMap = component.componentMap;

            if (componentMap) {
              _context.next = 3;
              break;
            }

            throw new (_generalError || _load_generalError()).default('could not find component in the .bitmap file');

          case 3:
            componentDir = componentMap.getComponentDir();

            // TODO: check if files were modified before deleting them and delete them only if forces provided

            if (!force) {}
            // To implement
            // Check if config files changed
            // If yes throw error


            // Passing here the ENV_TYPE as well to make sure it's not removed since we need it later
            resolvedConfigDir = configDir.getResolved({ componentDir });
            deleteCompilerFilesP = component.compiler ? component.compiler.removeFilesFromFs(component.compilerDependencies) : Promise.resolve('');
            deleteTesterFilesP = component.tester ? component.tester.removeFilesFromFs(component.testerDependencies) : Promise.resolve('');
            _context.next = 10;
            return Promise.all([deleteCompilerFilesP, deleteTesterFilesP]);

          case 10:

            // Delete bit.json and bit.json dir
            bitJsonDir = resolvedConfigDir.getEnvTypeCleaned();
            bitJsonDirFullPath = (_path || _load_path()).default.normalize((_path || _load_path()).default.join(consumerPath, bitJsonDir.dirPath));
            _context.next = 14;
            return (_bitJson || _load_bitJson()).default.removeIfExist(bitJsonDirFullPath);

          case 14:
            _context.next = 16;
            return (0, (_utils || _load_utils()).removeEmptyDir)(bitJsonDirFullPath);

          case 16:
            return _context.abrupt('return', { id: component.id.toStringWithoutVersion() });

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

  function injectConf(_x2, _x3, _x4, _x5) {
    return _ref.apply(this, arguments);
  }

  return injectConf;
}();