"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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 _command() {
  const data = _interopRequireDefault(require("../../command"));

  _command = function () {
    return data;
  };

  return data;
}

function _doctor() {
  const data = _interopRequireWildcard(require("../../../api/consumer/lib/doctor"));

  _doctor = function () {
    return data;
  };

  return data;
}

function _diagnosisListTemplate() {
  const data = _interopRequireDefault(require("../../templates/diagnosis-list-template"));

  _diagnosisListTemplate = function () {
    return data;
  };

  return data;
}

function _doctorResultsTemplate() {
  const data = _interopRequireDefault(require("../../templates/doctor-results-template"));

  _doctorResultsTemplate = function () {
    return data;
  };

  return data;
}

function _diagnosis() {
  const data = _interopRequireDefault(require("../../../doctor/diagnosis"));

  _diagnosis = function () {
    return data;
  };

  return data;
}

class Doctor extends _command().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", 'doctor [diagnosis-name]');
    (0, _defineProperty2().default)(this, "description", 'diagnose a bit workspace');
    (0, _defineProperty2().default)(this, "alias", '');
    (0, _defineProperty2().default)(this, "opts", [['j', 'json', 'return diagnoses in json format'], ['', 'list', 'list all available diagnoses'], ['s', 'save [filePath]', 'save diagnoses to a file']]);
    (0, _defineProperty2().default)(this, "migration", false);
  }

  action([diagnosisName], {
    list = false,
    save
  }) {
    if (list) {
      return (0, _doctor().listDiagnoses)();
    }

    let filePath = save; // Happen when used --save without specify the location

    if (save === true) {
      filePath = '.';
    }

    if (diagnosisName) {
      return (0, _doctor().runOne)({
        diagnosisName,
        filePath
      });
    }

    return (0, _doctor().default)({
      filePath
    });
  }

  report(res, args, flags) {
    if (flags.list) {
      return _listReport(res, flags.json);
    }

    if (args && args[0]) {
      return _runOneReport(res, flags.json);
    }

    return _runAllReport(res, flags.json);
  }

}

exports.default = Doctor;

function _listReport(res, json) {
  if (json) {
    return JSON.stringify(res, null, 2);
  } // const formatted = res.map(diagnosis => `${diagnosis.name}   ${diagnosis.description}\n`);


  const formatted = (0, _diagnosisListTemplate().default)(res);
  return formatted;
}

function _runOneReport(res, json) {
  const {
    examineResult,
    savedFilePath,
    metaData
  } = res;

  if (json) {
    const fullJson = {
      savedFilePath,
      examineResult
    };
    return JSON.stringify(fullJson, null, 2);
  }

  const formatted = (0, _doctorResultsTemplate().default)({
    examineResults: [examineResult],
    savedFilePath,
    metaData
  });
  return formatted;
}

function _runAllReport(res, json) {
  const {
    examineResults,
    savedFilePath,
    metaData
  } = res;

  if (json) {
    const fullJson = {
      savedFilePath,
      examineResults
    };
    return JSON.stringify(fullJson, null, 2);
  }

  const formatted = (0, _doctorResultsTemplate().default)({
    examineResults,
    savedFilePath,
    metaData
  });
  return formatted;
}