// tools/search-handler.ts — Handlers for search_specs and search_suggestions (SPEC-295)
import { listSpecs } from '../storage/spec-store.js';
import { hashProjectPath } from '../storage/base-store.js';
import { searchSpecs, getSuggestions } from '../engine/spec-searcher.js';
import { formatSuccess, formatError, toolResult } from './response-helpers.js';
// ---------------------------------------------------------------------------
// search_specs handler
// ---------------------------------------------------------------------------
export async function handleSearchSpecs(args) {
    if (!args.projectPath || args.projectPath.trim().length === 0) {
        return toolResult(formatError('projectPath is required', 'Provide the absolute path to your project root.'), undefined, true);
    }
    // SPEC-592 follow-up: Codex enterprise cross-workspace isolation gate
    const { guardProjectPathForCodex } = await import('../hosts/codex/workspace-scope.js');
    const searchGuard = guardProjectPathForCodex(args.projectPath);
    if (!searchGuard.allowed) {
        return toolResult(formatError('Access denied', searchGuard.reason), undefined, true);
    }
    const projectId = hashProjectPath(args.projectPath);
    const specs = await listSpecs(projectId);
    const response = searchSpecs(specs, args);
    const lines = [
        `Search Results — ${args.projectPath}`,
        `Found: ${String(response.total)} spec(s)${response.hasMore ? ' (more available)' : ''}`,
    ];
    if (args.query) {
        lines.push(`Query: "${args.query}"`);
    }
    lines.push('');
    if (response.items.length === 0) {
        lines.push('No specs match the given criteria.');
    }
    else {
        for (const item of response.items) {
            const fields = item.matchedFields.length > 0 ? ` [${item.matchedFields.join(', ')}]` : '';
            const tags = item.tags.length > 0 ? ` | tags: ${item.tags.join(', ')}` : '';
            lines.push(`**${item.specId}** — ${item.title}`);
            lines.push(`  Status: ${item.status} | Type: ${item.type}${tags}${fields}`);
        }
    }
    const body = lines.join('\n');
    return toolResult(formatSuccess('Search Results', body), response);
}
// ---------------------------------------------------------------------------
// search_suggestions handler
// ---------------------------------------------------------------------------
export async function handleSearchSuggestions(args) {
    if (!args.projectPath || args.projectPath.trim().length === 0) {
        return toolResult(formatError('projectPath is required', 'Provide the absolute path to your project root.'), undefined, true);
    }
    if (!args.query || args.query.trim().length === 0) {
        return toolResult(formatError('query is required', 'Provide a partial string to get suggestions for.'), undefined, true);
    }
    // SPEC-592 follow-up: Codex enterprise cross-workspace isolation gate
    const { guardProjectPathForCodex } = await import('../hosts/codex/workspace-scope.js');
    const suggestionsGuard = guardProjectPathForCodex(args.projectPath);
    if (!suggestionsGuard.allowed) {
        return toolResult(formatError('Access denied', suggestionsGuard.reason), undefined, true);
    }
    const projectId = hashProjectPath(args.projectPath);
    const specs = await listSpecs(projectId);
    const response = getSuggestions(specs, args);
    const field = args.field ?? 'tags';
    const lines = [`Suggestions for "${args.query}" in field: ${field}`, ''];
    if (response.suggestions.length === 0) {
        lines.push('No suggestions found.');
    }
    else {
        lines.push(response.suggestions.join(', '));
    }
    const body = lines.join('\n');
    return toolResult(formatSuccess('Suggestions', body), response);
}
//# sourceMappingURL=search-handler.js.map