'use strict';

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

var _table;

function _load_table() {
  return _table = require('table');
}

var _chalk;

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

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

// const NAME_COLUMN_WIDTH = 100;
// const DESCRIPTION_COLUMN_WIDTH = 30;

var summeryTableColumnConfig = {
  columnDefault: {
    alignment: 'left'
  }
};

function _formatStatusCell(status) {
  if (status) {
    return (_chalk || _load_chalk()).default.green('passed');
  }
  return (_chalk || _load_chalk()).default.red('failed');
}

function _createSummeryRow(examineResult) {
  var meta = examineResult.diagnosisMetaData;
  var status = _formatStatusCell(examineResult.bareResult.valid);
  return [meta.category, meta.name, meta.description, status];
}

function _createSummeryTable(examineResult) {
  var header = [(_chalk || _load_chalk()).default.bold('category'), (_chalk || _load_chalk()).default.bold('name'), (_chalk || _load_chalk()).default.bold('description'), (_chalk || _load_chalk()).default.bold('status')];
  var rows = examineResult.map(_createSummeryRow);
  rows.unshift(header);
  var output = (0, (_table || _load_table()).table)(rows, summeryTableColumnConfig);
  return output;
}

function _createSummerySection(examineResult) {
  // A placeholder if we will decide we want a title
  var title = (_chalk || _load_chalk()).default.underline('');
  var summeryTable = _createSummeryTable(examineResult);
  return `${title}\n${summeryTable}`;
}

function _createFullReportForDiagnosis(examineResult) {
  if (examineResult.bareResult.valid) {
    return '';
  }
  var title = (_chalk || _load_chalk()).default.underline(examineResult.diagnosisMetaData.name);
  var symptomsTitle = (_chalk || _load_chalk()).default.underline('symptoms');
  var symptomsText = examineResult.formattedSymptoms;
  var cureTitle = (_chalk || _load_chalk()).default.underline('cure');
  var cureText = examineResult.formattedManualTreat;
  return `${title}
  ${symptomsTitle}
  ${symptomsText}
  ${cureTitle}
  ${cureText}\n`;
}

function _createFullReportForDiagnoses(examineResult) {
  var fullDiagnosesReport = examineResult.map(_createFullReportForDiagnosis).join('\n');
  return fullDiagnosesReport;
}

function _createFullReportSection(examineResult) {
  var title = (_chalk || _load_chalk()).default.underline('Error report');
  var fullDiagnosesReport = _createFullReportForDiagnoses(examineResult);
  if (fullDiagnosesReport.trim() === '') {
    return '';
  }
  return `${title}
${fullDiagnosesReport}`;
}

function _createWrittenFileSection(savedFilePath) {
  if (!savedFilePath) {
    return '';
  }
  return `File written to ${savedFilePath}`;
}

function _createMetaSection(metaData) {
  return `
  bit version   : ${metaData.bitVersion}
  node version  : ${metaData.nodeVersion}
  npm version   : ${metaData.npmVersion || 'NA'}
  yarn version  : ${metaData.yarnVersion || 'NA'}
  platform      : ${metaData.platform}
  user details  : ${metaData.userDetails}
`;
}

function render(_ref) {
  var examineResults = _ref.examineResults,
      savedFilePath = _ref.savedFilePath,
      metaData = _ref.metaData;

  var meatSection = _createMetaSection(metaData);
  var summerySection = _createSummerySection(examineResults);
  var fullReportSection = _createFullReportSection(examineResults);
  var writtenFileSection = _createWrittenFileSection(savedFilePath);
  var output = `${meatSection}
${summerySection}
${fullReportSection}
${writtenFileSection}`;
  return output;
}