"use strict";

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

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

function _ramda() {
  const data = _interopRequireDefault(require("ramda"));

  _ramda = function () {
    return data;
  };

  return data;
}

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

  _chalk = function () {
    return data;
  };

  return data;
}

function _ttyTable() {
  const data = _interopRequireDefault(require("tty-table"));

  _ttyTable = function () {
    return data;
  };

  return data;
}

function _chalkBox() {
  const data = require("../chalk-box");

  _chalkBox = function () {
    return data;
  };

  return data;
}

const paintExample = example => {
  return example.raw;
};

const paintExamples = examples => {
  if (_ramda().default.isEmpty(examples) || _ramda().default.isNil(examples)) {
    return '';
  }

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

const paintDoc = doc => {
  const {
    name,
    description,
    args,
    returns,
    properties
  } = doc;
  const header = [{
    value: 'Name',
    width: 20,
    headerColor: 'cyan',
    headerAlign: 'left'
  }, {
    value: `${name}`,
    width: 50,
    headerColor: 'white',
    color: 'white',
    headerAlign: 'left'
  }];
  const opts = {
    align: 'left'
  };
  const table = new (_ttyTable().default)(header, [], opts);

  const paintArg = arg => {
    if (!arg && !arg.type && !arg.name) {
      return '';
    }

    if (!arg.type) {
      return `${arg.name}`;
    }

    return `${arg.name}: ${arg.type}`;
  };

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

  const paintDescription = arg => {
    if (!arg) return '';

    if (!arg.type) {
      return '';
    }

    if (arg && arg.type && !arg.description) {
      return arg.type;
    }

    return `${arg.type} -> ${arg.description}`;
  };

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

  const rows = [[_chalk().default.cyan('Description'), description], [_chalk().default.cyan('Args'), paintArgs()], [_chalk().default.cyan('Returns'), paintDescription(returns)], [_chalk().default.cyan('Properties'), paintProperties()]].filter(([, x]) => x);
  table.push(...rows);
  return table.render() + paintExamples(doc.examples);
};

exports.paintDoc = paintDoc;

var _default = docs => {
  if (_ramda().default.isEmpty(docs) || _ramda().default.isNil(docs)) {
    return '\nNo documentation found';
  } // $FlowFixMe


  return `\n${(0, _chalkBox().paintHeader)('Documentation')}${docs.map(paintDoc).join('')}`;
};

exports.default = _default;