"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const event = require("../../../base/common/event.js");
const iterator = require("../../../base/common/iterator.js");
const lifecycle = require("../../../base/common/lifecycle.js");
const linkedList = require("../../../base/common/linkedList.js");
const types = require("../../../base/common/types.js");
const instantiation = require("../../instantiation/common/instantiation.js");
const ICommandService = instantiation.createDecorator("commandService");
const CommandsRegistry = new class {
  constructor() {
    this._commands = /* @__PURE__ */ new Map();
    this._onDidRegisterCommand = new event.Emitter();
    this.onDidRegisterCommand = this._onDidRegisterCommand.event;
  }
  registerCommand(idOrCommand, handler) {
    if (!idOrCommand) {
      throw new Error(`invalid command`);
    }
    if (typeof idOrCommand === "string") {
      if (!handler) {
        throw new Error(`invalid command`);
      }
      return this.registerCommand({ id: idOrCommand, handler });
    }
    if (idOrCommand.description) {
      const constraints = [];
      for (let arg of idOrCommand.description.args) {
        constraints.push(arg.constraint);
      }
      const actualHandler = idOrCommand.handler;
      idOrCommand.handler = function(accessor, ...args) {
        types.validateConstraints(args, constraints);
        return actualHandler(accessor, ...args);
      };
    }
    const { id } = idOrCommand;
    let commands = this._commands.get(id);
    if (!commands) {
      commands = new linkedList.LinkedList();
      this._commands.set(id, commands);
    }
    let removeFn = commands.unshift(idOrCommand);
    let ret = lifecycle.toDisposable(() => {
      removeFn();
      const command = this._commands.get(id);
      if (command === null || command === void 0 ? void 0 : command.isEmpty()) {
        this._commands.delete(id);
      }
    });
    this._onDidRegisterCommand.fire(id);
    return ret;
  }
  registerCommandAlias(oldId, newId) {
    return CommandsRegistry.registerCommand(oldId, (accessor, ...args) => accessor.get(ICommandService).executeCommand(newId, ...args));
  }
  getCommand(id) {
    const list = this._commands.get(id);
    if (!list || list.isEmpty()) {
      return void 0;
    }
    return iterator.Iterable.first(list);
  }
  getCommands() {
    const result = /* @__PURE__ */ new Map();
    for (const key of this._commands.keys()) {
      const command = this.getCommand(key);
      if (command) {
        result.set(key, command);
      }
    }
    return result;
  }
}();
CommandsRegistry.registerCommand("noop", () => {
});
exports.CommandsRegistry = CommandsRegistry;
exports.ICommandService = ICommandService;
