"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MarkdownString = void 0;
exports.escapeCodicons = escapeCodicons;
exports.escapeMarkdownSyntaxTokens = escapeMarkdownSyntaxTokens;
exports.isMarkdownString = isMarkdownString;
exports.parseHrefAndDimensions = parseHrefAndDimensions;
const utils_1 = require("../utils");
const escapeCodiconsRegex = /(\\)?\$\([a-z0-9-]+?(?:~[a-z0-9-]*?)?\)/gi;
function escapeCodicons(text) {
    return text.replace(escapeCodiconsRegex, (match, escaped) => (escaped ? match : `\\${match}`));
}
function escapeMarkdownSyntaxTokens(text) {
    // escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
    return text.replace(/[\\`*_{}[\]()#+\-.!]/g, '\\$&');
}
class MarkdownString {
    constructor(value = '', isTrustedOrOptions = false) {
        var _a, _b;
        this.value = value;
        if (typeof this.value !== 'string') {
            throw (0, utils_1.illegalArgument)('value');
        }
        if (typeof isTrustedOrOptions === 'boolean') {
            this.isTrusted = isTrustedOrOptions;
            this.supportThemeIcons = false;
        }
        else {
            this.isTrusted = (_a = isTrustedOrOptions.isTrusted) !== null && _a !== void 0 ? _a : undefined;
            this.supportThemeIcons = (_b = isTrustedOrOptions.supportThemeIcons) !== null && _b !== void 0 ? _b : false;
        }
    }
    appendText(value, newlineStyle = 0 /* MarkdownStringTextNewlineStyle.Paragraph */) {
        this.value += escapeMarkdownSyntaxTokens(this.supportThemeIcons ? escapeCodicons(value) : value)
            .replace(/([ \t]+)/g, (_match, g1) => '&nbsp;'.repeat(g1.length))
            .replace(/^>/gm, '\\>')
            .replace(/\n/g, newlineStyle === 1 /* MarkdownStringTextNewlineStyle.Break */ ? '\\\n' : '\n\n');
        return this;
    }
    appendMarkdown(value) {
        this.value += value;
        return this;
    }
    appendCodeblock(langId, code) {
        this.value += '\n```';
        this.value += langId;
        this.value += '\n';
        this.value += code;
        this.value += '\n```\n';
        return this;
    }
}
exports.MarkdownString = MarkdownString;
function isMarkdownString(thing) {
    if (thing instanceof MarkdownString) {
        return true;
    }
    else if (thing && typeof thing === 'object') {
        return (typeof thing.value === 'string' &&
            (typeof thing.isTrusted === 'boolean' ||
                thing.isTrusted === undefined) &&
            (typeof thing.supportThemeIcons === 'boolean' ||
                thing.supportThemeIcons === undefined));
    }
    return false;
}
function parseHrefAndDimensions(href) {
    const dimensions = [];
    const splitted = href.split('|').map((s) => s.trim());
    href = splitted[0];
    const parameters = splitted[1];
    if (parameters) {
        const heightFromParams = /height=(\d+)/.exec(parameters);
        const widthFromParams = /width=(\d+)/.exec(parameters);
        const height = heightFromParams ? heightFromParams[1] : '';
        const width = widthFromParams ? widthFromParams[1] : '';
        const widthIsFinite = isFinite(parseInt(width, 10));
        const heightIsFinite = isFinite(parseInt(height, 10));
        if (widthIsFinite) {
            dimensions.push(`width="${width}"`);
        }
        if (heightIsFinite) {
            dimensions.push(`height="${height}"`);
        }
    }
    return { href, dimensions };
}
//# sourceMappingURL=html-content.js.map