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

class Diff extends _command().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", 'diff [values...]');
    (0, _defineProperty2().default)(this, "description", `show diff between components files
  bit diff => compare all modified components to their model version
  bit diff [ids...] => compare the specified components against their modified states
  bit diff [id] [version] => compare the specified version to used or modified files
  bit diff [id] [version] [to_version] => compare the specified version files to to_version files
  the id can be used with wildcards (e.g. bit diff "utils/*")`);
    (0, _defineProperty2().default)(this, "alias", '');
    (0, _defineProperty2().default)(this, "opts", [['v', 'verbose', 'show a more verbose output when possible']]);
    (0, _defineProperty2().default)(this, "loader", true);
  }

  action([values], {
    verbose = false
  }) {
    return (0, _consumer().diff)(values, verbose);
  }

  report(diffResults) {
    return diffResults.map(diffResult => {
      if (diffResult.hasDiff) {
        const titleStr = `showing diff for ${_chalk().default.bold(diffResult.id.toStringWithoutVersion())}`;
        const titleSeparator = new Array(titleStr.length).fill('-').join('');

        const title = _chalk().default.cyan(`${titleSeparator}\n${titleStr}\n${titleSeparator}`); // $FlowFixMe


        const filesWithDiff = diffResult.filesDiff.filter(file => file.diffOutput);
        const files = filesWithDiff.map(fileDiff => fileDiff.diffOutput).join('\n');
        const fields = diffResult.fieldsDiff ? diffResult.fieldsDiff.map(field => field.diffOutput).join('\n') : '';
        return `${title}\n${files}\n${fields}`;
      }

      return `no diff for ${_chalk().default.bold(diffResult.id.toString())}`;
    }).join('\n\n');
  }

}

exports.default = Diff;