"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeDefinition = void 0;
const striptags_1 = __importDefault(require("striptags"));
const EMPHASIS_TAGS = ['a', 'b', 'em', 'internalXref'];
const normalizeHtmlTags = (definition) => (0, striptags_1.default)((0, striptags_1.default)(definition, EMPHASIS_TAGS), undefined, '"');
const normalizeLineBreaks = (definition) => definition.replace(/\r\n/g, '\n');
const normalizeWhitespace = (definition) => definition.replace(/[^\S\n ]+/g, ' ').replace(/[ ]+/g, ' ');
const normalizeQuotes = (definition) => definition.replace(/\."/g, '".');
/**
 * `(1.2) definition` -> `definition`
 */
const normalizeMarkers = (definition) => definition.replace(/^\(\d+\.\d+\)\s+/g, '');
const normalizeTrailingSymbols = (definition) => definition.trim().replace(/:$/, '');
const normalizeLeadingSymbols = (definition) => definition.trim().replace(/^:/, '');
/**
 * @see https://stackoverflow.com/a/40732368
 */
const normalizeNonWords = (definition) => (/[\w\u0621-\u064A]+/.test(definition) ? definition : '');
const normalizeCommas = (definition) => {
    return definition
        .replace(/\s+,\s+/g, ', ')
        .replace(/^,/, '')
        .replace(/,$/, '');
};
const normalizers = [
    normalizeHtmlTags,
    normalizeMarkers,
    normalizeQuotes,
    normalizeLineBreaks,
    normalizeWhitespace,
    normalizeTrailingSymbols,
    normalizeLeadingSymbols,
    normalizeNonWords,
    normalizeCommas,
    (definition) => definition.trim(),
];
const normalizeDefinition = (definition) => {
    const normalized = normalizers.reduce((result, normalize) => normalize(result), definition);
    const hasChanged = normalized !== definition;
    return hasChanged ? (0, exports.normalizeDefinition)(normalized) : normalized;
};
exports.normalizeDefinition = normalizeDefinition;
