/**
 * Keyboard Bindings
 *
 * All keyboard shortcut bindings for the DEFAULT_CONTEXT.
 * Separated from pointer bindings for readability.
 *
 * Extracted from pointer-contexts.ts in v1.7.0.
 *
 * @since 1.7.0
 */

import type { GuardContext } from './types';

const isCommandShortcut = (ctx: GuardContext) =>
	!ctx.commandLineVisible;

/**
 * All keyboard shortcut gesture bindings.
 * Each binding maps a key pattern to an action ID.
 */
export const KEYBOARD_BINDINGS = [
	{
		id: 'slash-open-command-line',
		pattern: { kind: 'key', phase: 'down', code: 'Slash', modifiers: { ctrl: false, meta: false, shift: false } },
		actionId: 'open-command-line',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'escape-fallback',
		pattern: { kind: 'key', phase: 'down', key: 'Escape' },
		actionId: 'escape-input',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'delete-selection',
		pattern: { kind: 'key', phase: 'down', key: 'Delete' },
		actionId: 'delete-selection',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'backspace-delete-selection',
		pattern: { kind: 'key', phase: 'down', key: 'Backspace', modifiers: { ctrl: false, meta: false } },
		actionId: 'delete-selection',
		when: isCommandShortcut,
		consumeInput: true,
	},
	// Search
	{
		id: 'open-search-ctrl-f',
		pattern: { kind: 'key', phase: 'down', key: 'f', modifiers: { ctrl: true, shift: false } },
		actionId: 'open-search',
		consumeInput: true,
	},
	{
		id: 'open-search-meta-f',
		pattern: { kind: 'key', phase: 'down', key: 'f', modifiers: { meta: true, shift: false } },
		actionId: 'open-search',
		consumeInput: true,
	},
	// Copy / Cut / Paste
	{
		id: 'copy-selection-ctrl-c',
		pattern: { kind: 'key', phase: 'down', key: 'c', modifiers: { ctrl: true, shift: false } },
		actionId: 'copy-selection',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'copy-selection-meta-c',
		pattern: { kind: 'key', phase: 'down', key: 'c', modifiers: { meta: true, shift: false } },
		actionId: 'copy-selection',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'cut-selection-ctrl-x',
		pattern: { kind: 'key', phase: 'down', key: 'x', modifiers: { ctrl: true, shift: false } },
		actionId: 'cut-selection',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'cut-selection-meta-x',
		pattern: { kind: 'key', phase: 'down', key: 'x', modifiers: { meta: true, shift: false } },
		actionId: 'cut-selection',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'paste-selection-ctrl-v',
		pattern: { kind: 'key', phase: 'down', key: 'v', modifiers: { ctrl: true, shift: false } },
		actionId: 'paste-selection',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'paste-selection-meta-v',
		pattern: { kind: 'key', phase: 'down', key: 'v', modifiers: { meta: true, shift: false } },
		actionId: 'paste-selection',
		when: isCommandShortcut,
		consumeInput: true,
	},
	// Duplicate
	{
		id: 'duplicate-selection-ctrl-d',
		pattern: { kind: 'key', phase: 'down', key: 'd', modifiers: { ctrl: true, shift: false } },
		actionId: 'duplicate-selection',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'duplicate-selection-meta-d',
		pattern: { kind: 'key', phase: 'down', key: 'd', modifiers: { meta: true, shift: false } },
		actionId: 'duplicate-selection',
		when: isCommandShortcut,
		consumeInput: true,
	},
	// Select All
	{
		id: 'select-all-ctrl-a',
		pattern: { kind: 'key', phase: 'down', key: 'a', modifiers: { ctrl: true, shift: false } },
		actionId: 'select-all',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'select-all-meta-a',
		pattern: { kind: 'key', phase: 'down', key: 'a', modifiers: { meta: true, shift: false } },
		actionId: 'select-all',
		when: isCommandShortcut,
		consumeInput: true,
	},
	// Merge
	{
		id: 'merge-selection-ctrl-m',
		pattern: { kind: 'key', phase: 'down', key: 'm', modifiers: { ctrl: true, shift: false } },
		actionId: 'merge-selection',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'merge-selection-meta-m',
		pattern: { kind: 'key', phase: 'down', key: 'm', modifiers: { meta: true, shift: false } },
		actionId: 'merge-selection',
		when: isCommandShortcut,
		consumeInput: true,
	},
	// Undo / Redo
	{
		id: 'undo-ctrl-z',
		pattern: { kind: 'key', phase: 'down', key: 'z', modifiers: { ctrl: true, shift: false } },
		actionId: 'undo',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'undo-meta-z',
		pattern: { kind: 'key', phase: 'down', key: 'z', modifiers: { meta: true, shift: false } },
		actionId: 'undo',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'redo-ctrl-shift-z',
		pattern: { kind: 'key', phase: 'down', key: 'z', modifiers: { ctrl: true, shift: true } },
		actionId: 'redo',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'redo-meta-shift-z',
		pattern: { kind: 'key', phase: 'down', key: 'z', modifiers: { meta: true, shift: true } },
		actionId: 'redo',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'redo-ctrl-y',
		pattern: { kind: 'key', phase: 'down', key: 'y', modifiers: { ctrl: true } },
		actionId: 'redo',
		when: isCommandShortcut,
		consumeInput: true,
	},
	{
		id: 'redo-meta-y',
		pattern: { kind: 'key', phase: 'down', key: 'y', modifiers: { meta: true } },
		actionId: 'redo',
		when: isCommandShortcut,
		consumeInput: true,
	},
] as const;
