'use strict';

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

var _regenerator;

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

var _execa;

function _load_execa() {
  return _execa = _interopRequireDefault(require('execa'));
}

var _generalError;

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

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

/**
 * use git `merge-file` command. From the command help:
 * `git merge-file <current-file> <base-file> <other-file>
 * git merge-file incorporates all changes that lead from the <base-file> to <other-file> into
 * <current-file>. The result ordinarily goes into <current-file>.`
 *
 * Here, we are not going to write the result into current-file. Instead, we'll use the "-p" flag,
 * to just return the results.
 */
exports.default = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(_ref) {
    var filePath = _ref.filePath,
        currentFile = _ref.currentFile,
        baseFile = _ref.baseFile,
        otherFile = _ref.otherFile;
    var mergeResult, result;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            mergeResult = { filePath, output: null, conflict: null };
            _context.prev = 1;
            _context.next = 4;
            return (0, (_execa || _load_execa()).default)('git', ['merge-file', '-L', currentFile.label, '-L', 'Base File', '-L', otherFile.label, currentFile.path, baseFile.path, otherFile.path, '-p']);

          case 4:
            result = _context.sent;

            mergeResult.output = result.stdout;
            return _context.abrupt('return', mergeResult);

          case 9:
            _context.prev = 9;
            _context.t0 = _context['catch'](1);

            if (!(_context.t0.code && Number.isInteger(_context.t0.code) && _context.t0.stdout)) {
              _context.next = 14;
              break;
            }

            mergeResult.conflict = _context.t0.stdout;
            return _context.abrupt('return', mergeResult);

          case 14:
            if (!(_context.t0.code === 'ENOENT')) {
              _context.next = 16;
              break;
            }

            throw new (_generalError || _load_generalError()).default('unable to run git merge-file command, please make sure you have git installed');

          case 16:
            throw _context.t0;

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

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

  return mergeFiles;
}();