'use strict';

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

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _iterateJsdoc = require('../iterateJsdoc');

var _iterateJsdoc2 = _interopRequireDefault(_iterateJsdoc);

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

var extractParagraphs = function extractParagraphs(text) {
  return text.split(/\n\n/);
};

var isNewLinePrecededByAPeriod = function isNewLinePrecededByAPeriod(text) {
  var lastLineEndsSentence = void 0;

  var lines = text.split('\n');

  return !_lodash2.default.some(lines, function (line) {
    if (_lodash2.default.isBoolean(lastLineEndsSentence) && !lastLineEndsSentence && /^[A-Z]/.test(line)) {
      return true;
    }

    lastLineEndsSentence = /\.$/.test(line);

    return false;
  });
};

var isCapitalized = function isCapitalized(str) {
  return str[0] === str[0].toUpperCase();
};

var validateDescription = function validateDescription(description, report) {
  if (!description) {
    return false;
  }

  var paragraphs = extractParagraphs(description);

  return _lodash2.default.some(paragraphs, function (paragraph, index) {
    if (!isCapitalized(paragraph)) {
      if (index === 0) {
        report('Description must start with an uppercase character.');
      } else {
        report('Paragraph must start with an uppercase character.');
      }

      return true;
    }

    if (!/\.$/.test(paragraph)) {
      report('Sentence must end with a period.');

      return true;
    }

    if (!isNewLinePrecededByAPeriod(paragraph)) {
      report('A line of text is started with an uppercase character, but preceding line does not end the sentence.');

      return true;
    }

    return false;
  });
};

exports.default = (0, _iterateJsdoc2.default)(function (_ref) {
  var jsdoc = _ref.jsdoc,
      report = _ref.report;

  if (validateDescription(jsdoc.description, report)) {
    return;
  }

  var tags = _lodash2.default.filter(jsdoc.tags, function (tag) {
    return _lodash2.default.includes(['param', 'returns'], tag.tag);
  });

  _lodash2.default.some(tags, function (tag) {
    var description = _lodash2.default.trimStart(tag.description, '- ');

    return validateDescription(description, report);
  });
});
module.exports = exports['default'];
//# sourceMappingURL=requireDescriptionCompleteSentence.js.map