/**
 * Built-in Search Commands
 *
 * Commands for searching and filtering nodes:
 * - searchNodes: Search nodes by label/type
 * - clearSearch: Clear active search filter
 */

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

/**
 * Search nodes by label or type
 */
export const searchNodesCommand: CommandDefinition = {
	name: 'searchNodes',
	description: 'Search nodes by label, type, or ID',
	aliases: ['find', 'search'],
	category: 'selection',
	inputs: [
		{
			name: 'query',
			type: 'text',
			prompt: 'Search:',
			required: true,
		},
	],
	execute: async (inputs, ctx) => {
		const query = inputs.query as string;
		if (!query) return;

		const { setSearchQueryAtom } = await import('../../core/search-store');
		ctx.set(setSearchQueryAtom, query);
	},
};

/**
 * Clear active search
 */
export const clearSearchCommand: CommandDefinition = {
	name: 'clearSearch',
	description: 'Clear the active search filter',
	aliases: ['clearsearch'],
	category: 'selection',
	inputs: [],
	execute: async (_inputs, ctx) => {
		const { clearSearchAtom } = await import('../../core/search-store');
		ctx.set(clearSearchAtom);
	},
};

/**
 * Register all search commands
 */
export function registerSearchCommands(): void {
	registerCommand(searchNodesCommand);
	registerCommand(clearSearchCommand);
}
