'use strict';

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

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _chalk;

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

var _ttyTable;

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

var _chalkBox;

function _load_chalkBox() {
  return _chalkBox = require('../chalk-box');
}

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

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

var paintExample = function paintExample(example) {
  return example.raw;
};

var paintExamples = function paintExamples(examples) {
  if ((_ramda || _load_ramda()).default.isEmpty(examples) || (_ramda || _load_ramda()).default.isNil(examples)) {
    return '';
  }

  return `\n${(0, (_chalkBox || _load_chalkBox()).paintHeader)('Examples')}\n${examples.map(paintExample).join('\n')}`;
};

var paintDoc = exports.paintDoc = function paintDoc(doc) {
  var name = doc.name,
      description = doc.description,
      args = doc.args,
      returns = doc.returns;


  var header = [{ value: 'Name', width: 20, headerColor: 'cyan', headerAlign: 'left' }, { value: `${name}`, width: 50, headerColor: 'white', color: 'white', headerAlign: 'left' }];
  var opts = {
    align: 'left'
  };

  var table = new (_ttyTable || _load_ttyTable()).default(header, [], opts);

  var painArg = function painArg(arg) {
    if (!arg.type && !arg.name) {
      return '';
    }
    if (!arg.type) {
      return `${arg.name}`;
    }
    return `${arg.name}: ${arg.type}`;
  };

  var painDescription = function painDescription(arg) {
    if (!arg.type) {
      return '';
    }
    if (arg.type && !arg.description) {
      return arg.type;
    }
    return `${arg.type} -> ${arg.description}`;
  };

  var rows = [[(_chalk || _load_chalk()).default.cyan('Description'), description], [(_chalk || _load_chalk()).default.cyan('Args'), `(${args.map(painArg).join(', ')})`], [(_chalk || _load_chalk()).default.cyan('Returns'), painDescription(returns)]].filter(function (x) {
    return x;
  });

  // console.log('rows', rows);

  table.push.apply(table, _toConsumableArray(rows));
  return table.render() + paintExamples(doc.examples);
};

exports.default = function (docs) {
  if ((_ramda || _load_ramda()).default.isEmpty(docs) || (_ramda || _load_ramda()).default.isNil(docs)) {
    return '\nNo documentation found';
  }
  // $FlowFixMe
  return `\n${(0, (_chalkBox || _load_chalkBox()).paintHeader)('Documentation')}${docs.map(paintDoc).join('')}`;
};