"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const keybindings = require("../../../base/common/keybindings.js");
const platform$1 = require("../../../base/common/platform.js");
const commands = require("../../commands/common/commands.js");
const platform = require("../../registry/common/platform.js");
class KeybindingsRegistryImpl {
  constructor() {
    this._coreKeybindings = [];
    this._extensionKeybindings = [];
    this._cachedMergedKeybindings = null;
  }
  /**
   * Take current platform into account and reduce to primary & secondary.
   */
  static bindToCurrentPlatform(kb) {
    if (platform$1.OS === 1) {
      if (kb && kb.win) {
        return kb.win;
      }
    } else if (platform$1.OS === 2) {
      if (kb && kb.mac) {
        return kb.mac;
      }
    } else {
      if (kb && kb.linux) {
        return kb.linux;
      }
    }
    return kb;
  }
  registerKeybindingRule(rule) {
    const actualKb = KeybindingsRegistryImpl.bindToCurrentPlatform(rule);
    if (actualKb && actualKb.primary) {
      const kk = keybindings.createKeybinding(actualKb.primary, platform$1.OS);
      if (kk) {
        this._registerDefaultKeybinding(kk, rule.id, rule.args, rule.weight, 0, rule.when);
      }
    }
    if (actualKb && Array.isArray(actualKb.secondary)) {
      for (let i = 0, len = actualKb.secondary.length; i < len; i++) {
        const k = actualKb.secondary[i];
        const kk = keybindings.createKeybinding(k, platform$1.OS);
        if (kk) {
          this._registerDefaultKeybinding(kk, rule.id, rule.args, rule.weight, -i - 1, rule.when);
        }
      }
    }
  }
  registerCommandAndKeybindingRule(desc) {
    this.registerKeybindingRule(desc);
    commands.CommandsRegistry.registerCommand(desc);
  }
  static _mightProduceChar(keyCode) {
    if (keyCode >= 21 && keyCode <= 30) {
      return true;
    }
    if (keyCode >= 31 && keyCode <= 56) {
      return true;
    }
    return keyCode === 80 || keyCode === 81 || keyCode === 82 || keyCode === 83 || keyCode === 84 || keyCode === 85 || keyCode === 86 || keyCode === 110 || keyCode === 111 || keyCode === 87 || keyCode === 88 || keyCode === 89 || keyCode === 90 || keyCode === 91 || keyCode === 92;
  }
  _assertNoCtrlAlt(keybinding, commandId) {
    if (keybinding.ctrlKey && keybinding.altKey && !keybinding.metaKey) {
      if (KeybindingsRegistryImpl._mightProduceChar(keybinding.keyCode)) {
        console.warn("Ctrl+Alt+ keybindings should not be used by default under Windows. Offender: ", keybinding, " for ", commandId);
      }
    }
  }
  _registerDefaultKeybinding(keybinding, commandId, commandArgs, weight1, weight2, when) {
    if (platform$1.OS === 1) {
      this._assertNoCtrlAlt(keybinding.parts[0], commandId);
    }
    this._coreKeybindings.push({
      keybinding: keybinding.parts,
      command: commandId,
      commandArgs,
      when,
      weight1,
      weight2,
      extensionId: null,
      isBuiltinExtension: false
    });
    this._cachedMergedKeybindings = null;
  }
  getDefaultKeybindings() {
    if (!this._cachedMergedKeybindings) {
      this._cachedMergedKeybindings = [].concat(this._coreKeybindings).concat(this._extensionKeybindings);
      this._cachedMergedKeybindings.sort(sorter);
    }
    return this._cachedMergedKeybindings.slice(0);
  }
}
const KeybindingsRegistry = new KeybindingsRegistryImpl();
const Extensions = {
  EditorModes: "platform.keybindingsRegistry"
};
platform.Registry.add(Extensions.EditorModes, KeybindingsRegistry);
function sorter(a, b) {
  if (a.weight1 !== b.weight1) {
    return a.weight1 - b.weight1;
  }
  if (a.command < b.command) {
    return -1;
  }
  if (a.command > b.command) {
    return 1;
  }
  return a.weight2 - b.weight2;
}
exports.Extensions = Extensions;
exports.KeybindingsRegistry = KeybindingsRegistry;
