// tools/registry/search.ts — AC-5: Search specs in the registry (no auth required)
import { compactResult, formatTable } from '../output-formatter.js';
/** 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.
 * Tries the HTTP client first, falls back to the local registry client stub if unavailable.
 */
export async function handleRegistrySearch(args) {
    try {
        const filters = buildFilters(args);
        // Try real HTTP backend first; fall back to stub if null
        const { fetchRegistrySearch } = await import('../../engine/registry/http-client.js');
        const httpResult = await fetchRegistrySearch(filters);
        let result;
        if (httpResult !== null) {
            result = httpResult;
        }
        else {
            // Dynamic import — engine module may be created by another agent
            const { createRegistryClient } = (await import('../../engine/spec-registry/index.js'));
            const client = createRegistryClient();
            result = await client.searchSpecs(filters);
        }
        const formatted = {
            specs: result.data.map(formatSearchItem),
            pagination: result.meta,
        };
        const tableRows = result.data.map((s) => ({
            name: s.name,
            description: s.description,
            stack: s.stack.join(', '),
            difficulty: String(s.difficulty),
            downloads: String(s.downloads),
        }));
        const { page, limit, total } = formatted.pagination;
        const tableText = tableRows.length > 0
            ? formatTable(tableRows, ['name', 'description', 'stack', 'difficulty', 'downloads'])
            : 'No results found.';
        return compactResult([tableText, '', `Page ${page}/${Math.ceil(total / limit)} — ${total} total results`].join('\n'));
    }
    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