/**
 * Fetch the list of available indicator providers.
 * This is a lightweight call that doesn't require item IDs.
 */
export async function fetchIndicatorProviders(signal) {
    const response = await fetch("/parhelia/indicatorProviders", {
        credentials: "include",
        signal,
    });
    if (!response.ok) {
        throw new Error(`Failed to fetch indicator providers: ${response.statusText}`);
    }
    return response.json();
}
/**
 * Fetch indicators using NDJSON streaming (POST).
 * Calls onChunk for each chunk as it arrives.
 *
 * @param itemIds - Item IDs to fetch indicators for
 * @param providers - Optional filter for specific provider IDs
 * @param language - Language code
 * @param onChunk - Callback for each streaming chunk
 * @param signal - Optional AbortSignal for cancellation
 * @param sessionId - Optional session ID to exclude from presence indicators
 */
export async function fetchIndicatorsStream(itemIds, providers, language, onChunk, signal, sessionId) {
    if (itemIds.length === 0) {
        onChunk({ type: "providers", data: [] });
        onChunk({ type: "complete" });
        return;
    }
    const response = await fetch("/parhelia/indicators", {
        method: "POST",
        credentials: "include",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify({
            itemIds,
            providers: providers && providers.length > 0 ? providers : undefined,
            language,
            sessionId,
        }),
        signal,
    });
    if (!response.ok) {
        throw new Error(`Failed to fetch indicators: ${response.statusText}`);
    }
    const reader = response.body?.getReader();
    if (!reader) {
        throw new Error("Response body is not readable");
    }
    const decoder = new TextDecoder();
    let buffer = "";
    try {
        while (true) {
            const { done, value } = await reader.read();
            if (done)
                break;
            buffer += decoder.decode(value, { stream: true });
            const lines = buffer.split("\n");
            buffer = lines.pop() || ""; // Keep incomplete line in buffer
            for (const line of lines) {
                const trimmed = line.trim();
                if (trimmed) {
                    try {
                        const chunk = JSON.parse(trimmed);
                        onChunk(chunk);
                    }
                    catch (e) {
                        console.error("Failed to parse indicator chunk:", trimmed, e);
                    }
                }
            }
        }
        // Process any remaining data in buffer
        if (buffer.trim()) {
            try {
                const chunk = JSON.parse(buffer.trim());
                onChunk(chunk);
            }
            catch (e) {
                console.error("Failed to parse final indicator chunk:", buffer, e);
            }
        }
    }
    finally {
        reader.releaseLock();
    }
}
//# sourceMappingURL=indicatorService.js.map