/**
 * Pointer & Default Mapping Contexts
 *
 * DEFAULT_CONTEXT assembled from pointer-bindings + keyboard-bindings.
 * Input mode contexts for pick operations.
 *
 * @since 1.7.0 — bindings extracted into keyboard-bindings.ts and pointer-bindings.ts
 */

import type { GuardContext, MappingContext } from './types';
import { POINTER_BINDINGS } from './pointer-bindings';
import { KEYBOARD_BINDINGS } from './keyboard-bindings';

// Re-export for consumers
export { POINTER_BINDINGS } from './pointer-bindings';
export { KEYBOARD_BINDINGS } from './keyboard-bindings';

// =============================================================================
// Default Context (priority 100 — lowest built-in)
// =============================================================================

export const DEFAULT_CONTEXT: MappingContext = {
	id: 'default',
	priority: 100,
	enabled: true,
	bindings: [
		...POINTER_BINDINGS,
		...KEYBOARD_BINDINGS,
	],
};

// =============================================================================
// Input Mode Contexts (priority 5 — override most things)
// =============================================================================

export const PICK_NODE_CONTEXT: MappingContext = {
	id: 'input-mode:pickNode',
	priority: 5,
	enabled: true,
	bindings: [
		{
			id: 'pick-tap-node',
			pattern: { type: 'tap', subjectKind: 'node' },
			actionId: 'resolve-pick-node',
			consumeInput: true,
		},
		{
			id: 'pick-cancel-bg',
			pattern: { type: 'tap', subjectKind: 'background' },
			actionId: 'cancel-pick',
			consumeInput: true,
		},
		{
			id: 'pick-cancel-key',
			pattern: { kind: 'key', phase: 'down', key: 'Escape' },
			actionId: 'cancel-pick',
			consumeInput: true,
		},
	],
};

export const PICK_NODES_CONTEXT: MappingContext = {
	id: 'input-mode:pickNodes',
	priority: 5,
	enabled: true,
	bindings: [
		{
			id: 'pick-tap-node',
			pattern: { type: 'tap', subjectKind: 'node' },
			actionId: 'resolve-pick-node',
			consumeInput: true,
		},
		{
			id: 'pick-done',
			pattern: { type: 'double-tap', subjectKind: 'background' },
			actionId: 'finish-pick-nodes',
			consumeInput: true,
		},
		{
			id: 'pick-cancel-bg',
			pattern: { type: 'tap', subjectKind: 'background', button: 2 },
			actionId: 'cancel-pick',
			consumeInput: true,
		},
		{
			id: 'pick-cancel-key',
			pattern: { kind: 'key', phase: 'down', key: 'Escape' },
			actionId: 'cancel-pick',
			consumeInput: true,
		},
	],
};

export const PICK_POINT_CONTEXT: MappingContext = {
	id: 'input-mode:pickPoint',
	priority: 5,
	enabled: true,
	bindings: [
		{
			id: 'pick-tap',
			pattern: { type: 'tap' },
			actionId: 'resolve-pick-point',
			consumeInput: true,
		},
		{
			id: 'pick-cancel-key',
			pattern: { kind: 'key', phase: 'down', key: 'Escape' },
			actionId: 'cancel-pick',
			consumeInput: true,
		},
	],
};

/**
 * Lookup table: input mode type → MappingContext to push.
 * 'normal' maps to null (no extra context).
 */
export const INPUT_MODE_CONTEXTS: Record<string, MappingContext | null> = {
	normal: null,
	pickNode: PICK_NODE_CONTEXT,
	pickNodes: PICK_NODES_CONTEXT,
	pickPoint: PICK_POINT_CONTEXT,
	text: null,
	select: null,
};
