"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const lifecycle = require("../../../base/common/lifecycle.js");
const nls = require("../../../nls.js");
const quickAccess = require("../common/quickAccess.js");
const quickInput = require("../common/quickInput.js");
const platform = require("../../registry/common/platform.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);
  };
};
exports.HelpQuickAccessProvider = class HelpQuickAccessProvider {
  constructor(quickInputService) {
    this.quickInputService = quickInputService;
    this.registry = platform.Registry.as(quickAccess.Extensions.Quickaccess);
  }
  provide(picker) {
    const disposables = new lifecycle.DisposableStore();
    disposables.add(picker.onDidAccept(() => {
      const [item] = picker.selectedItems;
      if (item) {
        this.quickInputService.quickAccess.show(item.prefix, { preserveValue: true });
      }
    }));
    disposables.add(picker.onDidChangeValue((value) => {
      const providerDescriptor = this.registry.getQuickAccessProvider(value.substr(HelpQuickAccessProvider.PREFIX.length));
      if (providerDescriptor && providerDescriptor.prefix && providerDescriptor.prefix !== HelpQuickAccessProvider.PREFIX) {
        this.quickInputService.quickAccess.show(providerDescriptor.prefix, { preserveValue: true });
      }
    }));
    const { editorProviders, globalProviders } = this.getQuickAccessProviders();
    picker.items = editorProviders.length === 0 || globalProviders.length === 0 ? (
      // Without groups
      [
        ...editorProviders.length === 0 ? globalProviders : editorProviders
      ]
    ) : (
      // With groups
      [
        { label: nls.localize("globalCommands", "global commands"), type: "separator" },
        ...globalProviders,
        { label: nls.localize("editorCommands", "editor commands"), type: "separator" },
        ...editorProviders
      ]
    );
    return disposables;
  }
  getQuickAccessProviders() {
    const globalProviders = [];
    const editorProviders = [];
    for (const provider of this.registry.getQuickAccessProviders().sort((providerA, providerB) => providerA.prefix.localeCompare(providerB.prefix))) {
      if (provider.prefix === HelpQuickAccessProvider.PREFIX) {
        continue;
      }
      for (const helpEntry of provider.helpEntries) {
        const prefix = helpEntry.prefix || provider.prefix;
        const label = prefix || "…";
        (helpEntry.needsEditor ? editorProviders : globalProviders).push({
          prefix,
          label,
          ariaLabel: nls.localize("helpPickAriaLabel", "{0}, {1}", label, helpEntry.description),
          description: helpEntry.description
        });
      }
    }
    return { editorProviders, globalProviders };
  }
};
exports.HelpQuickAccessProvider.PREFIX = "?";
exports.HelpQuickAccessProvider = __decorate([
  __param(0, quickInput.IQuickInputService)
], exports.HelpQuickAccessProvider);
