/**
 * Built-in Mapping Contexts
 *
 * Default bindings, palm rejection, and input-mode contexts.
 * All are plain data with runtime conditions expressed via guard predicates.
 *
 * Keyboard contexts are in ./keyboard-contexts.ts
 * Default + input mode contexts are in ./pointer-contexts.ts
 */

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

// Re-export all contexts for backward compat
export {
	SEARCH_CONTEXT,
	KEYBOARD_MANIPULATE_CONTEXT,
	KEYBOARD_NAVIGATE_CONTEXT,
} from './keyboard-contexts';

export {
	DEFAULT_CONTEXT,
	PICK_NODE_CONTEXT,
	PICK_NODES_CONTEXT,
	PICK_POINT_CONTEXT,
	INPUT_MODE_CONTEXTS,
} from './pointer-contexts';

// =============================================================================
// Palm Rejection Context (priority 0 — highest)
// =============================================================================

/**
 * When a stylus is active, block finger taps and remap finger-on-node
 * drags to pan. Pinch and scroll are intentionally NOT blocked.
 */
export const PALM_REJECTION_CONTEXT: MappingContext = {
	id: 'palm-rejection',
	priority: 0,
	enabled: true,
	bindings: [
		{
			id: 'pr-finger-tap',
			pattern: { type: 'tap', source: 'finger' },
			actionId: 'none',
			when: (ctx) => ctx.isStylusActive,
			consumeInput: true,
		},
		{
			id: 'pr-finger-dtap',
			pattern: { type: 'double-tap', source: 'finger' },
			actionId: 'none',
			when: (ctx) => ctx.isStylusActive,
			consumeInput: true,
		},
		{
			id: 'pr-finger-ttap',
			pattern: { type: 'triple-tap', source: 'finger' },
			actionId: 'none',
			when: (ctx) => ctx.isStylusActive,
			consumeInput: true,
		},
		{
			id: 'pr-finger-lp',
			pattern: { type: 'long-press', source: 'finger' },
			actionId: 'none',
			when: (ctx) => ctx.isStylusActive,
			consumeInput: true,
		},
		{
			id: 'pr-finger-drag-node',
			pattern: { type: 'drag', subjectKind: 'node', source: 'finger' },
			actionId: 'pan',
			when: (ctx) => ctx.isStylusActive,
			consumeInput: true,
		},
	],
};

// =============================================================================
// Active Interaction Context (priority 15)
// =============================================================================

export const ACTIVE_INTERACTION_CONTEXT: MappingContext = {
	id: 'active-interaction',
	priority: 15,
	enabled: true,
	bindings: [
		{
			id: 'escape-cancel-active-interaction',
			pattern: { kind: 'key', phase: 'down', key: 'Escape' },
			actionId: 'cancel-active-input',
			when: (ctx) =>
				ctx.isDragging ||
				ctx.isSplitting ||
				Boolean(ctx.custom.isSelecting) ||
				Boolean(ctx.custom.isCreatingEdge),
			consumeInput: true,
		},
	],
};
