import { computeSyncFromCode, buildSourceBinding, buildMarkerComment, detectLanguage, } from '../engine/spec-source-sync.js';
import { getAllBindings, saveAllBindings, upsertBinding, removeBinding, getBindingsForSpec, } from '../storage/spec-bindings-store.js';
export async function handleSyncSpecFromCode(input) {
    const existing = await getAllBindings(input.projectPath);
    const { result, newBindings } = await computeSyncFromCode(input.projectPath, input.filePaths, existing);
    await saveAllBindings(input.projectPath, newBindings);
    const lines = [
        '**Sync Complete**',
        '',
        `| Metric | Value |`,
        `|--------|-------|`,
        `| Scanned Files | ${result.scannedFiles} |`,
        `| Bindings Found | ${result.bindingsFound} |`,
        `| Created | ${result.bindingsCreated} |`,
        `| Updated | ${result.bindingsUpdated} |`,
        `| Synced At | ${result.syncedAt} |`,
    ];
    if (result.errors.length > 0) {
        lines.push('', '**Errors**');
        for (const e of result.errors) {
            lines.push(`- ${e}`);
        }
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
export async function handleMarkAsSpecSource(input) {
    const binding = buildSourceBinding(input.specId, input.filePath, input.lineNumber);
    await upsertBinding(input.projectPath, binding);
    const lang = detectLanguage(input.filePath);
    const marker = buildMarkerComment(input.specId, lang);
    const lines = [
        `Binding created: **${input.specId}** → \`${input.filePath}\``,
        '',
        `Add this marker to your file:`,
        '```',
        marker,
        '```',
        '',
        `Binding ID: ${binding.id}`,
        `Language: ${binding.language}`,
        `Status: ${binding.status}`,
    ];
    if (input.lineNumber !== undefined) {
        lines.push(`Line: ${input.lineNumber}`);
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
export async function handleListSpecBindings(input) {
    if (input.action === 'unmark' && input.bindingId !== undefined) {
        await removeBinding(input.projectPath, input.bindingId);
        return {
            content: [{ type: 'text', text: `Binding ${input.bindingId} removed.` }],
        };
    }
    const bindings = input.specId !== undefined
        ? await getBindingsForSpec(input.projectPath, input.specId)
        : await getAllBindings(input.projectPath);
    if (bindings.length === 0) {
        return {
            content: [{ type: 'text', text: 'No spec bindings found.' }],
        };
    }
    const active = bindings.filter((b) => b.status === 'active').length;
    const stale = bindings.filter((b) => b.status === 'stale').length;
    const lines = [
        `**Spec Bindings** (${bindings.length} total | ${active} active | ${stale} stale)`,
        '',
    ];
    for (const b of bindings) {
        const lineSuffix = b.lineNumber !== undefined ? `:${b.lineNumber}` : '';
        lines.push(`- \`${b.specId}\` → \`${b.filePath}${lineSuffix}\` [${b.status}]`);
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
//# sourceMappingURL=spec-source-handler.js.map