/**
 * Keyboard Mapping Contexts
 *
 * Search, manipulate, and navigate keyboard contexts extracted from contexts.ts.
 */

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

// =============================================================================
// Search Context (priority 25)
// =============================================================================

export const SEARCH_CONTEXT: MappingContext = {
	id: 'search-active',
	priority: 25,
	enabled: true,
	bindings: [
		{
			id: 'search-next-enter',
			pattern: { kind: 'key', phase: 'down', key: 'Enter', modifiers: { shift: false } },
			actionId: 'search-next-result',
			when: (ctx) => ctx.isSearchActive && !ctx.commandLineVisible,
			consumeInput: true,
		},
		{
			id: 'search-prev-enter',
			pattern: { kind: 'key', phase: 'down', key: 'Enter', modifiers: { shift: true } },
			actionId: 'search-prev-result',
			when: (ctx) => ctx.isSearchActive && !ctx.commandLineVisible,
			consumeInput: true,
		},
		{
			id: 'search-next-ctrl-g',
			pattern: { kind: 'key', phase: 'down', key: 'g', modifiers: { ctrl: true, shift: false } },
			actionId: 'search-next-result',
			when: (ctx) => ctx.isSearchActive,
			consumeInput: true,
		},
		{
			id: 'search-prev-ctrl-g',
			pattern: { kind: 'key', phase: 'down', key: 'g', modifiers: { ctrl: true, shift: true } },
			actionId: 'search-prev-result',
			when: (ctx) => ctx.isSearchActive,
			consumeInput: true,
		},
		{
			id: 'search-next-meta-g',
			pattern: { kind: 'key', phase: 'down', key: 'g', modifiers: { meta: true, shift: false } },
			actionId: 'search-next-result',
			when: (ctx) => ctx.isSearchActive,
			consumeInput: true,
		},
		{
			id: 'search-prev-meta-g',
			pattern: { kind: 'key', phase: 'down', key: 'g', modifiers: { meta: true, shift: true } },
			actionId: 'search-prev-result',
			when: (ctx) => ctx.isSearchActive,
			consumeInput: true,
		},
	],
};

// =============================================================================
// Keyboard Interaction Contexts (priority 30 / 40)
// =============================================================================

export const KEYBOARD_MANIPULATE_CONTEXT: MappingContext = {
	id: 'keyboard:manipulate',
	priority: 30,
	enabled: true,
	bindings: [
		{
			id: 'manipulate-arrow-up',
			pattern: { kind: 'key', phase: 'down', key: 'ArrowUp', modifiers: { shift: false } },
			actionId: 'nudge-selection-up',
			when: (ctx) => ctx.keyboardInteractionMode === 'manipulate',
			consumeInput: true,
		},
		{
			id: 'manipulate-arrow-down',
			pattern: { kind: 'key', phase: 'down', key: 'ArrowDown', modifiers: { shift: false } },
			actionId: 'nudge-selection-down',
			when: (ctx) => ctx.keyboardInteractionMode === 'manipulate',
			consumeInput: true,
		},
		{
			id: 'manipulate-arrow-left',
			pattern: { kind: 'key', phase: 'down', key: 'ArrowLeft', modifiers: { shift: false } },
			actionId: 'nudge-selection-left',
			when: (ctx) => ctx.keyboardInteractionMode === 'manipulate',
			consumeInput: true,
		},
		{
			id: 'manipulate-arrow-right',
			pattern: { kind: 'key', phase: 'down', key: 'ArrowRight', modifiers: { shift: false } },
			actionId: 'nudge-selection-right',
			when: (ctx) => ctx.keyboardInteractionMode === 'manipulate',
			consumeInput: true,
		},
		{
			id: 'manipulate-shift-arrow-up',
			pattern: { kind: 'key', phase: 'down', key: 'ArrowUp', modifiers: { shift: true } },
			actionId: 'nudge-selection-up-large',
			when: (ctx) => ctx.keyboardInteractionMode === 'manipulate',
			consumeInput: true,
		},
		{
			id: 'manipulate-shift-arrow-down',
			pattern: { kind: 'key', phase: 'down', key: 'ArrowDown', modifiers: { shift: true } },
			actionId: 'nudge-selection-down-large',
			when: (ctx) => ctx.keyboardInteractionMode === 'manipulate',
			consumeInput: true,
		},
		{
			id: 'manipulate-shift-arrow-left',
			pattern: { kind: 'key', phase: 'down', key: 'ArrowLeft', modifiers: { shift: true } },
			actionId: 'nudge-selection-left-large',
			when: (ctx) => ctx.keyboardInteractionMode === 'manipulate',
			consumeInput: true,
		},
		{
			id: 'manipulate-shift-arrow-right',
			pattern: { kind: 'key', phase: 'down', key: 'ArrowRight', modifiers: { shift: true } },
			actionId: 'nudge-selection-right-large',
			when: (ctx) => ctx.keyboardInteractionMode === 'manipulate',
			consumeInput: true,
		},
		{
			id: 'manipulate-escape',
			pattern: { kind: 'key', phase: 'down', key: 'Escape' },
			actionId: 'exit-keyboard-manipulate-mode',
			when: (ctx) => ctx.keyboardInteractionMode === 'manipulate',
			consumeInput: true,
		},
	],
};

export const KEYBOARD_NAVIGATE_CONTEXT: MappingContext = {
	id: 'keyboard:navigate',
	priority: 40,
	enabled: true,
	bindings: [
		{
			id: 'navigate-arrow-up',
			pattern: { kind: 'key', phase: 'down', key: 'ArrowUp' },
			actionId: 'navigate-focus-up',
			when: (ctx) => ctx.keyboardInteractionMode === 'navigate',
			consumeInput: true,
		},
		{
			id: 'navigate-arrow-down',
			pattern: { kind: 'key', phase: 'down', key: 'ArrowDown' },
			actionId: 'navigate-focus-down',
			when: (ctx) => ctx.keyboardInteractionMode === 'navigate',
			consumeInput: true,
		},
		{
			id: 'navigate-arrow-left',
			pattern: { kind: 'key', phase: 'down', key: 'ArrowLeft' },
			actionId: 'navigate-focus-left',
			when: (ctx) => ctx.keyboardInteractionMode === 'navigate',
			consumeInput: true,
		},
		{
			id: 'navigate-arrow-right',
			pattern: { kind: 'key', phase: 'down', key: 'ArrowRight' },
			actionId: 'navigate-focus-right',
			when: (ctx) => ctx.keyboardInteractionMode === 'navigate',
			consumeInput: true,
		},
		{
			id: 'navigate-enter',
			pattern: { kind: 'key', phase: 'down', key: 'Enter' },
			actionId: 'enter-keyboard-manipulate-mode',
			when: (ctx) => ctx.keyboardInteractionMode === 'navigate',
			consumeInput: true,
		},
		{
			id: 'navigate-space',
			pattern: { kind: 'key', phase: 'down', code: 'Space' },
			actionId: 'activate-focused-node',
			when: (ctx) => ctx.keyboardInteractionMode === 'navigate',
			consumeInput: true,
		},
		{
			id: 'navigate-tab',
			pattern: { kind: 'key', phase: 'down', key: 'Tab', modifiers: { shift: false } },
			actionId: 'cycle-focus-forward',
			when: (ctx) => ctx.keyboardInteractionMode === 'navigate',
			consumeInput: true,
		},
		{
			id: 'navigate-shift-tab',
			pattern: { kind: 'key', phase: 'down', key: 'Tab', modifiers: { shift: true } },
			actionId: 'cycle-focus-backward',
			when: (ctx) => ctx.keyboardInteractionMode === 'navigate',
			consumeInput: true,
		},
	],
};
