import { formatPersistentLogEventType, formatPersistentLogStage, formatPersistentLogStatus, } from "./persistentLogLabels";
function normalizeCopyValue(value) {
    if (!value)
        return null;
    const trimmed = value.trim();
    return trimmed.length > 0 ? trimmed : null;
}
function formatDuration(durationMs) {
    if (typeof durationMs !== "number" || !Number.isFinite(durationMs) || durationMs < 0) {
        return null;
    }
    if (durationMs < 1000)
        return `${durationMs}ms`;
    return `${(durationMs / 1000).toFixed(durationMs >= 10000 ? 0 : 1)}s`;
}
function formatCreatedAt(value) {
    const createdAt = new Date(value);
    if (Number.isNaN(createdAt.getTime())) {
        return value;
    }
    return `${createdAt.toLocaleString()} (${createdAt.toISOString()})`;
}
function appendField(lines, label, value) {
    const normalized = normalizeCopyValue(value);
    if (!normalized)
        return;
    lines.push(`${label}: ${normalized}`);
}
function appendBlock(lines, label, value) {
    const normalized = normalizeCopyValue(value);
    if (!normalized)
        return;
    lines.push(`${label}:\n${normalized}`);
}
export function formatPersistentLogEntryForCopy(entry, index) {
    const lines = [];
    lines.push(index ? `Entry ${index}` : "Entry");
    lines.push(`Time: ${formatCreatedAt(entry.createdAt)}`);
    lines.push(`Type: ${formatPersistentLogEventType(entry.eventType)}`);
    if (entry.stage) {
        lines.push(`Step: ${formatPersistentLogStage(entry.stage)}`);
    }
    if (entry.status) {
        lines.push(`State: ${formatPersistentLogStatus(entry.status)}`);
    }
    appendField(lines, "Source", entry.source);
    if (typeof entry.success === "boolean") {
        lines.push(`Outcome: ${entry.success ? "Success" : "Failure"}`);
    }
    if (typeof entry.seq === "number") {
        lines.push(`Seq: ${entry.seq}`);
    }
    appendField(lines, "Duration", formatDuration(entry.durationMs));
    appendField(lines, "Agent ID", entry.agentId);
    appendField(lines, "Parent Agent ID", entry.parentAgentId);
    appendField(lines, "Project ID", entry.projectId);
    appendField(lines, "Task ID", entry.taskId);
    appendField(lines, "Run ID", entry.runId);
    appendField(lines, "Session ID", entry.sessionId);
    appendField(lines, "User ID", entry.userId);
    appendField(lines, "Message ID", entry.messageId);
    appendField(lines, "Tool Call ID", entry.toolCallId);
    appendBlock(lines, "Summary", entry.summary);
    appendBlock(lines, "Error", entry.error);
    appendBlock(lines, "Payload", entry.payloadPreview);
    appendBlock(lines, "Result", entry.resultPreview);
    appendBlock(lines, "Metadata", entry.metadataJson);
    return lines.join("\n");
}
export function formatPersistentLogsForCopy(entries, options) {
    if (entries.length === 0)
        return "";
    const header = options?.title?.trim() || "Agent and taskboard logs";
    return [
        header,
        `Entries: ${entries.length}`,
        ...entries.map((entry, index) => formatPersistentLogEntryForCopy(entry, index + 1)),
    ].join("\n\n");
}
//# sourceMappingURL=persistentLogCopy.js.map