"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var use_shortcuts_exports = {};
__export(use_shortcuts_exports, {
  useShortcuts: () => useShortcuts
});
module.exports = __toCommonJS(use_shortcuts_exports);
var import_react = require("react");
var import_ricos_context = require("./../../ricos-context/index.js");
var import_toolbars_v3 = require("./../../toolbars-v3/index.js");
var import_shortcut_definitions = require("./shortcut-definitions.js");
const parseKeySequence = (sequence) => {
  const parts = sequence.toLowerCase().split(/[-+]/);
  return {
    key: parts[parts.length - 1],
    ctrlKey: parts.includes("ctrl"),
    metaKey: parts.includes("meta") || parts.includes("cmd"),
    shiftKey: parts.includes("shift"),
    altKey: parts.includes("alt")
  };
};
const matchesKeyEvent = (event, parsedKey) => {
  const keyMatches = /^\d$/.test(parsedKey.key) ? event.code.toLowerCase() === `digit${parsedKey.key}` : event.key.toLowerCase() === parsedKey.key;
  return keyMatches && !!event.ctrlKey === parsedKey.ctrlKey && !!event.metaKey === parsedKey.metaKey && !!event.shiftKey === parsedKey.shiftKey && !!event.altKey === parsedKey.altKey;
};
const useShortcuts = () => {
  const shortcuts = (0, import_react.useContext)(import_ricos_context.ShortcutsContext);
  const { getPublicCommands, adapter } = (0, import_react.useContext)(import_ricos_context.EditorContext);
  const { t, platform, portal, isMobile } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  if (isMobile) {
    return;
  }
  const handleKeyDown = (0, import_react.useCallback)(
    (event) => {
      const isEditorFocused = adapter.tiptapEditor.view.hasFocus();
      const isToolbarFocused = portal?.querySelector(`[data-hook~=${import_toolbars_v3.FLOATING_FORMATTING_TOOLBAR}]`)?.contains(event.target);
      let focusedElement;
      if (isEditorFocused) {
        focusedElement = "editor";
      }
      if (isToolbarFocused) {
        focusedElement = "toolbar";
      }
      if (!focusedElement) {
        return;
      }
      const { handlers, keyMap } = shortcuts.getHotKeysProps({
        group: "ShortcutsSheet_Section_Formatting",
        publicCommands: getPublicCommands(),
        editor: adapter.tiptapEditor,
        t,
        platform
      });
      for (const [shortcutName, keyData] of Object.entries(keyMap)) {
        for (const sequence of keyData.sequences) {
          const parsedKey = parseKeySequence(sequence.toString());
          if (!matchesKeyEvent(event, parsedKey)) {
            continue;
          }
          const handler = handlers[shortcutName];
          if (handler) {
            const isEnabledInCurrentContext = keyData.enabledIn.includes(focusedElement);
            if (!isEnabledInCurrentContext) {
              continue;
            }
            const result = handler(event);
            if (result === "handled") {
              return;
            }
          }
        }
      }
    },
    [shortcuts, getPublicCommands, adapter, t, platform, portal]
  );
  (0, import_react.useEffect)(() => {
    const allShortcuts = [...import_shortcut_definitions.generalShortcuts, ...import_shortcut_definitions.navigationShortcuts];
    if (shortcuts.asArray().filter((s) => s.getName() === "OpenShortcutsSheet").length > 0) {
      return;
    }
    allShortcuts.forEach((shortcut) => {
      shortcuts.register(shortcut);
    });
  }, [shortcuts]);
  (0, import_react.useEffect)(() => {
    document.addEventListener("keydown", handleKeyDown);
    return () => {
      document.removeEventListener("keydown", handleKeyDown);
    };
  }, [handleKeyDown]);
};
//# sourceMappingURL=use-shortcuts.js.map
