import { DisposableStore } from "../../../base/common/lifecycle.mjs";
import { localize } from "../../../nls.mjs";
import { Extensions } from "../common/quickAccess.mjs";
import { IQuickInputService } from "../common/quickInput.mjs";
import { Registry } from "../../registry/common/platform.mjs";
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);
  };
};
let HelpQuickAccessProvider = class HelpQuickAccessProvider2 {
  constructor(quickInputService) {
    this.quickInputService = quickInputService;
    this.registry = Registry.as(Extensions.Quickaccess);
  }
  provide(picker) {
    const disposables = new 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(HelpQuickAccessProvider2.PREFIX.length));
      if (providerDescriptor && providerDescriptor.prefix && providerDescriptor.prefix !== HelpQuickAccessProvider2.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: localize("globalCommands", "global commands"), type: "separator" },
        ...globalProviders,
        { label: 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 === HelpQuickAccessProvider2.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: localize("helpPickAriaLabel", "{0}, {1}", label, helpEntry.description),
          description: helpEntry.description
        });
      }
    }
    return { editorProviders, globalProviders };
  }
};
HelpQuickAccessProvider.PREFIX = "?";
HelpQuickAccessProvider = __decorate([
  __param(0, IQuickInputService)
], HelpQuickAccessProvider);
export {
  HelpQuickAccessProvider
};
