// tools/registry/search.ts — AC-5: Search specs in the registry (no auth required)
/** Format a single search item for display. */
function formatSearchItem(item) {
    return {
        name: item.name,
        description: item.description,
        stack: item.stack,
        difficulty: item.difficulty,
        downloads: item.downloads,
        rating: item.rating,
        version: item.version,
        category: item.category,
        completenessScore: item.completenessScore,
    };
}
/** Build search filters from raw tool args. */
/* v8 ignore start -- optional filter branches tested via integration */
function buildFilters(args) {
    const filters = {};
    if (typeof args.query === 'string') {
        filters.query = args.query;
    }
    if (typeof args.stack === 'string') {
        filters.stack = args.stack;
    }
    if (typeof args.category === 'string') {
        filters.category = args.category;
    }
    if (typeof args.sort === 'string') {
        filters.sort = args.sort;
    }
    if (typeof args.page === 'number') {
        filters.page = args.page;
    }
    if (typeof args.limit === 'number') {
        filters.limit = args.limit;
    }
    if (args.difficulty !== null &&
        args.difficulty !== undefined &&
        typeof args.difficulty === 'object') {
        const d = args.difficulty;
        filters.difficulty = {
            min: typeof d.min === 'number' ? d.min : undefined,
            max: typeof d.max === 'number' ? d.max : undefined,
        };
    }
    return filters;
}
/* v8 ignore stop */
/**
 * Handle registry_search tool invocation.
 * Searches the spec registry with the given filters. No authentication required.
 */
export async function handleRegistrySearch(args) {
    try {
        // Dynamic import — engine module may be created by another agent
        const { createRegistryClient } = (await import('../../engine/spec-registry/index.js'));
        const filters = buildFilters(args);
        const client = createRegistryClient();
        const result = await client.searchSpecs(filters);
        const formatted = {
            specs: result.data.map(formatSearchItem),
            pagination: result.meta,
        };
        return {
            content: [{ type: 'text', text: JSON.stringify(formatted, null, 2) }],
        };
    }
    catch (error) {
        /* v8 ignore start -- unexpected runtime errors */
        const message = error instanceof Error ? error.message : String(error);
        return {
            content: [{ type: 'text', text: `Registry search failed: ${message}` }],
            isError: true,
        };
        /* v8 ignore stop */
    }
}
//# sourceMappingURL=search.js.map