"use strict";
//!!! DO NOT modify, this file was COPIED from 'microsoft/vscode'
Object.defineProperty(exports, "__esModule", { value: true });
exports.MarkdownString = void 0;
exports.isEmptyMarkdownString = isEmptyMarkdownString;
exports.isMarkdownString = isMarkdownString;
exports.markdownStringEqual = markdownStringEqual;
exports.escapeMarkdownSyntaxTokens = escapeMarkdownSyntaxTokens;
exports.escapeMarkdownLinkLabel = escapeMarkdownLinkLabel;
exports.appendEscapedMarkdownCodeBlockFence = appendEscapedMarkdownCodeBlockFence;
exports.appendEscapedMarkdownInlineCode = appendEscapedMarkdownInlineCode;
exports.escapeDoubleQuotes = escapeDoubleQuotes;
exports.removeMarkdownEscapes = removeMarkdownEscapes;
exports.parseHrefAndDimensions = parseHrefAndDimensions;
exports.createMarkdownLink = createMarkdownLink;
exports.createMarkdownCommandLink = createMarkdownCommandLink;
exports.createCommandUri = createCommandUri;
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
const errors_1 = require("./errors");
const iconLabels_1 = require("./iconLabels");
const network_1 = require("./network");
const resources_1 = require("./resources");
const strings_1 = require("./strings");
const uri_1 = require("./uri");
class MarkdownString {
    static lift(dto) {
        const markdownString = new MarkdownString(dto.value, dto);
        markdownString.uris = dto.uris;
        markdownString.baseUri = dto.baseUri ? uri_1.URI.revive(dto.baseUri) : undefined;
        return markdownString;
    }
    constructor(value = '', isTrustedOrOptions = false) {
        this.value = value;
        if (typeof this.value !== 'string') {
            throw (0, errors_1.illegalArgument)('value');
        }
        if (typeof isTrustedOrOptions === 'boolean') {
            this.isTrusted = isTrustedOrOptions;
            this.supportThemeIcons = false;
            this.supportHtml = false;
            this.supportAlertSyntax = false;
        }
        else {
            this.isTrusted = isTrustedOrOptions.isTrusted ?? undefined;
            this.supportThemeIcons = isTrustedOrOptions.supportThemeIcons ?? false;
            this.supportHtml = isTrustedOrOptions.supportHtml ?? false;
            this.supportAlertSyntax = isTrustedOrOptions.supportAlertSyntax ?? false;
        }
    }
    appendText(value, newlineStyle = 0 /* MarkdownStringTextNewlineStyle.Paragraph */) {
        this.value += escapeMarkdownSyntaxTokens(this.supportThemeIcons ? (0, iconLabels_1.escapeIcons)(value) : value) // CodeQL [SM02383] The Markdown is fully sanitized after being rendered.
            .replace(/([ \t]+)/g, (_match, g1) => '&nbsp;'.repeat(g1.length)) // CodeQL [SM02383] The Markdown is fully sanitized after being rendered.
            .replace(/\>/gm, '\\>') // CodeQL [SM02383] The Markdown is fully sanitized after being rendered.
            .replace(/\n/g, newlineStyle === 1 /* MarkdownStringTextNewlineStyle.Break */ ? '\\\n' : '\n\n'); // CodeQL [SM02383] The Markdown is fully sanitized after being rendered.
        return this;
    }
    appendMarkdown(value) {
        this.value += value;
        return this;
    }
    appendCodeblock(langId, code) {
        this.value += `\n${appendEscapedMarkdownCodeBlockFence(code, langId)}\n`;
        return this;
    }
    appendLink(target, label, title) {
        this.value += '[';
        this.value += this._escape(label, ']');
        this.value += '](';
        this.value += this._escape(String(target), ')');
        if (title) {
            this.value += ` "${this._escape(this._escape(title, '"'), ')')}"`;
        }
        this.value += ')';
        return this;
    }
    _escape(value, ch) {
        const r = new RegExp((0, strings_1.escapeRegExpCharacters)(ch), 'g');
        return value.replace(r, (match, offset) => {
            if (value.charAt(offset - 1) !== '\\') {
                return `\\${match}`;
            }
            else {
                return match;
            }
        });
    }
}
exports.MarkdownString = MarkdownString;
function isEmptyMarkdownString(oneOrMany) {
    if (isMarkdownString(oneOrMany)) {
        return !oneOrMany.value;
    }
    else if (Array.isArray(oneOrMany)) {
        return oneOrMany.every(isEmptyMarkdownString);
    }
    else {
        return true;
    }
}
function isMarkdownString(thing) {
    if (thing instanceof MarkdownString) {
        return true;
    }
    else if (thing && typeof thing === 'object') {
        return typeof thing.value === 'string'
            && (typeof thing.isTrusted === 'boolean' || typeof thing.isTrusted === 'object' || thing.isTrusted === undefined)
            && (typeof thing.supportThemeIcons === 'boolean' || thing.supportThemeIcons === undefined)
            && (typeof thing.supportAlertSyntax === 'boolean' || thing.supportAlertSyntax === undefined);
    }
    return false;
}
function markdownStringEqual(a, b) {
    if (a === b) {
        return true;
    }
    else if (!a || !b) {
        return false;
    }
    else {
        return a.value === b.value
            && a.isTrusted === b.isTrusted
            && a.supportThemeIcons === b.supportThemeIcons
            && a.supportHtml === b.supportHtml
            && a.supportAlertSyntax === b.supportAlertSyntax
            && (a.baseUri === b.baseUri || !!a.baseUri && !!b.baseUri && (0, resources_1.isEqual)(uri_1.URI.from(a.baseUri), uri_1.URI.from(b.baseUri)));
    }
}
function escapeMarkdownSyntaxTokens(text) {
    // escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
    return text.replace(/[\\`*_{}[\]()#+\-!~]/g, '\\$&'); // CodeQL [SM02383] Backslash is escaped in the character class
}
/**
 * Escapes only the characters that would break out of markdown link text
 * (`[label](url)`) syntax: `\` and `]`. Use this when the escaped string is
 * displayed as the visible label of a link, since renderers that extract the
 * link text without re-parsing markdown (e.g. the chat inline anchor / skill
 * pill) would otherwise show full `escapeMarkdownSyntaxTokens` backslashes
 * (`\-`, `\.`, ...) verbatim.
 */
function escapeMarkdownLinkLabel(text) {
    return text.replace(/[\\\]]/g, '\\$&');
}
/**
 * @see https://github.com/microsoft/vscode/issues/193746
 */
function appendEscapedMarkdownCodeBlockFence(code, langId) {
    const longestFenceLength = code.match(/^`+/gm)?.reduce((a, b) => (a.length > b.length ? a : b)).length ??
        0;
    const desiredFenceLength = longestFenceLength >= 3 ? longestFenceLength + 1 : 3;
    // the markdown result
    return [
        `${'`'.repeat(desiredFenceLength)}${langId}`,
        code,
        `${'`'.repeat(desiredFenceLength)}`,
    ].join('\n');
}
/**
 * Wraps arbitrary text in a markdown inline code span using a backtick fence
 * long enough to safely contain any backtick sequences present in the text.
 *
 * Backticks inside an inline code span cannot be backslash-escaped per the
 * CommonMark spec — the only safe way is to choose a delimiter run longer
 * than any run of backticks in the content (and pad with spaces if the
 * content begins or ends with a backtick).
 */
function appendEscapedMarkdownInlineCode(text) {
    const longestBacktickRun = Math.max(0, ...(text.match(/`+/g) ?? []).map(m => m.length));
    const fence = '`'.repeat(longestBacktickRun + 1);
    const needsSpace = text.startsWith('`') || text.endsWith('`');
    const content = needsSpace ? ` ${text} ` : text;
    return `${fence}${content}${fence}`;
}
function escapeDoubleQuotes(input) {
    return input.replace(/"/g, '&quot;');
}
function removeMarkdownEscapes(text) {
    if (!text) {
        return text;
    }
    return text.replace(/\\([\\`*_{}[\]()#+\-.!~])/g, '$1');
}
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));
        const heightIsFinite = isFinite(parseInt(height));
        if (widthIsFinite) {
            dimensions.push(`width="${width}"`);
        }
        if (heightIsFinite) {
            dimensions.push(`height="${height}"`);
        }
    }
    return { href, dimensions };
}
function createMarkdownLink(text, href, title, escapeTokens = true) {
    return `[${escapeTokens ? escapeMarkdownSyntaxTokens(text) : text}](${href}${title ? ` "${escapeMarkdownSyntaxTokens(title)}"` : ''})`;
}
function createMarkdownCommandLink(command, escapeTokens = true) {
    const uri = createCommandUri(command.id, ...(command.arguments || [])).toString();
    return createMarkdownLink(command.text, uri, command.tooltip, escapeTokens);
}
function createCommandUri(commandId, ...commandArgs) {
    return uri_1.URI.from({
        scheme: network_1.Schemas.command,
        path: commandId,
        query: commandArgs.length ? encodeURIComponent(JSON.stringify(commandArgs)) : undefined,
    });
}
//# sourceMappingURL=htmlContent.js.map