"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.applyVersionReport = void 0;

function _defineProperty2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

  _defineProperty2 = function () {
    return data;
  };

  return data;
}

function _chalk() {
  const data = _interopRequireDefault(require("chalk"));

  _chalk = function () {
    return data;
  };

  return data;
}

function _command() {
  const data = _interopRequireDefault(require("../../command"));

  _command = function () {
    return data;
  };

  return data;
}

function _consumer() {
  const data = require("../../../api/consumer");

  _consumer = function () {
    return data;
  };

  return data;
}

function _mergeVersion() {
  const data = require("../../../consumer/versions-ops/merge-version");

  _mergeVersion = function () {
    return data;
  };

  return data;
}

const applyVersionReport = (components, addName = true, showVersion = false) => {
  const tab = addName ? '\t' : '';
  return components.map(component => {
    const name = showVersion ? component.id.toString() : component.id.toStringWithoutVersion();
    const files = Object.keys(component.filesStatus).map(file => {
      const note = component.filesStatus[file] === _mergeVersion().FileStatus.manual ? _chalk().default.white('automatic merge failed. please fix conflicts manually and then tag the results.') : '';
      return `${tab}${component.filesStatus[file]} ${_chalk().default.bold(file)} ${note}`;
    }).join('\n');
    return `${addName ? name : ''}\n${_chalk().default.cyan(files)}`;
  }).join('\n\n');
};

exports.applyVersionReport = applyVersionReport;

class Merge extends _command().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", 'merge <version> <ids...>');
    (0, _defineProperty2().default)(this, "description", `merge changes of different component versions
  the id can be used with wildcards (e.g. bit merge 0.0.1 "utils/*")`);
    (0, _defineProperty2().default)(this, "alias", '');
    (0, _defineProperty2().default)(this, "opts", [['o', 'ours', 'in case of a conflict, override the used version with the current modification'], ['t', 'theirs', 'in case of a conflict, override the current modification with the specified version'], ['m', 'manual', 'in case of a conflict, leave the files with a conflict state to resolve them manually later']]);
    (0, _defineProperty2().default)(this, "loader", true);
  }

  action([version, ids], {
    ours = false,
    theirs = false,
    manual = false
  }) {
    const mergeStrategy = (0, _mergeVersion().getMergeStrategy)(ours, theirs, manual);
    return (0, _consumer().merge)(version, ids, mergeStrategy);
  }

  report({
    components,
    version
  }) {
    // $FlowFixMe version is set in case of merge command
    const title = `successfully merged components from version ${_chalk().default.bold(version)}\n`; // $FlowFixMe components is set in case of merge command

    const componentsStr = applyVersionReport(components);
    return _chalk().default.underline(title) + _chalk().default.green(componentsStr);
  }

}

exports.default = Merge;