'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.paintCiResults = exports.paintBuildResults = exports.paintSummarySpecsResults = exports.paintAllSpecsResults = exports.paintSpecsResults = exports.paintLog = exports.paintHeader = exports.paintBitProp = exports.formatBitString = exports.formatPlainComponentItemWithVersions = exports.formatPlainComponentItem = exports.formatBit = exports.formatNewBit = undefined;

var _chalk;

function _load_chalk() {
  return _chalk = _interopRequireDefault(require('chalk'));
}

var _ttyTable;

function _load_ttyTable() {
  return _ttyTable = _interopRequireDefault(require('tty-table'));
}

var _specsResults;

function _load_specsResults() {
  return _specsResults = _interopRequireDefault(require('../consumer/specs-results/specs-results'));
}

var _consumerComponent;

function _load_consumerComponent() {
  return _consumerComponent = _interopRequireDefault(require('../consumer/component/consumer-component'));
}

var _mergeVersion;

function _load_mergeVersion() {
  return _mergeVersion = require('../consumer/versions-ops/merge-version/merge-version');
}

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

var formatNewBit = exports.formatNewBit = function formatNewBit(_ref) {
  var box = _ref.box,
      name = _ref.name;
  return (_chalk || _load_chalk()).default.white('     > ') + (_chalk || _load_chalk()).default.cyan(`${box}/${name}`);
};
var formatBit = exports.formatBit = function formatBit(_ref2) {
  var scope = _ref2.scope,
      box = _ref2.box,
      name = _ref2.name,
      version = _ref2.version;
  return (_chalk || _load_chalk()).default.white('     > ') + (_chalk || _load_chalk()).default.cyan(`${scope ? `${scope}/` : ''}${box}/${name} - ${version ? version.toString() : 'latest'}`);
};

var formatPlainComponentItem = exports.formatPlainComponentItem = function formatPlainComponentItem(_ref3) {
  var scope = _ref3.scope,
      box = _ref3.box,
      name = _ref3.name,
      version = _ref3.version,
      deprecated = _ref3.deprecated;
  return (_chalk || _load_chalk()).default.cyan(`- ${scope ? `${scope}/` : ''}${box}/${name}@${version ? version.toString() : 'latest'}  ${deprecated ? (_chalk || _load_chalk()).default.yellow('[deprecated]') : ''}`);
};

var formatPlainComponentItemWithVersions = exports.formatPlainComponentItemWithVersions = function formatPlainComponentItemWithVersions(component, importDetails) {
  var status = importDetails.status;
  var id = component.id.toStringWithoutVersion();
  var versions = importDetails.versions.length ? `new versions: ${importDetails.versions.join(', ')}` : '';
  // $FlowFixMe component.version should be set here
  var usedVersion = status === 'added' ? `, currently used version ${component.version}` : '';
  var getConflictMessage = function getConflictMessage() {
    if (!importDetails.filesStatus) return '';
    var conflictedFiles = Object.keys(importDetails.filesStatus) // $FlowFixMe file is set
    .filter(function (file) {
      return importDetails.filesStatus[file] === (_mergeVersion || _load_mergeVersion()).FileStatus.manual;
    });
    if (!conflictedFiles.length) return '';
    return `(the following files were saved with conflicts ${conflictedFiles.map(function (file) {
      return (_chalk || _load_chalk()).default.bold(file);
    }).join(', ')}) `;
  };
  var deprecated = component.deprecated ? (_chalk || _load_chalk()).default.yellow('deprecated') : '';
  return `- ${(_chalk || _load_chalk()).default.green(status)} ${(_chalk || _load_chalk()).default.cyan(id)} ${versions}${usedVersion} ${getConflictMessage()}${deprecated}`;
};

var formatBitString = exports.formatBitString = function formatBitString(bit) {
  return (_chalk || _load_chalk()).default.white('     > ') + (_chalk || _load_chalk()).default.cyan(`${bit}`);
};

var paintBitProp = exports.paintBitProp = function paintBitProp(key, value) {
  if (!value) return '';
  return `${(_chalk || _load_chalk()).default.magenta(key)} -> ${value}\n`;
};

var paintHeader = exports.paintHeader = function paintHeader(value) {
  if (!value) return '';
  return `${(_chalk || _load_chalk()).default.underline(value)}\n`;
};

var paintAuthor = function paintAuthor(email, username) {
  if (email && username) {
    return (_chalk || _load_chalk()).default.white(`author: ${username} <${email}>\n`);
  } else if (email && !username) {
    return (_chalk || _load_chalk()).default.white(`author: <${email}>\n`);
  } else if (!email && username) {
    return (_chalk || _load_chalk()).default.white(`author: ${username}\n`);
  }

  return '';
};

var paintLog = exports.paintLog = function paintLog(_ref4) {
  var message = _ref4.message,
      date = _ref4.date,
      tag = _ref4.tag,
      username = _ref4.username,
      email = _ref4.email;

  return (_chalk || _load_chalk()).default.yellow(`tag ${tag}\n`) + paintAuthor(email, username) + (_chalk || _load_chalk()).default.white(`date: ${date}\n`) + (message ? (_chalk || _load_chalk()).default.white(`\n      ${message}\n`) : '');
};

var successTest = function successTest(test) {
  return `✔   ${(_chalk || _load_chalk()).default.white(test.title)} - ${(_chalk || _load_chalk()).default.cyan(`${test.duration}ms`)}`;
};

var failureTest = function failureTest(test) {
  return `❌   ${(_chalk || _load_chalk()).default.white(test.title)} - ${(_chalk || _load_chalk()).default.cyan(`${test.duration}ms`)}
    ${(_chalk || _load_chalk()).default.red(test.err.message)}`;
};

var paintMissingTester = function paintMissingTester(componentId) {
  var componentIdBold = (_chalk || _load_chalk()).default.bold(componentId);
  return (_chalk || _load_chalk()).default.bold.red(`tester for component: ${componentIdBold} is not defined`);
};

var paintTest = function paintTest(test) {
  return test.pass ? successTest(test) : failureTest(test);
};

// Failures which are not on tests, for example on before blocks
var paintGeneralFailure = function paintGeneralFailure(failure) {
  var duration = failure.duration ? ` - ${(_chalk || _load_chalk()).default.cyan(`${failure.duration}ms`)}` : '';
  return `❌   ${(_chalk || _load_chalk()).default.white(failure.title)} ${duration}
    ${(_chalk || _load_chalk()).default.red(failure.err.message)}`;
};

var paintStats = function paintStats(results) {
  var statsHeader = results.pass ? (_chalk || _load_chalk()).default.underline.green('\ntests passed') : (_chalk || _load_chalk()).default.underline.red('\ntests failed');
  var fileName = results.specFile ? (_chalk || _load_chalk()).default.white(`\nfile: ${results.specFile}`) : '';
  var totalDuration = results.stats && results.stats.duration !== undefined ? `total duration - ${(_chalk || _load_chalk()).default.cyan(`${results.stats.duration}ms\n`)}` : '';
  return `${statsHeader}${fileName}\n${totalDuration}\n`;
};

var paintSpecsResults = exports.paintSpecsResults = function paintSpecsResults(results) {
  if (!results) return [];
  return results.map(function (specResult) {
    var stats = paintStats(specResult);
    var tests = specResult.tests ? `${specResult.tests.map(paintTest).join('\n')}\n` : '';
    var failures = specResult.failures ? `${specResult.failures.map(paintGeneralFailure).join('\n')}\n` : '';
    var final = tests || failures ? stats + tests + failures : '';
    return final;
  });
};

var paintAllSpecsResults = exports.paintAllSpecsResults = function paintAllSpecsResults(results) {
  if (results.length === 0) return (_chalk || _load_chalk()).default.yellow('nothing to test');
  return results.map(function (result) {
    if (result.missingTester) return paintMissingTester(result.componentId);
    var componentId = (_chalk || _load_chalk()).default.bold(result.componentId);
    if (result.specs) return componentId + paintSpecsResults(result.specs);
    return (_chalk || _load_chalk()).default.yellow(`tests are not defined for component: ${componentId}`);
  }).join('\n');
};

var paintSummarySpecsResults = exports.paintSummarySpecsResults = function paintSummarySpecsResults(results) {
  if (results.length <= 1) return ''; // it there are no results or only one result, no need for summary
  var summaryHeader = [];
  summaryHeader.push({ value: 'Component ID', width: 80, headerColor: 'cyan' });
  summaryHeader.push({ value: 'Specs Results', width: 50, headerColor: 'cyan' });
  var specsSummary = function specsSummary(specResults) {
    var specsPassed = specResults.map(function (specResult) {
      return specResult.pass;
    });
    var areAllPassed = specsPassed.every(function (isPassed) {
      return isPassed;
    });
    return areAllPassed ? (_chalk || _load_chalk()).default.green('passed') : (_chalk || _load_chalk()).default.red('failed');
  };
  var summaryRows = results.map(function (result) {
    var componentId = (_chalk || _load_chalk()).default.bold(result.componentId);
    if (result.missingTester) return [componentId, (_chalk || _load_chalk()).default.bold.red('tester is not defined')];
    if (result.specs) return [componentId, specsSummary(result.specs)];
    return [componentId, (_chalk || _load_chalk()).default.yellow('tests are not defined')];
  });

  var summaryTable = new (_ttyTable || _load_ttyTable()).default(summaryHeader, summaryRows);
  return summaryTable.render();
};

var paintBuildResults = exports.paintBuildResults = function paintBuildResults(buildResults) {
  if (buildResults) {
    var statsHeader = (_chalk || _load_chalk()).default.underline.green('\nbuilt Files:\n');
    return statsHeader + buildResults.map(function (file) {
      return `${(_chalk || _load_chalk()).default.cyan(`${file.path}`)}`;
    }).join('\n');
  }
  return '';
};
var paintCiResults = exports.paintCiResults = function paintCiResults(_ref5) {
  var dists = _ref5.dists,
      specsResults = _ref5.specsResults;

  return paintBuildResults(dists) + paintSpecsResults(specsResults);
};