/**
 * SSE Format Parsers for different AI providers
 * Handles parsing Server-Sent Events from OpenAI, Anthropic (Claude), and Google Gemini APIs
 */
/**
 * OpenAI streaming format parser
 * Format: data: {"choices":[{"delta":{"content":"token"}}]}
 * End signal: data: [DONE]
 *
 * @param line - Raw SSE line from stream
 * @returns Extracted token or null
 *
 * @example
 * parseOpenAI('data: {"choices":[{"delta":{"content":"Hello"}}]}')
 * // Returns: "Hello"
 */
const parseOpenAI = (line) => {
    var _a, _b, _c;
    if (!line.startsWith('data: '))
        return null;
    const data = line.slice(6).trim();
    // Handle stream end signal
    if (data === '[DONE]')
        return null;
    try {
        const parsed = JSON.parse(data);
        return ((_c = (_b = (_a = parsed.choices) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.delta) === null || _c === void 0 ? void 0 : _c.content) || null;
    }
    catch (_d) {
        // Invalid JSON - skip this line
        return null;
    }
};
/**
 * Anthropic (Claude) streaming format parser
 * Format:
 *   event: content_block_delta
 *   data: {"delta":{"type":"text_delta","text":"token"}}
 *
 * @param line - Raw SSE line from stream
 * @returns Extracted token or null
 *
 * @example
 * parseAnthropic('data: {"delta":{"type":"text_delta","text":"Hello"}}')
 * // Returns: "Hello"
 */
const parseAnthropic = (line) => {
    var _a, _b;
    if (!line.startsWith('data: '))
        return null;
    const data = line.slice(6).trim();
    try {
        const parsed = JSON.parse(data);
        // Anthropic sends different event types - we only care about text deltas
        if (parsed.type === 'content_block_delta') {
            return ((_a = parsed.delta) === null || _a === void 0 ? void 0 : _a.text) || null;
        }
        // Also handle direct delta format
        if (((_b = parsed.delta) === null || _b === void 0 ? void 0 : _b.type) === 'text_delta') {
            return parsed.delta.text || null;
        }
        return null;
    }
    catch (_c) {
        // Invalid JSON - skip this line
        return null;
    }
};
/**
 * Google Gemini streaming format parser
 * Format: data: {"candidates":[{"content":{"parts":[{"text":"token"}]}}]}
 *
 * @param line - Raw SSE line from stream
 * @returns Extracted token or null
 *
 * @example
 * parseGemini('data: {"candidates":[{"content":{"parts":[{"text":"Hello"}]}}]}')
 * // Returns: "Hello"
 */
const parseGemini = (line) => {
    var _a, _b, _c, _d, _e;
    if (!line.startsWith('data: '))
        return null;
    const data = line.slice(6).trim();
    try {
        const parsed = JSON.parse(data);
        return ((_e = (_d = (_c = (_b = (_a = parsed.candidates) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.content) === null || _c === void 0 ? void 0 : _c.parts) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.text) || null;
    }
    catch (_f) {
        // Invalid JSON - skip this line
        return null;
    }
};
/**
 * Parser registry mapping format names to parser functions
 */
const SSE_PARSERS = {
    'openai': parseOpenAI,
    'anthropic': parseAnthropic,
    'google-gemini': parseGemini,
};
/**
 * Parse an SSE line using the specified format parser
 *
 * @param line - Raw SSE line from stream
 * @param format - AI provider format or custom parser function
 * @returns Extracted token or null
 *
 * @example
 * // Using predefined format
 * parseSSELine('data: {...}', 'openai')
 *
 * // Using custom parser
 * parseSSELine('data: {...}', (line) => customParser(line))
 */
const parseSSELine = (line, format) => {
    var _a;
    if (typeof format === 'function') {
        return format(line);
    }
    return ((_a = SSE_PARSERS[format]) === null || _a === void 0 ? void 0 : _a.call(SSE_PARSERS, line)) || null;
};

export { SSE_PARSERS, parseAnthropic, parseGemini, parseOpenAI, parseSSELine };
