
import { registerCss } from '@codingame/monaco-vscode-api/css';
import { __decorate, __param } from '../../../../../../../../external/tslib/tslib.es6.js';
import { renderMarkdown } from '../../../../../base/browser/markdownRenderer.js';
import { createElement } from '../../../../../base/browser/dom.js';
import { createTrustedTypesPolicy } from '../../../../../base/browser/trustedTypes.js';
import { onUnexpectedError } from '../../../../../base/common/errors.js';
import { IOpenerService } from '../../../../../platform/opener/common/opener.service.js';
import { EditorOption } from '../../../../common/config/editorOptions.js';
import { ILanguageService } from '../../../../common/languages/language.service.js';
import { PLAINTEXT_LANGUAGE_ID } from '../../../../common/languages/modesRegistry.js';
import { tokenizeToString } from '../../../../common/languages/textToHtmlTokenizer.js';
import { applyFontInfo } from '../../../config/domFontInfo.js';
import * as renderedMarkdown from './renderedMarkdown.css';

var MarkdownRenderer_1;
registerCss(renderedMarkdown);
let MarkdownRenderer = class MarkdownRenderer {
    static { MarkdownRenderer_1 = this; }
    static { this._ttpTokenizer = createTrustedTypesPolicy('tokenizeToString', {
        createHTML(html) {
            return html;
        }
    }); }
    constructor(_options, _languageService, _openerService) {
        this._options = _options;
        this._languageService = _languageService;
        this._openerService = _openerService;
    }
    render(markdown, options, outElement) {
        const rendered = renderMarkdown(markdown, {
            codeBlockRenderer: (alias, value) => this.renderCodeBlock(alias, value),
            actionHandler: (link, mdStr) => this.openMarkdownLink(link, mdStr),
            ...options,
        }, outElement);
        rendered.element.classList.add('rendered-markdown');
        return rendered;
    }
    async renderCodeBlock(languageAlias, value) {
        let languageId;
        if (languageAlias) {
            languageId = this._languageService.getLanguageIdByLanguageName(languageAlias);
        }
        else if (this._options.editor) {
            languageId = this._options.editor.getModel()?.getLanguageId();
        }
        if (!languageId) {
            languageId = PLAINTEXT_LANGUAGE_ID;
        }
        const html = await tokenizeToString(this._languageService, value, languageId);
        const element = createElement('span');
        element.innerHTML = (MarkdownRenderer_1._ttpTokenizer?.createHTML(html) ?? html);
        if (this._options.editor) {
            const fontInfo = this._options.editor.getOption(EditorOption.fontInfo);
            applyFontInfo(element, fontInfo);
        }
        else if (this._options.codeBlockFontFamily) {
            element.style.fontFamily = this._options.codeBlockFontFamily;
        }
        if (this._options.codeBlockFontSize !== undefined) {
            element.style.fontSize = this._options.codeBlockFontSize;
        }
        return element;
    }
    async openMarkdownLink(link, markdown) {
        await openLinkFromMarkdown(this._openerService, link, markdown.isTrusted);
    }
};
MarkdownRenderer = MarkdownRenderer_1 = ( __decorate([
    ( __param(1, ILanguageService)),
    ( __param(2, IOpenerService))
], MarkdownRenderer));
async function openLinkFromMarkdown(openerService, link, isTrusted, skipValidation) {
    try {
        return await openerService.open(link, {
            fromUserGesture: true,
            allowContributedOpeners: true,
            allowCommands: toAllowCommandsOption(isTrusted),
            skipValidation
        });
    }
    catch (e) {
        onUnexpectedError(e);
        return false;
    }
}
function toAllowCommandsOption(isTrusted) {
    if (isTrusted === true) {
        return true;
    }
    if (isTrusted && Array.isArray(isTrusted.enabledCommands)) {
        return isTrusted.enabledCommands;
    }
    return false;
}

export { MarkdownRenderer, openLinkFromMarkdown };
