// engine/spec-exporter.ts — Pure functions for exporting specs to CSV/Markdown — SPEC-298
// ---------------------------------------------------------------------------
// Column definitions
// ---------------------------------------------------------------------------
export const ALL_COLUMNS = [
    'id',
    'title',
    'status',
    'type',
    'scope',
    'difficulty',
    'risk',
    'target',
    'tags',
    'createdAt',
    'updatedAt',
    'devHours',
    'totalCostUsd',
    'estimatedModel',
];
// ---------------------------------------------------------------------------
// Value extraction
// ---------------------------------------------------------------------------
/**
 * Extract the string value for a given column from a spec.
 */
export function extractColumnValue(spec, column) {
    switch (column) {
        case 'id':
            return spec.id;
        case 'title':
            return spec.title;
        case 'status':
            return spec.status;
        case 'type':
            return spec.type;
        case 'scope':
            return spec.scope;
        case 'difficulty':
            return String(spec.difficulty);
        case 'risk':
            return spec.risk;
        case 'target':
            return spec.target;
        case 'tags':
            return spec.tags.join(';');
        case 'createdAt':
            return spec.createdAt;
        case 'updatedAt':
            return spec.updatedAt;
        case 'devHours':
            return spec.estimation.devHours.toString();
        case 'totalCostUsd':
            return spec.estimation.totalCostUsd.toString();
        case 'estimatedModel':
            return spec.estimation.recommendedModel;
    }
}
// ---------------------------------------------------------------------------
// CSV helpers (RFC 4180)
// ---------------------------------------------------------------------------
/**
 * Escape a CSV field value per RFC 4180.
 * If the value contains a comma, newline, or double-quote, wrap in quotes
 * and double any internal quotes.
 */
export function escapeCsv(value) {
    if (value.includes(',') || value.includes('\n') || value.includes('"')) {
        return `"${value.replace(/"/g, '""')}"`;
    }
    return value;
}
/**
 * Serialize specs to CSV format.
 * First row is the header, subsequent rows are data.
 */
export function specsToCsv(specs, columns) {
    const header = columns.map((c) => escapeCsv(c)).join(',');
    const rows = specs.map((spec) => columns.map((col) => escapeCsv(extractColumnValue(spec, col))).join(','));
    return [header, ...rows].join('\n');
}
// ---------------------------------------------------------------------------
// Markdown table helpers
// ---------------------------------------------------------------------------
/**
 * Serialize specs to a Markdown table format.
 */
export function specsToMarkdownTable(specs, columns) {
    const escapeCell = (value) => value.replace(/\|/g, '\\|');
    const header = `| ${columns.map((c) => escapeCell(c)).join(' | ')} |`;
    const separator = `| ${columns.map(() => '---').join(' | ')} |`;
    const rows = specs.map((spec) => `| ${columns.map((col) => escapeCell(extractColumnValue(spec, col))).join(' | ')} |`);
    return [header, separator, ...rows].join('\n');
}
// ---------------------------------------------------------------------------
// Filtering
// ---------------------------------------------------------------------------
/**
 * Filter specs by status, type, and tags from ExportInput.
 */
export function filterSpecs(specs, input) {
    return specs.filter((spec) => {
        if (input.status && input.status.length > 0) {
            if (!input.status.includes(spec.status)) {
                return false;
            }
        }
        if (input.type && input.type.length > 0) {
            if (!input.type.includes(spec.type)) {
                return false;
            }
        }
        if (input.tags && input.tags.length > 0) {
            const hasTag = input.tags.some((tag) => spec.tags.includes(tag));
            if (!hasTag) {
                return false;
            }
        }
        return true;
    });
}
//# sourceMappingURL=spec-exporter.js.map