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

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

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _bitJson;

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

var _removeEmptyDir;

function _load_removeEmptyDir() {
  return _removeEmptyDir = _interopRequireDefault(require('../../utils/fs/remove-empty-dir'));
}

var _generalError;

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

var _configDir;

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

var _sources;

function _load_sources() {
  return _sources = require('../component/sources');
}

var _constants;

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

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(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();

            if (!(!force && areEnvsModified(component, component.componentFromModel))) {
              _context.next = 6;
              break;
            }

            throw new Error('unable to inject-conf, some or all configuration files are modified. please use "--force" flag to force removing the configuration files');

          case 6:

            // 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, configDir, (_constants || _load_constants()).COMPILER_ENV_TYPE, consumerPath) : Promise.resolve('');
            deleteTesterFilesP = component.tester ? component.tester.removeFilesFromFs(component.testerDependencies, configDir, (_constants || _load_constants()).TESTER_ENV_TYPE, consumerPath) : Promise.resolve('');
            _context.next = 11;
            return Promise.all([deleteCompilerFilesP, deleteTesterFilesP]);

          case 11:

            // 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 = 15;
            return (_bitJson || _load_bitJson()).default.removeIfExist(bitJsonDirFullPath);

          case 15:
            _context.next = 17;
            return (0, (_removeEmptyDir || _load_removeEmptyDir()).default)(bitJsonDirFullPath);

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

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

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

  return injectConf;
}();

/**
 * returns whether the envs configuration files were modified on the filesystem
 */


function areEnvsModified(componentFromFs, componentFromModel) {
  if (!componentFromModel) return false;
  var envTypes = [(_constants || _load_constants()).COMPILER_ENV_TYPE, (_constants || _load_constants()).TESTER_ENV_TYPE];
  return envTypes.some(function (envType) {
    var fsHashes = // $FlowFixMe
    componentFromFs[envType] && componentFromFs[envType].files ? componentFromFs[envType].files.map(function (file) {
      return file.toSourceAsLinuxEOL().hash();
    }).sort() : [];
    var modelHashes = // $FlowFixMe
    componentFromModel[envType] && componentFromModel[envType].files ? componentFromModel[envType].files.map(function (file) {
      return file.file.hash();
    }).sort() : [];
    return !(_ramda || _load_ramda()).default.equals(fsHashes, modelHashes);
  });
}