'use strict';

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

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 _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,
      properties = doc.properties;


  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 paintArg = function paintArg(arg) {
    if (!arg && !arg.type && !arg.name) {
      return '';
    }
    if (!arg.type) {
      return `${arg.name}`;
    }
    return `${arg.name}: ${arg.type}`;
  };

  var paintArgs = function paintArgs() {
    if (!args || !args.length) return '';
    return `(${args.map(paintArg).join(', ')})`;
  };

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

  var paintProperties = function paintProperties() {
    if (!properties || !properties.length) return '';
    return `(${properties.map(paintArg).join(', ')})`;
  };

  var rows = [[(_chalk || _load_chalk()).default.cyan('Description'), description], [(_chalk || _load_chalk()).default.cyan('Args'), paintArgs()], [(_chalk || _load_chalk()).default.cyan('Returns'), paintDescription(returns)], [(_chalk || _load_chalk()).default.cyan('Properties'), paintProperties()]].filter(function (_ref) {
    var _ref2 = _slicedToArray(_ref, 2),
        x = _ref2[1];

    return x;
  });

  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('')}`;
};