"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = 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 _mergeCmd() {
  const data = require("./merge-cmd");

  _mergeCmd = function () {
    return data;
  };

  return data;
}

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

  _mergeVersion = function () {
    return data;
  };

  return data;
}

function _constants() {
  const data = require("../../../constants");

  _constants = function () {
    return data;
  };

  return data;
}

class Checkout extends _command().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", 'checkout [values...]');
    (0, _defineProperty2().default)(this, "description", `switch between component versions or remove local changes
  bit checkout <version> [ids...] => checkout the specified ids (or all components when --all is used) to the specified version
  bit checkout latest [ids...] => checkout the specified ids (or all components when --all is used) to their latest versions
  bit checkout [ids...] --reset => remove local modifications from the specified ids (or all components when --all is used)
  the id can be used with wildcards (e.g. bit checkout 0.0.1 "utils/*")`);
    (0, _defineProperty2().default)(this, "alias", 'U');
    (0, _defineProperty2().default)(this, "opts", [['i', 'interactive-merge', 'when a component is modified and the merge process found conflicts, display options to resolve them'], ['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'], ['r', 'reset', 'remove local changes'], ['a', 'all', 'all components'], ['v', 'verbose', 'showing verbose output for inspection'], ['', 'skip-npm-install', 'do not install packages of the imported components'], ['', 'ignore-dist', 'do not write dist files (when exist)']]);
    (0, _defineProperty2().default)(this, "loader", true);
  }

  action([values], {
    interactiveMerge = false,
    ours = false,
    theirs = false,
    manual = false,
    reset = false,
    all = false,
    verbose = false,
    skipNpmInstall = false,
    ignoreDist = false
  }) {
    const checkoutProps = {
      promptMergeOptions: interactiveMerge,
      mergeStrategy: (0, _mergeVersion().getMergeStrategy)(ours, theirs, manual),
      reset,
      all,
      verbose,
      skipNpmInstall,
      ignoreDist
    };
    return (0, _consumer().checkout)(values, checkoutProps);
  }

  report({
    components,
    version,
    failedComponents
  }) {
    const isLatest = Boolean(version && version === _constants().LATEST);
    const isReset = !version;

    const getFailureOutput = () => {
      if (!failedComponents || !failedComponents.length) return '';
      const title = 'the checkout has been canceled on the following component(s)';
      const body = failedComponents.map(failedComponent => `${_chalk().default.bold(failedComponent.id.toString())} - ${_chalk().default.red(failedComponent.failureMessage)}`).join('\n');
      return `${title}\n${body}\n\n`;
    };

    const getSuccessfulOutput = () => {
      if (!components || !components.length) return '';

      if (components.length === 1) {
        const component = components[0];
        const componentName = isReset ? component.id.toString() : component.id.toStringWithoutVersion();
        if (isReset) return `successfully reset ${_chalk().default.bold(componentName)}\n`;
        const title = `successfully switched ${_chalk().default.bold(componentName)} to version ${_chalk().default.bold( // $FlowFixMe version is defined when !isReset
        isLatest ? component.id.version : version)}\n`;
        return `${title} ${(0, _mergeCmd().applyVersionReport)(components, false)}`;
      }

      if (isReset) {
        const title = 'successfully reset the following components\n\n';
        const body = components.map(component => _chalk().default.bold(component.id.toString())).join('\n');
        return title + body;
      } // $FlowFixMe version is defined when !isReset


      const versionOutput = isLatest ? 'their latest version' : `version ${_chalk().default.bold(version)}`;
      const title = `successfully switched the following components to ${versionOutput}\n\n`;
      const showVersion = isLatest || isReset;
      const componentsStr = (0, _mergeCmd().applyVersionReport)(components, true, showVersion);
      return title + componentsStr;
    };

    const failedOutput = getFailureOutput();
    const successOutput = getSuccessfulOutput();
    return failedOutput + successOutput;
  }

}

exports.default = Checkout;