/**
 * Built-in Selection Commands
 *
 * Commands for selection manipulation:
 * - selectAll: Select all nodes
 * - clearSelection: Clear selection
 * - invertSelection: Invert selection
 */

import type { CommandDefinition } from '../types';
import { registerCommand } from '../registry';

/**
 * Select all nodes
 */
export const selectAllCommand: CommandDefinition = {
	name: 'selectAll',
	description: 'Select all nodes in the graph',
	aliases: ['all'],
	category: 'selection',
	inputs: [],
	execute: async (_inputs, ctx) => {
		const { nodeKeysAtom, selectedNodeIdsAtom } = await import('../../core');
		const nodeKeys = ctx.get(nodeKeysAtom);
		ctx.set(selectedNodeIdsAtom, new Set(nodeKeys));
	},
};

/**
 * Clear selection
 */
export const clearSelectionCommand: CommandDefinition = {
	name: 'clearSelection',
	description: 'Clear all selection',
	aliases: ['deselect', 'clear'],
	category: 'selection',
	inputs: [],
	execute: async (_inputs, ctx) => {
		const { clearSelectionAtom } = await import('../../core');
		ctx.set(clearSelectionAtom);
	},
};

/**
 * Invert selection
 */
export const invertSelectionCommand: CommandDefinition = {
	name: 'invertSelection',
	description: 'Invert the current selection',
	aliases: ['invert'],
	category: 'selection',
	inputs: [],
	execute: async (_inputs, ctx) => {
		const { nodeKeysAtom, selectedNodeIdsAtom } = await import('../../core');
		const allNodeKeys = ctx.get(nodeKeysAtom);
		const currentSelection = ctx.selectedNodeIds;
		const invertedSelection = allNodeKeys.filter((id: string) => !currentSelection.has(id));
		ctx.set(selectedNodeIdsAtom, new Set(invertedSelection));
	},
};

/**
 * Register all selection commands
 */
export function registerSelectionCommands(): void {
	registerCommand(selectAllCommand);
	registerCommand(clearSelectionCommand);
	registerCommand(invertSelectionCommand);
}
