'use strict';

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

var _regenerator;

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

var movePaths = exports.movePaths = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(consumer, _ref) {
    var from = _ref.from,
        to = _ref.to;

    var fromExists, toExists, fromRelative, toRelative, fromAbsolute, toAbsolute, existingPath, changes, componentsIds, _ref3, components;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            fromExists = (_fsExtra || _load_fsExtra()).default.existsSync(from);
            toExists = (_fsExtra || _load_fsExtra()).default.existsSync(to);

            if (!(fromExists && toExists)) {
              _context.next = 4;
              break;
            }

            throw new (_generalError || _load_generalError()).default(`unable to move because both paths from (${from}) and to (${to}) already exist`);

          case 4:
            if (!(!fromExists && !toExists)) {
              _context.next = 6;
              break;
            }

            throw new (_generalError || _load_generalError()).default(`both paths from (${from}) and to (${to}) do not exist`);

          case 6:
            fromRelative = consumer.getPathRelativeToConsumer(from);
            toRelative = consumer.getPathRelativeToConsumer(to);
            fromAbsolute = consumer.toAbsolutePath(fromRelative);
            toAbsolute = consumer.toAbsolutePath(toRelative);
            existingPath = fromExists ? fromAbsolute : toAbsolute;
            changes = consumer.bitMap.updatePathLocation(fromRelative, toRelative, existingPath);

            if (fromExists && !toExists) {
              // user would like to physically move the file. Otherwise (!fromExists and toExists), user would like to only update bit.map
              (0, (_moveSync || _load_moveSync()).default)(fromAbsolute, toAbsolute);
            }

            if ((_ramda || _load_ramda()).default.isEmpty(changes)) {
              _context.next = 25;
              break;
            }

            componentsIds = changes.map(function (c) {
              return (_bitId || _load_bitId()).BitId.parse(c.id);
            });
            _context.next = 17;
            return (_packageJson || _load_packageJson()).addComponentsToRoot(consumer, componentsIds);

          case 17:
            _context.next = 19;
            return consumer.loadComponents(componentsIds);

          case 19:
            _ref3 = _context.sent;
            components = _ref3.components;
            _context.next = 23;
            return (0, (_links || _load_links()).linkComponentsToNodeModules)(components, consumer);

          case 23:
            _context.next = 25;
            return (0, (_links || _load_links()).reLinkDependents)(consumer, components);

          case 25:
            return _context.abrupt('return', changes);

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

  return function movePaths(_x, _x2) {
    return _ref2.apply(this, arguments);
  };
}();

exports.moveExistingComponent = moveExistingComponent;

var _fsExtra;

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

var _ramda;

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

var _bitId;

function _load_bitId() {
  return _bitId = require('../../bit-id');
}

var _links;

function _load_links() {
  return _links = require('../../links');
}

var _packageJson;

function _load_packageJson() {
  return _packageJson = _interopRequireWildcard(require('../component/package-json'));
}

var _generalError;

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

var _;

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

var _consumerComponent;

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

var _moveSync;

function _load_moveSync() {
  return _moveSync = _interopRequireDefault(require('../../utils/fs/move-sync'));
}

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

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

function moveExistingComponent(consumer, component, oldPath, newPath) {
  if ((_fsExtra || _load_fsExtra()).default.existsSync(newPath)) {
    throw new (_generalError || _load_generalError()).default(`could not move the component ${component.id.toString()} from ${oldPath} to ${newPath} as the destination path already exists`);
  }
  var componentMap = consumer.bitMap.getComponent(component.id);
  componentMap.updateDirLocation(consumer.getPathRelativeToConsumer(oldPath), consumer.getPathRelativeToConsumer(newPath));
  (0, (_moveSync || _load_moveSync()).default)(oldPath, newPath);
  component.writtenPath = newPath;
}