"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformMarkdown = exports.sanitizeStripOptions = exports.typografConfig = exports.DEFAULT_ALLOWED_TAGS = exports.TransformType = void 0;
exports.addTypografRules = addTypografRules;
exports.typograf = typograf;
exports.sanitizeHtml = sanitizeHtml;
exports.typografToHTML = typografToHTML;
exports.typografToText = typografToText;
exports.fullTransform = fullTransform;
exports.typografEntity = typografEntity;
const tslib_1 = require("tslib");
const transform_1 = tslib_1.__importDefault(require("@diplodoc/transform"));
const sanitize_html_1 = tslib_1.__importDefault(require("sanitize-html"));
const typograf_1 = tslib_1.__importDefault(require("typograf"));
var TransformType;
(function (TransformType) {
    TransformType["Text"] = "text";
    TransformType["Html"] = "html";
})(TransformType || (exports.TransformType = TransformType = {}));
exports.DEFAULT_ALLOWED_TAGS = [
    'br',
    'b',
    'i',
    'strong',
    'em',
    'sup',
    'sub',
    'a',
    'ul',
    'ol',
    'li',
];
exports.typografConfig = {
    enabled: ['common/nbsp/afterNumber', 'common/nbsp/afterParagraphMark'],
    disabled: [
        'common/symbols/cf',
        'ru/other/phone-number',
        'common/space/afterColon',
        'common/space/afterSemicolon',
    ],
};
exports.sanitizeStripOptions = {
    allowedTags: [],
    allowedAttributes: {},
};
function addTypografRules(options) {
    options.forEach((option) => {
        typograf_1.default.addRule(option);
    });
}
function enableRules(tp) {
    const { disabled, enabled } = exports.typografConfig;
    enabled.forEach((rule) => tp.enableRule(rule));
    disabled.forEach((rule) => tp.disableRule(rule));
}
function typograf(text, lang = 'ru') {
    const localeByLang = {
        ru: ['ru', 'en-US'],
        en: ['en-US', 'ru'],
    };
    const tp = new typograf_1.default({
        locale: localeByLang[lang] || lang,
        htmlEntity: { type: 'name' },
    });
    enableRules(tp);
    return tp.execute(text);
}
function sanitizeHtml(html, options = exports.sanitizeStripOptions) {
    return html && (0, sanitize_html_1.default)(html, options || exports.sanitizeStripOptions);
}
function typografToHTML(text, lang, allowedTags = exports.DEFAULT_ALLOWED_TAGS) {
    return text && typograf(sanitizeHtml(text, { allowedTags }), lang);
}
function typografToText(text, lang) {
    return text && sanitizeHtml(typograf(text, lang));
}
const transformMarkdown = (input, options) => {
    const { result } = (0, transform_1.default)(input ?? '', options);
    return result;
};
exports.transformMarkdown = transformMarkdown;
function fullTransform(input, { lang, ...options }) {
    const result = (0, exports.transformMarkdown)(input, { lang, ...options });
    const { html, title } = result;
    return {
        ...result,
        html: typograf(html, lang),
        title: title && typograf(title, lang),
    };
}
function typografEntity({ entity, fields, lang = 'ru', transformType = TransformType.Text, }) {
    const transformTypeMap = {
        text: typografToText,
        html: typografToHTML,
    };
    const transformer = transformTypeMap[transformType];
    fields.forEach((fieldName) => {
        if (entity[fieldName]) {
            // eslint-disable-next-line no-param-reassign, no-not-accumulator-reassign/no-not-accumulator-reassign
            entity[fieldName] = transformer(entity[fieldName], lang);
        }
    });
    return entity;
}
//# sourceMappingURL=utils.js.map
