'use strict';

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

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _command;

function _load_command() {
  return _command = _interopRequireDefault(require('../../command'));
}

var _doctor;

function _load_doctor() {
  return _doctor = _interopRequireDefault(require('../../../api/consumer/lib/doctor'));
}

var _doctor2;

function _load_doctor2() {
  return _doctor2 = require('../../../api/consumer/lib/doctor');
}

var _diagnosisListTemplate;

function _load_diagnosisListTemplate() {
  return _diagnosisListTemplate = _interopRequireDefault(require('../../templates/diagnosis-list-template'));
}

var _doctorResultsTemplate;

function _load_doctorResultsTemplate() {
  return _doctorResultsTemplate = _interopRequireDefault(require('../../templates/doctor-results-template'));
}

var _diagnosis;

function _load_diagnosis() {
  return _diagnosis = _interopRequireDefault(require('../../../doctor/diagnosis'));
}

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var Doctor = function (_Command) {
  _inherits(Doctor, _Command);

  function Doctor() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, Doctor);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Doctor.__proto__ || Object.getPrototypeOf(Doctor)).call.apply(_ref, [this].concat(args))), _this), _this.name = 'doctor [diagnosis-name]', _this.description = 'diagnose a bit workspace', _this.alias = '', _this.opts = [['j', 'json', 'return diagnoses in json format'], ['', 'list', 'list all available diagnoses'], ['s', 'save [filePath]', 'save diagnoses to a file']], _this.migration = false, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(Doctor, [{
    key: 'action',
    value: function action(_ref2, _ref3) {
      var _ref4 = _slicedToArray(_ref2, 1),
          diagnosisName = _ref4[0];

      var _ref3$list = _ref3.list,
          list = _ref3$list === undefined ? false : _ref3$list,
          save = _ref3.save;

      if (list) {
        return (0, (_doctor2 || _load_doctor2()).listDiagnoses)();
      }
      var filePath = save;
      // Happen when used --save without specify the location
      if (save === true) {
        filePath = '.';
      }
      if (diagnosisName) {
        return (0, (_doctor2 || _load_doctor2()).runOne)({ diagnosisName, filePath });
      }
      return (0, (_doctor || _load_doctor()).default)({ filePath });
    }
  }, {
    key: 'report',
    value: function 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);
    }
  }]);

  return Doctor;
}((_command || _load_command()).default);

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`);
  var formatted = (0, (_diagnosisListTemplate || _load_diagnosisListTemplate()).default)(res);
  return formatted;
}

function _runOneReport(res, json) {
  var examineResult = res.examineResult,
      savedFilePath = res.savedFilePath,
      metaData = res.metaData;

  if (json) {
    var fullJson = {
      savedFilePath,
      examineResult
    };
    return JSON.stringify(fullJson, null, 2);
  }
  var formatted = (0, (_doctorResultsTemplate || _load_doctorResultsTemplate()).default)({ examineResults: [examineResult], savedFilePath, metaData });
  return formatted;
}

function _runAllReport(res, json) {
  var examineResults = res.examineResults,
      savedFilePath = res.savedFilePath,
      metaData = res.metaData;

  if (json) {
    var fullJson = {
      savedFilePath,
      examineResults
    };
    return JSON.stringify(fullJson, null, 2);
  }
  var formatted = (0, (_doctorResultsTemplate || _load_doctorResultsTemplate()).default)({ examineResults, savedFilePath, metaData });
  return formatted;
}