"use strict";

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

var _lodash = _interopRequireDefault(require("lodash"));

var _iterateJsdoc = _interopRequireDefault(require("../iterateJsdoc"));

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

var _default = (0, _iterateJsdoc.default)(({
  sourceCode,
  jsdoc,
  report,
  utils,
  context,
  settings,
  jsdocNode
}) => {
  if (!jsdoc.tags) {
    return;
  }

  const _ref = context.options[0] || {},
        _ref$definedTags = _ref.definedTags,
        definedTags = _ref$definedTags === void 0 ? [] : _ref$definedTags;

  let definedPreferredTags = [];
  let definedNonPreferredTags = [];
  const tagNamePreference = settings.tagNamePreference;

  if (Object.keys(tagNamePreference).length) {
    definedNonPreferredTags = _lodash.default.keys(tagNamePreference); // Replace `_.values` with `Object.values` when we may start requiring Node 7+

    definedPreferredTags = _lodash.default.values(tagNamePreference).map(preferredTag => {
      if (typeof preferredTag === 'string') {
        // May become an empty string but will be filtered out below
        return preferredTag;
      }

      if (!preferredTag) {
        return undefined;
      }

      if (typeof preferredTag !== 'object') {
        utils.reportSettings('Invalid `settings.jsdoc.tagNamePreference`. Values must be falsy, a string, or an object.');
      }

      return preferredTag.replacement;
    }).filter(preferredType => {
      return preferredType;
    });
  }

  jsdoc.tags.forEach(jsdocTag => {
    const tagName = jsdocTag.tag;

    if (utils.isValidTag(tagName, [...definedTags, ...definedPreferredTags, ...definedNonPreferredTags])) {
      let preferredTagName = utils.getPreferredTagName({
        allowObjectReturn: true,
        defaultMessage: `Blacklisted tag found (\`@${tagName}\`)`,
        tagName
      });
      let message = `Invalid JSDoc tag (preference). Replace "${tagName}" JSDoc tag with "${preferredTagName}".`;

      if (!preferredTagName) {
        return;
      }

      if (typeof preferredTagName === 'object') {
        var _preferredTagName = preferredTagName;
        message = _preferredTagName.message;
        preferredTagName = _preferredTagName.replacement;
      }

      if (preferredTagName !== tagName) {
        report(message, fixer => {
          const replacement = sourceCode.getText(jsdocNode).replace(new RegExp(`@${_lodash.default.escapeRegExp(tagName)}\\b`, 'u'), `@${preferredTagName}`);
          return fixer.replaceText(jsdocNode, replacement);
        }, jsdocTag);
      }
    } else {
      report(`Invalid JSDoc tag name "${tagName}".`, null, jsdocTag);
    }
  });
}, {
  iterateAllJsdocs: true,
  meta: {
    fixable: 'code',
    schema: [{
      additionalProperties: false,
      properties: {
        definedTags: {
          items: {
            type: 'string'
          },
          type: 'array'
        }
      },
      type: 'object'
    }],
    type: 'suggestion'
  }
});

exports.default = _default;
module.exports = exports.default;
//# sourceMappingURL=checkTagNames.js.map