// tools/consult-docs.ts — Fetch official documentation
// Uses web-fetcher engine: consultDocs()
import { t, ti } from '../i18n/index.js';
import { knowledgeStore } from '../storage/index.js';
import { consultDocs } from '../engine/web-fetcher.js';
export async function handleConsultDocs(args) {
    const { topic, projectId, framework } = args;
    const knowledge = await knowledgeStore.getKnowledge(projectId);
    if (!knowledge) {
        return {
            content: [
                {
                    type: 'text',
                    text: ti('errors.projectNotFound', { projectId }),
                },
            ],
            isError: true,
        };
    }
    // Auto-detect framework from project knowledge if not provided
    const resolvedFramework = framework ?? knowledge.framework ?? knowledge.language;
    const result = await consultDocs(topic, resolvedFramework);
    // Format output
    const sourceLabel = result.source === 'web' ? 'live' : result.source === 'cache' ? 'cached' : 'offline';
    const staleNote = result.stale ? ' (stale)' : '';
    const lines = [
        `## ${t('tools.consult_docs.description')}`,
        '',
        `**Framework:** ${result.framework}`,
        `**Topic:** ${result.topic}`,
        `**Source:** ${sourceLabel}${staleNote}`,
        '',
        `### Documentation URL`,
        '',
        `[${result.framework} docs for "${result.topic}"](${result.docsUrl})`,
        '',
        '### Summary',
        '',
        result.summary,
        '',
    ];
    if (result.codeExamples.length > 0) {
        lines.push('### Code Examples');
        lines.push('');
        lines.push('```');
        for (const example of result.codeExamples) {
            lines.push(example);
        }
        lines.push('```');
        lines.push('');
    }
    if (result.bestPractices.length > 0) {
        lines.push('### Best Practices');
        lines.push('');
        for (const practice of result.bestPractices) {
            lines.push(`- ${practice}`);
        }
        lines.push('');
    }
    // Additional context from project docs registry
    if (Object.keys(knowledge.docsRegistry).length > 0) {
        lines.push('### Project Docs Registry');
        lines.push('');
        lines.push('Additional documentation sources configured for this project:');
        lines.push('');
        for (const [key, value] of Object.entries(knowledge.docsRegistry)) {
            lines.push(`- **${key}:** [${value.base}](${value.base})`);
        }
    }
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
    };
}
//# sourceMappingURL=consult-docs.js.map