/**
 * Transcript uploader
 *
 * Handles reading, parsing, and uploading transcripts to the registry.
 */
import * as fs from "fs/promises";
import { transcriptApi } from "../../../api/transcript.js";
import { debug } from "../../../cli/logger.js";
/**
 * Parse a JSONL transcript file into an array of messages
 *
 * @param args - Configuration arguments
 * @param args.content - Raw file content
 *
 * @returns Array of parsed messages (skips invalid lines)
 */
const parseTranscript = (args) => {
    const { content } = args;
    const lines = content.trim().split("\n");
    const messages = [];
    for (const line of lines) {
        if (!line.trim())
            continue;
        try {
            const parsed = JSON.parse(line);
            messages.push(parsed);
        }
        catch {
            // Skip invalid JSON lines
            debug({ message: `Skipping invalid JSON line in transcript` });
        }
    }
    return messages;
};
/**
 * Extract sessionId from transcript messages
 *
 * @param args - Configuration arguments
 * @param args.messages - Parsed transcript messages
 *
 * @returns Session ID or null if not found
 */
const extractSessionId = (args) => {
    const { messages } = args;
    for (const msg of messages) {
        if (msg.sessionId) {
            return msg.sessionId;
        }
    }
    return null;
};
/**
 * Process a transcript file for upload
 *
 * Reads the transcript, extracts sessionId, and uploads to the registry.
 * File deletion is NOT handled here — callers are responsible for cleanup
 * based on the user's garbage collection preference.
 *
 * @param args - Configuration arguments
 * @param args.transcriptPath - Path to the .jsonl transcript file
 * @param args.projectName - Optional project name for server-side association
 * @param args.orgId - Optional organization ID for upload destination
 * @param args.skillsetName - Optional active skillset name for server-side association
 *
 * @returns True if upload succeeded, false otherwise
 */
export const processTranscriptForUpload = async (args) => {
    const { transcriptPath, projectName, orgId, skillsetName } = args;
    // Read transcript file
    let content;
    try {
        content = await fs.readFile(transcriptPath, "utf-8");
    }
    catch (err) {
        debug({ message: `Failed to read transcript: ${transcriptPath}` });
        return false;
    }
    // Parse transcript
    const messages = parseTranscript({ content });
    if (messages.length === 0) {
        debug({ message: `Transcript has no valid messages: ${transcriptPath}` });
        return false;
    }
    // Extract sessionId
    const sessionId = extractSessionId({ messages });
    if (sessionId == null) {
        debug({ message: `Transcript has no sessionId: ${transcriptPath}` });
        return false;
    }
    // Upload transcript
    try {
        await transcriptApi.upload({
            sessionId,
            messages,
            ...(projectName != null && { projectName }),
            ...(orgId != null && { orgId }),
            ...(skillsetName != null && { skillsetName }),
        });
        debug({ message: `Uploaded transcript: ${sessionId}` });
        return true;
    }
    catch (err) {
        debug({
            message: `Failed to upload transcript ${sessionId}: ${err instanceof Error ? err.message : String(err)}`,
        });
        return false;
    }
};
//# sourceMappingURL=uploader.js.map