/**
 * Core export logic — export tasks to various portable formats.
 *
 * Extracted from CLI export command for dispatch layer access.
 *
 * @task T5323, T5328
 */
import { writeFile } from 'node:fs/promises';
import { getAccessor } from '../store/data-accessor.js';
function taskToCsvRow(task, delimiter) {
    const escapeField = (val) => {
        if (val.includes(delimiter) || val.includes('"') || val.includes('\n')) {
            return '"' + val.replace(/"/g, '""') + '"';
        }
        return val;
    };
    return [
        escapeField(task.id),
        escapeField(task.title),
        escapeField(task.status),
        escapeField(task.priority),
        escapeField(task.type ?? 'task'),
        escapeField(task.parentId ?? ''),
        escapeField(task.phase ?? ''),
        escapeField((task.depends ?? []).join(',')),
        escapeField(task.createdAt ?? ''),
    ].join(delimiter);
}
function taskToMarkdown(task) {
    const status = task.status === 'done' ? 'x' : ' ';
    const priority = task.priority === 'critical' ? '!!!' : task.priority === 'high' ? '!!' : '';
    return `- [${status}] **${task.id}** ${priority} ${task.title}`;
}
/**
 * Export tasks to a portable format.
 * Returns the formatted content and metadata.
 */
export async function exportTasks(params) {
    const accessor = await getAccessor(params.cwd);
    const queryResult = await accessor.queryTasks({});
    const projectMeta = await accessor.getMetaValue('project');
    let tasks = queryResult.tasks;
    if (params.status) {
        const statuses = params.status.split(',').map((s) => s.trim());
        tasks = tasks.filter((t) => statuses.includes(t.status));
    }
    if (params.parent) {
        tasks = tasks.filter((t) => t.parentId === params.parent);
    }
    if (params.phase) {
        tasks = tasks.filter((t) => t.phase === params.phase);
    }
    const format = params.format ?? 'json';
    let content;
    switch (format) {
        case 'json': {
            content = JSON.stringify({
                exportedAt: new Date().toISOString(),
                projectName: projectMeta?.name ?? 'Unknown',
                taskCount: tasks.length,
                tasks,
            }, null, 2);
            break;
        }
        case 'csv': {
            const header = 'id,title,status,priority,type,parentId,phase,depends,createdAt';
            const rows = tasks.map((t) => taskToCsvRow(t, ','));
            content = [header, ...rows].join('\n');
            break;
        }
        case 'tsv': {
            const header = 'id\ttitle\tstatus\tpriority\ttype\tparentId\tphase\tdepends\tcreatedAt';
            const rows = tasks.map((t) => taskToCsvRow(t, '\t'));
            content = [header, ...rows].join('\n');
            break;
        }
        case 'markdown': {
            const lines = [`# ${projectMeta?.name ?? 'Tasks'}\n`];
            const byStatus = new Map();
            for (const t of tasks) {
                const list = byStatus.get(t.status) ?? [];
                list.push(t);
                byStatus.set(t.status, list);
            }
            for (const [status, statusTasks] of byStatus) {
                lines.push(`\n## ${status}\n`);
                for (const t of statusTasks) {
                    lines.push(taskToMarkdown(t));
                }
            }
            content = lines.join('\n');
            break;
        }
        default:
            throw new Error(`Unknown format: ${format}. Valid: json, csv, tsv, markdown`);
    }
    if (params.output) {
        await writeFile(params.output, content);
        return { format, taskCount: tasks.length, file: params.output };
    }
    return { format, taskCount: tasks.length, content };
}
//# sourceMappingURL=export.js.map