import { EditorKeyboardShortcut } from "./editor-keyboard-shortcut.js";
const TOPICS = ["ricos.shortcuts.functionality.applied"];
class EditorKeyboardShortcuts {
  constructor(modalService, config = {
    isDebugMode: false
  }) {
    this.shortcuts = [];
    this.isDebugMode = false;
    this.topicsToPublish = TOPICS;
    this.modalService = modalService;
    this.isDebugMode = config.isDebugMode;
  }
  hasDuplicate(shortcut) {
    return this.shortcuts.find((s) => s.equals(shortcut));
  }
  /**
   * Constructs keyboard shortcut data for given group
   */
  getHotKeysProps({
    group,
    publicCommands,
    editor,
    t,
    platform
  }) {
    const publisher = this.publishers.byTopic("ricos.shortcuts.functionality.applied");
    return this.shortcuts.filter((s) => s.isEnabled()).filter(
      (s) => s.getGroup() === group || ["ShortcutsSheet_Section_General", "ShortcutsSheet_Section_ToolbarNavigation"].includes(
        s.getGroup()
      )
    ).reduce(
      ({ keyMap, handlers }, shortcut) => {
        return {
          allowChanges: false,
          keyMap: {
            ...keyMap,
            [shortcut.getName()]: {
              sequences: [shortcut.getKeys(platform).toString().toLowerCase()],
              enabledIn: shortcut.getEnabledIn(),
              ...shortcut.getDisplayData(t, platform)
            }
          },
          handlers: {
            ...handlers,
            [shortcut.getName()]: (event) => {
              const result = shortcut.getCommand()({
                publicCommands,
                modalService: this.modalService,
                event,
                editor
              });
              if (result === "handled") {
                publisher.publish({ shortcutName: shortcut.getName() });
              }
              return result;
            }
          }
        };
      },
      { keyMap: {}, handlers: {}, allowChanges: false }
    );
  }
  /**
   * Registers shortcut, validates it has no conflicts.
   */
  register(shortcut) {
    const candidate = EditorKeyboardShortcut.of(shortcut);
    this.shortcuts.push(candidate);
  }
  /**
   * Registers shortcut, override in case of a conflicts.
   */
  mergeKeyboadShortcutProp(shortcut) {
    const candidate = EditorKeyboardShortcut.of(shortcut);
    const duplicate = this.hasDuplicate(candidate);
    if (duplicate) {
      this.shortcuts = this.shortcuts.filter((s) => !s.equals(duplicate));
    }
    this.shortcuts.push(candidate);
  }
  /**
   * Unregisters shortcut
   */
  unregister(shortcut) {
    this.shortcuts = this.shortcuts.filter((s) => !s.equals(EditorKeyboardShortcut.of(shortcut)));
  }
  /**
   * Filters shortcuts according to predicate
   */
  filter(predicate) {
    this.shortcuts = this.shortcuts.filter(predicate);
    return this;
  }
  /**
   * Gets shortcut array
   */
  asArray() {
    return this.shortcuts;
  }
  /**
   * Provides shortcut display data by given shortcut name for current locale and platform
   * Only returns data for shortcuts that should be displayed in the shortcut sheet
   */
  getShortcutDisplayData(name, t, platform) {
    const shortcut = this.shortcuts.find((s) => s.getName() === name);
    if (!shortcut || !shortcut.shouldShowInShortcutSheet()) {
      return {
        name: "Empty",
        description: "",
        keyCombinationText: "",
        tooltipHint: "",
        group: ""
      };
    }
    return shortcut.getDisplayData(t, platform);
  }
  /**
   * Builds a map of shortcut display data, grouped by shortcut groups
   * Only includes shortcuts that should be displayed in the shortcut sheet
   */
  getDisplayData(t, platform) {
    return this.shortcuts.filter((shortcut) => shortcut.shouldShowInShortcutSheet()).reduce(
      (map, shortcut) => ({
        ...map,
        [shortcut.getGroup()]: [
          ...map[shortcut.getGroup()] || [],
          shortcut.getDisplayData(t, platform)
        ]
      }),
      {}
    );
  }
}
export {
  EditorKeyboardShortcuts
};
//# sourceMappingURL=editor-keyboard-shortcuts.js.map
