// tools/check-versions.ts — Check latest framework/library versions
// Uses web-fetcher engine: checkVersions()
import { t, ti } from '../i18n/index.js';
import { knowledgeStore } from '../storage/index.js';
import { checkVersions } from '../engine/web-fetcher.js';
export async function handleCheckVersions(args) {
    const { projectId, includeDevDeps = false } = args;
    const knowledge = await knowledgeStore.getKnowledge(projectId);
    if (!knowledge) {
        return {
            content: [
                {
                    type: 'text',
                    text: ti('errors.projectNotFound', { projectId }),
                },
            ],
            isError: true,
        };
    }
    // Build a dependency map from project knowledge
    // The stack array provides high-level dependencies; real package.json parsing
    // happens at init_project time and is stored in knowledge.
    const dependencies = {};
    // Add framework as a dependency
    if (knowledge.framework) {
        dependencies[knowledge.framework] = 'latest';
    }
    // Add stack items as dependencies
    for (const item of knowledge.stack) {
        dependencies[item] = 'latest';
    }
    // Add detected linters
    for (const linter of knowledge.linting.detectedLinters) {
        if (linter.version) {
            dependencies[linter.tool] = linter.version;
        }
    }
    const result = await checkVersions(dependencies, includeDevDeps, knowledge.language);
    // Format output
    const lines = [
        `## ${t('tools.check_versions.description')}`,
        '',
        `**Project:** ${knowledge.projectPath}`,
        `**Language:** ${knowledge.language}` +
            (knowledge.framework ? ` / ${knowledge.framework}` : ''),
        '',
    ];
    if (result.dependencies.length > 0) {
        lines.push('### Dependencies');
        lines.push('');
        lines.push('| Package | Current | Latest | Status | Deprecated |');
        lines.push('|---------|---------|--------|--------|------------|');
        for (const dep of result.dependencies) {
            const statusIcon = dep.updateType === 'up-to-date'
                ? 'OK'
                : dep.updateType === 'security'
                    ? 'SECURITY'
                    : dep.updateType === 'major'
                        ? 'MAJOR'
                        : dep.updateType === 'minor'
                            ? 'MINOR'
                            : 'PATCH';
            lines.push(`| ${dep.name} | ${dep.currentVersion} | ${dep.latestVersion} | ${statusIcon} | ${dep.deprecated ? 'Yes' : 'No'} |`);
        }
    }
    lines.push('');
    lines.push('### Summary');
    lines.push('');
    lines.push(`- **Security alerts:** ${result.securityAlerts}`);
    lines.push(`- **Updates available:** ${result.updatesAvailable}`);
    lines.push('');
    lines.push(`**Recommendation:** ${result.recommendation}`);
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
    };
}
//# sourceMappingURL=check-versions.js.map