"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var keys_exports = {};
__export(keys_exports, {
  Keys: () => Keys
});
module.exports = __toCommonJS(keys_exports);
var E = __toESM(require("fp-ts-esm/Either"));
var import_function = require("fp-ts-esm/function");
var import_ricos_content = require("./../ricos-content/index.js");
var import_ricos_types = require("./../ricos-types/index.js");
class KeyboardShortcutParseError extends Error {
}
class Keys {
  constructor(key, modifiers = []) {
    this.macOsMap = {
      Meta: "⌘",
      Ctrl: "⌃",
      Alt: "⌥",
      Shift: "⇧",
      Enter: "↩",
      Space: "␣",
      Backspace: "⌫",
      Tab: "⇥",
      Escape: "⎋",
      Arrow: "← ↑ ↓ →",
      ArrowLeft: "←",
      ArrowRight: "→",
      ArrowUp: "↑",
      ArrowDown: "↓",
      Delete: "⌦",
      Home: "⇱",
      End: "⇲",
      PageUp: "⇞",
      PageDown: "⇟",
      Insert: "⌤",
      AltGraph: "⌥"
    };
    this.windowsMap = {
      Meta: "⊞",
      Ctrl: "Ctrl",
      Alt: "Alt",
      Shift: "⇧",
      Enter: "⏎",
      Space: "␣",
      Backspace: "⟵",
      Tab: "↹",
      Escape: "Esc",
      Arrow: "← ↑ ↓ →",
      ArrowLeft: "←",
      ArrowRight: "→",
      ArrowUp: "↑",
      ArrowDown: "↓",
      Delete: "Del",
      Home: "Home",
      End: "End",
      PageUp: "PgUp",
      PageDown: "PgDn",
      Insert: "Ins",
      AltGraph: "AltGr"
    };
    if (!Keys.isValidKeys(key, modifiers)) {
      throw new KeyboardShortcutParseError(
        `invalid keys combination: modifiers = ${modifiers.join("+")} | key = ${key}`
      );
    }
    this.modifiers = modifiers.filter(Boolean);
    this.key = key;
  }
  static isValidKeys(key, modifiers) {
    return (0, import_ricos_types.isTriggerKey)(key) && modifiers.filter(Boolean).every(import_ricos_types.isModifierKey);
  }
  /**
   * Textual representation of parsed Keys:
   * ```ts
   *  `${modifiers}+${keys}`
   * ```
   * @memberof Keys
   */
  toString() {
    return `${this.modifiers.length > 0 ? this.modifiers.sort().filter(Boolean).join("+").concat("+") : ""}${this.key}`;
  }
  toPlatformString(platform) {
    return platform === "macOs" ? this.toMacOsString() : this.toWindowsString();
  }
  toMacOsString() {
    return this.modifiers.map((modifier) => this.macOsMap[modifier]).concat(this.macOsMap[this.key] || this.key).join(" ");
  }
  toWindowsString() {
    return this.modifiers.map((modifier) => this.windowsMap[modifier]).concat(this.windowsMap[this.key] || this.key).join(" ");
  }
  toTooltipHint(platform) {
    return this.toPlatformString(platform).replace(/ /g, platform === "macOs" ? "" : "+");
  }
  equals(keys) {
    return this.toString() === keys.toString();
  }
  /**
   * Parses keys string
   *
   * Example: 'Control+B' parsed to
   * ```ts
   *  {
   *    modifiers: ['Control'],
   *    key: ['B']
   *  }
   * ```
   *
   * @param {BasicKeyCombination} keys key combination
   * @returns  {Keys} Keys instance
   * @memberof Keys
   */
  static parse(keys) {
    return (0, import_function.pipe)(
      keys,
      (0, import_ricos_content.getMatches)(/(?:([^+]+)\+)?(?:([^+]+)\+)?(.+)/gi),
      E.fromOption(() => new KeyboardShortcutParseError(`invalid keys combination: ${keys}`)),
      E.map(
        ([, modifier1, modifier2, key]) => new Keys(key, [modifier1, modifier2].filter(Boolean))
      ),
      E.fold((e) => {
        throw e;
      }, import_function.identity)
    );
  }
}
//# sourceMappingURL=keys.js.map
