import * as E from "fp-ts-esm/Either";
import { identity, pipe } from "fp-ts-esm/function";
import { getMatches } from "./../ricos-content/index.js";
import { isModifierKey, isTriggerKey } from "./../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 isTriggerKey(key) && modifiers.filter(Boolean).every(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 pipe(
      keys,
      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;
      }, identity)
    );
  }
}
export {
  Keys
};
//# sourceMappingURL=keys.js.map
