"use strict";

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

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

function _table() {
  const data = require("table");

  _table = function () {
    return data;
  };

  return data;
}

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

  _chalk = function () {
    return data;
  };

  return data;
}

// const NAME_COLUMN_WIDTH = 100;
// const DESCRIPTION_COLUMN_WIDTH = 30;
const summeryTableColumnConfig = {
  columnDefault: {
    alignment: 'left'
  }
};

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

  return _chalk().default.red('failed');
}

function _createSummeryRow(examineResult) {
  const meta = examineResult.diagnosisMetaData;

  const status = _formatStatusCell(examineResult.bareResult.valid);

  return [meta.category, meta.name, meta.description, status];
}

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

function _createSummerySection(examineResult) {
  // A placeholder if we will decide we want a title
  const title = _chalk().default.underline('');

  const summeryTable = _createSummeryTable(examineResult);

  return `${title}\n${summeryTable}`;
}

function _createFullReportForDiagnosis(examineResult) {
  if (examineResult.bareResult.valid) {
    return '';
  }

  const title = _chalk().default.underline(examineResult.diagnosisMetaData.name);

  const symptomsTitle = _chalk().default.underline('symptoms');

  const symptomsText = examineResult.formattedSymptoms;

  const cureTitle = _chalk().default.underline('cure');

  const cureText = examineResult.formattedManualTreat;
  return `${title}
  ${symptomsTitle}
  ${symptomsText}
  ${cureTitle}
  ${cureText}\n`;
}

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

function _createFullReportSection(examineResult) {
  const title = _chalk().default.underline('Error report');

  const 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({
  examineResults,
  savedFilePath,
  metaData
}) {
  const meatSection = _createMetaSection(metaData);

  const summerySection = _createSummerySection(examineResults);

  const fullReportSection = _createFullReportSection(examineResults);

  const writtenFileSection = _createWrittenFileSection(savedFilePath);

  const output = `${meatSection}
${summerySection}
${fullReportSection}
${writtenFileSection}`;
  return output;
}