// tools/spec-export-handler.ts — Handlers for export_specs_csv and export_specs_markdown — SPEC-298
import { writeFile } from 'node:fs/promises';
import { listSpecs } from '../storage/spec-store.js';
import { hashProjectPath } from '../storage/base-store.js';
import { filterSpecs, specsToCsv, specsToMarkdownTable, ALL_COLUMNS, } from '../engine/spec-exporter.js';
import { formatSuccess, formatError, toolResult } from './response-helpers.js';
// ---------------------------------------------------------------------------
// Shared logic
// ---------------------------------------------------------------------------
async function buildExport(input, format) {
    const projectId = hashProjectPath(input.projectPath);
    const allSpecs = await listSpecs(projectId);
    const filtered = filterSpecs(allSpecs, input);
    const columns = input.columns && input.columns.length > 0 ? input.columns : ALL_COLUMNS;
    const content = format === 'csv' ? specsToCsv(filtered, columns) : specsToMarkdownTable(filtered, columns);
    let filePath;
    if (input.outputPath) {
        await writeFile(input.outputPath, content, 'utf8');
        filePath = input.outputPath;
    }
    const result = {
        rowCount: filtered.length,
        columns,
        format,
        content,
        filePath,
    };
    return { text: content, result };
}
// ---------------------------------------------------------------------------
// CSV handler
// ---------------------------------------------------------------------------
export async function handleExportSpecsCsv(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);
    }
    const { text, result } = await buildExport(args, 'csv');
    const summary = [
        `Exported ${String(result.rowCount)} specs to CSV`,
        `Columns: ${result.columns.join(', ')}`,
        result.filePath ? `Saved to: ${result.filePath}` : 'No file written (outputPath not provided)',
        '',
        text,
    ].join('\n');
    return toolResult(formatSuccess('Spec Export — CSV', summary), result);
}
// ---------------------------------------------------------------------------
// Markdown handler
// ---------------------------------------------------------------------------
export async function handleExportSpecsMarkdown(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);
    }
    const { text, result } = await buildExport(args, 'markdown');
    const summary = [
        `Exported ${String(result.rowCount)} specs to Markdown table`,
        `Columns: ${result.columns.join(', ')}`,
        result.filePath ? `Saved to: ${result.filePath}` : 'No file written (outputPath not provided)',
        '',
        text,
    ].join('\n');
    return toolResult(formatSuccess('Spec Export — Markdown', summary), result);
}
//# sourceMappingURL=spec-export-handler.js.map