import { saveApiSurface, loadApiSurfaces, loadApiSurfaceForSpec, } from '../storage/api-surfaces-store.js';
import { analyzeBreakingChanges, generateImpactReport, checkApiCompatibility, } from '../engine/dependency-impact-analyzer.js';
import { formatSuccess, formatError, toolResult } from './response-helpers.js';
// ---------------------------------------------------------------------------
// register_api_surface
// ---------------------------------------------------------------------------
export async function handleRegisterApiSurface(args) {
    if (!args.projectPath.trim()) {
        return toolResult(formatError('projectPath is required'), undefined, true);
    }
    if (!args.specId.trim()) {
        return toolResult(formatError('specId is required'), undefined, true);
    }
    if (!Array.isArray(args.surfaces) || args.surfaces.length === 0) {
        return toolResult(formatError('surfaces array must have at least one entry'), undefined, true);
    }
    const surface = {
        specId: args.specId,
        surfaces: args.surfaces,
        registeredAt: new Date().toISOString(),
    };
    await saveApiSurface(args.projectPath, surface);
    const body = [
        `Spec: ${args.specId}`,
        `Registered surfaces: ${String(args.surfaces.length)}`,
        ...args.surfaces.map((s) => `  - [${s.type}] ${s.definition.slice(0, 60)}`),
    ].join('\n');
    return toolResult(formatSuccess('API Surface Registered', body), surface);
}
// ---------------------------------------------------------------------------
// analyze_breaking_changes
// ---------------------------------------------------------------------------
export async function handleAnalyzeBreakingChanges(args) {
    if (!args.projectPath.trim()) {
        return toolResult(formatError('projectPath is required'), undefined, true);
    }
    if (!args.specId.trim()) {
        return toolResult(formatError('specId is required'), undefined, true);
    }
    if (!args.changeDescription.trim()) {
        return toolResult(formatError('changeDescription is required'), undefined, true);
    }
    const allSurfaces = await loadApiSurfaces(args.projectPath);
    const result = analyzeBreakingChanges(args.specId, args.changeDescription, allSurfaces);
    const lines = [
        `Source spec: ${result.sourceSpecId}`,
        `Overall impact: ${result.overallImpact.toUpperCase()}`,
        `Affected specs: ${String(result.affectedSpecs.length)}`,
        '',
    ];
    for (const a of result.affectedSpecs) {
        lines.push(`  [${a.impactLevel.toUpperCase()}] ${a.specId}: ${a.reason}`);
    }
    if (result.affectedSpecs.length === 0) {
        lines.push('  No affected specs found.');
    }
    return toolResult(formatSuccess('Breaking Changes Analysis', lines.join('\n')), result);
}
// ---------------------------------------------------------------------------
// dependency_impact_report
// ---------------------------------------------------------------------------
export async function handleDependencyImpactReport(args) {
    if (!args.projectPath.trim()) {
        return toolResult(formatError('projectPath is required'), undefined, true);
    }
    const allSurfaces = await loadApiSurfaces(args.projectPath);
    const result = analyzeBreakingChanges(args.specId, args.changeDescription, allSurfaces);
    const html = generateImpactReport(result);
    return toolResult(formatSuccess('Dependency Impact Report', `HTML report generated for spec ${args.specId}. ${String(result.affectedSpecs.length)} affected specs.\n\n${html.slice(0, 200)}...`), { html, result });
}
// ---------------------------------------------------------------------------
// check_api_compatibility
// ---------------------------------------------------------------------------
export async function handleCheckApiCompatibility(args) {
    if (!args.projectPath.trim()) {
        return toolResult(formatError('projectPath is required'), undefined, true);
    }
    if (!args.specId.trim()) {
        return toolResult(formatError('specId is required'), undefined, true);
    }
    const allSurfaces = await loadApiSurfaces(args.projectPath);
    const own = await loadApiSurfaceForSpec(args.projectPath, args.specId);
    const compat = checkApiCompatibility(args.specId, allSurfaces);
    const lines = [
        `Spec: ${args.specId}`,
        `Registered surfaces: ${String(compat.surfaceCount)}`,
        `Dependent specs: ${String(compat.dependents.length)}`,
    ];
    if (!own) {
        lines.push('  Warning: no API surfaces registered for this spec yet.');
    }
    if (compat.dependents.length > 0) {
        lines.push('', 'Dependents:');
        for (const d of compat.dependents) {
            lines.push(`  - ${d}`);
        }
    }
    return toolResult(formatSuccess('API Compatibility Check', lines.join('\n')), compat);
}
//# sourceMappingURL=dependency-impact-handler.js.map