import { useCallback, useContext, useEffect } from "react";
import { EditorContext, RicosContext, ShortcutsContext } from "./../../ricos-context/index.js";
import { FLOATING_FORMATTING_TOOLBAR } from "./../../toolbars-v3/index.js";
import { generalShortcuts, navigationShortcuts } from "./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 = useContext(ShortcutsContext);
  const { getPublicCommands, adapter } = useContext(EditorContext);
  const { t, platform, portal, isMobile } = useContext(RicosContext);
  if (isMobile) {
    return;
  }
  const handleKeyDown = useCallback(
    (event) => {
      const isEditorFocused = adapter.tiptapEditor.view.hasFocus();
      const isToolbarFocused = portal?.querySelector(`[data-hook~=${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]
  );
  useEffect(() => {
    const allShortcuts = [...generalShortcuts, ...navigationShortcuts];
    if (shortcuts.asArray().filter((s) => s.getName() === "OpenShortcutsSheet").length > 0) {
      return;
    }
    allShortcuts.forEach((shortcut) => {
      shortcuts.register(shortcut);
    });
  }, [shortcuts]);
  useEffect(() => {
    document.addEventListener("keydown", handleKeyDown);
    return () => {
      document.removeEventListener("keydown", handleKeyDown);
    };
  }, [handleKeyDown]);
};
export {
  useShortcuts
};
//# sourceMappingURL=use-shortcuts.js.map
