import type { MindmapUiActionId } from "./mindmap-ui-schema";

type MindmapShortcutEventLike = {
  key: string;
  shiftKey?: boolean;
  ctrlKey?: boolean;
  metaKey?: boolean;
  altKey?: boolean;
};

export type MindmapShortcutGateInput = {
  debugChromeEnabled: boolean;
  bridgeReady: boolean;
  readonly: boolean;
  isComposing: boolean;
  isEditableTarget: boolean;
  targetInsideRoot: boolean;
  keyboardShortcutArmed: boolean;
};

export const resolveMindmapShortcutAction = (
  event: MindmapShortcutEventLike,
): MindmapUiActionId | null => {
  if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) return null;
  if (event.key === "Enter") return "insertSiblingAfter";
  if (event.key === "Tab" || event.key === "Insert") return "insertChild";
  if (event.key === "Delete" || event.key === "Backspace") return "deleteNode";
  if (event.key === "F2") return "editNode";
  return null;
};

export const shouldInterceptMindmapShortcut = (
  input: MindmapShortcutGateInput,
): boolean => {
  if (input.debugChromeEnabled) return false;
  if (!input.bridgeReady || input.readonly || input.isComposing) return false;
  if (input.isEditableTarget) return false;
  if (input.targetInsideRoot) return true;
  return input.keyboardShortcutArmed;
};
