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

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

var _gitNotFound;

function _load_gitNotFound() {
  return _gitNotFound = _interopRequireDefault(require('./git/exceptions/git-not-found'));
}

var _gitExecutable;

function _load_gitExecutable() {
  return _gitExecutable = _interopRequireDefault(require('./git/git-executable'));
}

var _logger;

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

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

/**
 * 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 _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(_ref2) {
    var filePath = _ref2.filePath,
        currentFile = _ref2.currentFile,
        baseFile = _ref2.baseFile,
        otherFile = _ref2.otherFile;
    var mergeResult, gitExecutablePath, 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 };
            gitExecutablePath = (0, (_gitExecutable || _load_gitExecutable()).default)();
            _context.prev = 2;
            _context.next = 5;
            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 5:
            result = _context.sent;

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

          case 10:
            _context.prev = 10;
            _context.t0 = _context['catch'](2);

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

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

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

            (_logger || _load_logger()).default.error(`failed running Git at ${gitExecutablePath}. full command: ${_context.t0.cmd}`);
            throw new (_gitNotFound || _load_gitNotFound()).default(gitExecutablePath, _context.t0);

          case 18:
            throw _context.t0;

          case 19:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this, [[2, 10]]);
  }));

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

  return mergeFiles;
}();