"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const browser = require("../../../../base/browser/browser.js");
const dom = require("../../../../base/browser/dom.js");
const iconLabel = require("../../../../base/browser/ui/iconLabel/iconLabel.js");
const arrays = require("../../../../base/common/arrays.js");
const codicons = require("../../../../base/common/codicons.js");
const event = require("../../../../base/common/event.js");
const filters = require("../../../../base/common/filters.js");
const lifecycle = require("../../../../base/common/lifecycle.js");
const uri = require("../../../../base/common/uri.js");
const editorOptions = require("../../../common/config/editorOptions.js");
const languages = require("../../../common/languages.js");
const getIconClasses = require("../../../common/services/getIconClasses.js");
const model = require("../../../common/services/model.js");
const language = require("../../../common/languages/language.js");
const nls = require("../../../../nls.js");
const files = require("../../../../platform/files/common/files.js");
const iconRegistry = require("../../../../platform/theme/common/iconRegistry.js");
const themeService = require("../../../../platform/theme/common/themeService.js");
const suggestWidgetDetails = require("./suggestWidgetDetails.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 _a;
function getAriaId(index) {
  return `suggest-aria-id:${index}`;
}
const suggestMoreInfoIcon = iconRegistry.registerIcon("suggest-more-info", codicons.Codicon.chevronRight, nls.localize("suggestMoreInfoIcon", "Icon for more information in the suggest widget."));
const _completionItemColor = new (_a = class ColorExtractor {
  extract(item, out) {
    if (item.textLabel.match(ColorExtractor._regexStrict)) {
      out[0] = item.textLabel;
      return true;
    }
    if (item.completion.detail && item.completion.detail.match(ColorExtractor._regexStrict)) {
      out[0] = item.completion.detail;
      return true;
    }
    if (typeof item.completion.documentation === "string") {
      const match = ColorExtractor._regexRelaxed.exec(item.completion.documentation);
      if (match && (match.index === 0 || match.index + match[0].length === item.completion.documentation.length)) {
        out[0] = match[0];
        return true;
      }
    }
    return false;
  }
}, _a._regexRelaxed = /(#([\da-fA-F]{3}){1,2}|(rgb|hsl)a\(\s*(\d{1,3}%?\s*,\s*){3}(1|0?\.\d+)\)|(rgb|hsl)\(\s*\d{1,3}%?(\s*,\s*\d{1,3}%?){2}\s*\))/, _a._regexStrict = new RegExp(`^${_a._regexRelaxed.source}$`, "i"), _a)();
exports.ItemRenderer = class ItemRenderer {
  constructor(_editor, _modelService, _languageService, _themeService) {
    this._editor = _editor;
    this._modelService = _modelService;
    this._languageService = _languageService;
    this._themeService = _themeService;
    this._onDidToggleDetails = new event.Emitter();
    this.onDidToggleDetails = this._onDidToggleDetails.event;
    this.templateId = "suggestion";
  }
  dispose() {
    this._onDidToggleDetails.dispose();
  }
  renderTemplate(container) {
    const data = /* @__PURE__ */ Object.create(null);
    data.disposables = new lifecycle.DisposableStore();
    data.root = container;
    data.root.classList.add("show-file-icons");
    data.icon = dom.append(container, dom.$(".icon"));
    data.colorspan = dom.append(data.icon, dom.$("span.colorspan"));
    const text = dom.append(container, dom.$(".contents"));
    const main = dom.append(text, dom.$(".main"));
    data.iconContainer = dom.append(main, dom.$(".icon-label.codicon"));
    data.left = dom.append(main, dom.$("span.left"));
    data.right = dom.append(main, dom.$("span.right"));
    data.iconLabel = new iconLabel.IconLabel(data.left, { supportHighlights: true, supportIcons: true });
    data.disposables.add(data.iconLabel);
    data.parametersLabel = dom.append(data.left, dom.$("span.signature-label"));
    data.qualifierLabel = dom.append(data.left, dom.$("span.qualifier-label"));
    data.detailsLabel = dom.append(data.right, dom.$("span.details-label"));
    data.readMore = dom.append(data.right, dom.$("span.readMore" + themeService.ThemeIcon.asCSSSelector(suggestMoreInfoIcon)));
    data.readMore.title = nls.localize("readMore", "Read More");
    const configureFont = () => {
      const options = this._editor.getOptions();
      const fontInfo = options.get(
        44
        /* fontInfo */
      );
      const fontFamily = fontInfo.getMassagedFontFamily(browser.isSafari ? editorOptions.EDITOR_FONT_DEFAULTS.fontFamily : null);
      const fontFeatureSettings = fontInfo.fontFeatureSettings;
      const fontSize = options.get(
        107
        /* suggestFontSize */
      ) || fontInfo.fontSize;
      const lineHeight = options.get(
        108
        /* suggestLineHeight */
      ) || fontInfo.lineHeight;
      const fontWeight = fontInfo.fontWeight;
      const fontSizePx = `${fontSize}px`;
      const lineHeightPx = `${lineHeight}px`;
      data.root.style.fontSize = fontSizePx;
      data.root.style.fontWeight = fontWeight;
      main.style.fontFamily = fontFamily;
      main.style.fontFeatureSettings = fontFeatureSettings;
      main.style.lineHeight = lineHeightPx;
      data.icon.style.height = lineHeightPx;
      data.icon.style.width = lineHeightPx;
      data.readMore.style.height = lineHeightPx;
      data.readMore.style.width = lineHeightPx;
    };
    configureFont();
    data.disposables.add(this._editor.onDidChangeConfiguration((e) => {
      if (e.hasChanged(
        44
        /* fontInfo */
      ) || e.hasChanged(
        107
        /* suggestFontSize */
      ) || e.hasChanged(
        108
        /* suggestLineHeight */
      )) {
        configureFont();
      }
    }));
    return data;
  }
  renderElement(element, index, data) {
    const { completion } = element;
    data.root.id = getAriaId(index);
    data.colorspan.style.backgroundColor = "";
    const labelOptions = {
      labelEscapeNewLines: true,
      matches: filters.createMatches(element.score)
    };
    let color = [];
    if (completion.kind === 19 && _completionItemColor.extract(element, color)) {
      data.icon.className = "icon customcolor";
      data.iconContainer.className = "icon hide";
      data.colorspan.style.backgroundColor = color[0];
    } else if (completion.kind === 20 && this._themeService.getFileIconTheme().hasFileIcons) {
      data.icon.className = "icon hide";
      data.iconContainer.className = "icon hide";
      const labelClasses = getIconClasses.getIconClasses(this._modelService, this._languageService, uri.URI.from({ scheme: "fake", path: element.textLabel }), files.FileKind.FILE);
      const detailClasses = getIconClasses.getIconClasses(this._modelService, this._languageService, uri.URI.from({ scheme: "fake", path: completion.detail }), files.FileKind.FILE);
      labelOptions.extraClasses = labelClasses.length > detailClasses.length ? labelClasses : detailClasses;
    } else if (completion.kind === 23 && this._themeService.getFileIconTheme().hasFolderIcons) {
      data.icon.className = "icon hide";
      data.iconContainer.className = "icon hide";
      labelOptions.extraClasses = arrays.flatten([
        getIconClasses.getIconClasses(this._modelService, this._languageService, uri.URI.from({ scheme: "fake", path: element.textLabel }), files.FileKind.FOLDER),
        getIconClasses.getIconClasses(this._modelService, this._languageService, uri.URI.from({ scheme: "fake", path: completion.detail }), files.FileKind.FOLDER)
      ]);
    } else {
      data.icon.className = "icon hide";
      data.iconContainer.className = "";
      data.iconContainer.classList.add("suggest-icon", ...codicons.CSSIcon.asClassNameArray(languages.CompletionItemKinds.toIcon(completion.kind)));
    }
    if (completion.tags && completion.tags.indexOf(
      1
      /* Deprecated */
    ) >= 0) {
      labelOptions.extraClasses = (labelOptions.extraClasses || []).concat(["deprecated"]);
      labelOptions.matches = [];
    }
    data.iconLabel.setLabel(element.textLabel, void 0, labelOptions);
    if (typeof completion.label === "string") {
      data.parametersLabel.textContent = "";
      data.detailsLabel.textContent = stripNewLines(completion.detail || "");
      data.root.classList.add("string-label");
    } else {
      data.parametersLabel.textContent = stripNewLines(completion.label.detail || "");
      data.detailsLabel.textContent = stripNewLines(completion.label.description || "");
      data.root.classList.remove("string-label");
    }
    if (this._editor.getOption(
      106
      /* suggest */
    ).showInlineDetails) {
      dom.show(data.detailsLabel);
    } else {
      dom.hide(data.detailsLabel);
    }
    if (suggestWidgetDetails.canExpandCompletionItem(element)) {
      data.right.classList.add("can-expand-details");
      dom.show(data.readMore);
      data.readMore.onmousedown = (e) => {
        e.stopPropagation();
        e.preventDefault();
      };
      data.readMore.onclick = (e) => {
        e.stopPropagation();
        e.preventDefault();
        this._onDidToggleDetails.fire();
      };
    } else {
      data.right.classList.remove("can-expand-details");
      dom.hide(data.readMore);
      data.readMore.onmousedown = null;
      data.readMore.onclick = null;
    }
  }
  disposeTemplate(templateData) {
    templateData.disposables.dispose();
  }
};
exports.ItemRenderer = __decorate([
  __param(1, model.IModelService),
  __param(2, language.ILanguageService),
  __param(3, themeService.IThemeService)
], exports.ItemRenderer);
function stripNewLines(str) {
  return str.replace(/\r\n|\r|\n/g, "");
}
exports.getAriaId = getAriaId;
exports.suggestMoreInfoIcon = suggestMoreInfoIcon;
