/** Stable ids for docs keyboard shortcuts (user-configurable later). */
export type DocsShortcutId =
  | "file.open"
  | "file.print"
  | "edit.undo"
  | "edit.redo"
  | "edit.cut"
  | "edit.copy"
  | "edit.paste"
  | "edit.pastePlain"
  | "edit.selectAll"
  | "edit.findReplace"
  | "view.showNonPrintable"

export type DocsShortcutCategory = "file" | "edit" | "view"

/** Where the shortcut is handled. */
export type DocsShortcutScope = "document" | "editor"

/**
 * How the shortcut is executed.
 * - tiptap/browser: native handling when the editor is focused (listed for menus + future settings)
 * - custom: handled by our docs shortcut runtime
 */
export type DocsShortcutHandler = "tiptap" | "browser" | "custom"

export type DocsShortcutBinding = {
  key: string
  /** Cmd on macOS, Ctrl on Windows/Linux. Default true. */
  mod?: boolean
  shift?: boolean
  alt?: boolean
}

export type DocsShortcutDefinition = {
  id: DocsShortcutId
  /** Settings UI label (future). */
  label: string
  category: DocsShortcutCategory
  scope: DocsShortcutScope
  handler: DocsShortcutHandler
  defaultBinding: DocsShortcutBinding
  altBindings?: DocsShortcutBinding[]
}

export const DOCS_KEYBOARD_SHORTCUT_DEFINITIONS = [
  {
    id: "file.open",
    label: "Ouvrir",
    category: "file",
    scope: "document",
    handler: "custom",
    defaultBinding: { key: "o", mod: true },
  },
  {
    id: "file.print",
    label: "Imprimer",
    category: "file",
    scope: "document",
    handler: "custom",
    defaultBinding: { key: "p", mod: true },
  },
  {
    id: "edit.undo",
    label: "Annuler",
    category: "edit",
    scope: "editor",
    handler: "tiptap",
    defaultBinding: { key: "z", mod: true },
  },
  {
    id: "edit.redo",
    label: "Rétablir",
    category: "edit",
    scope: "editor",
    handler: "custom",
    defaultBinding: { key: "y", mod: true },
    altBindings: [{ key: "z", mod: true, shift: true }],
  },
  {
    id: "edit.cut",
    label: "Couper",
    category: "edit",
    scope: "editor",
    handler: "browser",
    defaultBinding: { key: "x", mod: true },
  },
  {
    id: "edit.copy",
    label: "Copier",
    category: "edit",
    scope: "editor",
    handler: "browser",
    defaultBinding: { key: "c", mod: true },
  },
  {
    id: "edit.paste",
    label: "Coller",
    category: "edit",
    scope: "editor",
    handler: "browser",
    defaultBinding: { key: "v", mod: true },
  },
  {
    id: "edit.pastePlain",
    label: "Coller sans la mise en forme",
    category: "edit",
    scope: "editor",
    handler: "custom",
    defaultBinding: { key: "v", mod: true, shift: true },
  },
  {
    id: "edit.selectAll",
    label: "Tout sélectionner",
    category: "edit",
    scope: "editor",
    handler: "tiptap",
    defaultBinding: { key: "a", mod: true },
  },
  {
    id: "edit.findReplace",
    label: "Rechercher et remplacer",
    category: "edit",
    scope: "editor",
    handler: "custom",
    defaultBinding: { key: "h", mod: true, shift: true },
  },
  {
    id: "view.showNonPrintable",
    label: "Afficher les caractères non imprimables",
    category: "view",
    scope: "document",
    handler: "custom",
    defaultBinding: { key: "p", mod: true, shift: true },
  },
] as const satisfies readonly DocsShortcutDefinition[]

export const DOCS_KEYBOARD_SHORTCUTS_BY_ID: Record<
  DocsShortcutId,
  DocsShortcutDefinition
> = Object.fromEntries(
  DOCS_KEYBOARD_SHORTCUT_DEFINITIONS.map((definition) => [definition.id, definition])
) as Record<DocsShortcutId, DocsShortcutDefinition>

/** User overrides persisted locally (future settings UI). */
export type DocsKeyboardShortcutsUserConfig = Partial<
  Record<DocsShortcutId, DocsShortcutBinding>
>

export const DOCS_KEYBOARD_SHORTCUTS_STORAGE_KEY = "ultidrive-docs-keyboard-shortcuts"
