"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const markdownRenderer = require("../../../../base/browser/markdownRenderer.js");
const opener = require("../../../../platform/opener/common/opener.js");
const language = require("../../../common/languages/language.js");
const errors = require("../../../../base/common/errors.js");
const textToHtmlTokenizer = require("../../../common/languages/textToHtmlTokenizer.js");
const event = require("../../../../base/common/event.js");
const lifecycle = require("../../../../base/common/lifecycle.js");
const domFontInfo = require("../../../browser/config/domFontInfo.js");
const modesRegistry = require("../../../common/languages/modesRegistry.js");
var __decorate = globalThis && globalThis.__decorate || function(decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
    r = Reflect.decorate(decorators, target, key, desc);
  else
    for (var i = decorators.length - 1; i >= 0; i--)
      if (d = decorators[i])
        r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = globalThis && globalThis.__param || function(paramIndex, decorator) {
  return function(target, key) {
    decorator(target, key, paramIndex);
  };
};
var __awaiter = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
  function adopt(value) {
    return value instanceof P ? value : new P(function(resolve) {
      resolve(value);
    });
  }
  return new (P || (P = Promise))(function(resolve, reject) {
    function fulfilled(value) {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    }
    function rejected(value) {
      try {
        step(generator["throw"](value));
      } catch (e) {
        reject(e);
      }
    }
    function step(result) {
      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
    }
    step((generator = generator.apply(thisArg, _arguments || [])).next());
  });
};
var _a;
exports.MarkdownRenderer = class MarkdownRenderer {
  constructor(_options, _languageService, _openerService) {
    this._options = _options;
    this._languageService = _languageService;
    this._openerService = _openerService;
    this._onDidRenderAsync = new event.Emitter();
    this.onDidRenderAsync = this._onDidRenderAsync.event;
  }
  dispose() {
    this._onDidRenderAsync.dispose();
  }
  render(markdown, options, markedOptions) {
    if (!markdown) {
      const element = document.createElement("span");
      return { element, dispose: () => {
      } };
    }
    const disposables = new lifecycle.DisposableStore();
    const rendered = disposables.add(markdownRenderer.renderMarkdown(markdown, Object.assign(Object.assign({}, this._getRenderOptions(markdown, disposables)), options), markedOptions));
    return {
      element: rendered.element,
      dispose: () => disposables.dispose()
    };
  }
  _getRenderOptions(markdown, disposeables) {
    return {
      codeBlockRenderer: (languageAlias, value) => __awaiter(this, void 0, void 0, function* () {
        var _a2, _b, _c;
        let languageId;
        if (languageAlias) {
          languageId = this._languageService.getLanguageIdByLanguageName(languageAlias);
        } else if (this._options.editor) {
          languageId = (_a2 = this._options.editor.getModel()) === null || _a2 === void 0 ? void 0 : _a2.getLanguageId();
        }
        if (!languageId) {
          languageId = modesRegistry.PLAINTEXT_LANGUAGE_ID;
        }
        const html = yield textToHtmlTokenizer.tokenizeToString(this._languageService, value, languageId);
        const element = document.createElement("span");
        element.innerHTML = (_c = (_b = MarkdownRenderer._ttpTokenizer) === null || _b === void 0 ? void 0 : _b.createHTML(html)) !== null && _c !== void 0 ? _c : html;
        if (this._options.editor) {
          const fontInfo = this._options.editor.getOption(
            44
            /* fontInfo */
          );
          domFontInfo.applyFontInfo(element, fontInfo);
        } else if (this._options.codeBlockFontFamily) {
          element.style.fontFamily = this._options.codeBlockFontFamily;
        }
        return element;
      }),
      asyncRenderCallback: () => this._onDidRenderAsync.fire(),
      actionHandler: {
        callback: (content) => this._openerService.open(content, { fromUserGesture: true, allowContributedOpeners: true, allowCommands: markdown.isTrusted }).catch(errors.onUnexpectedError),
        disposables: disposeables
      }
    };
  }
};
exports.MarkdownRenderer._ttpTokenizer = (_a = window.trustedTypes) === null || _a === void 0 ? void 0 : _a.createPolicy("tokenizeToString", {
  createHTML(html) {
    return html;
  }
});
exports.MarkdownRenderer = __decorate([
  __param(1, language.ILanguageService),
  __param(2, opener.IOpenerService)
], exports.MarkdownRenderer);
