//#region src/tui/sse-consumer.ts
/**
* Consume an SSE stream from a `ReadableStream<Uint8Array>` (Node.js 22+ fetch body).
*
* Parses the standard SSE wire format:
*   event: <name>\n
*   data: <json>\n
*   id: <id>\n
*   \n
*
* Calls `onEvent` for each complete event block.
*/
async function consumeSSEStream(body, onEvent, signal) {
	const reader = body.getReader();
	const decoder = new TextDecoder();
	let buffer = "";
	let currentEvent = "";
	let currentData = "";
	let currentId = "";
	try {
		while (true) {
			if (signal?.aborted) break;
			const { done, value } = await reader.read();
			if (done) break;
			buffer += decoder.decode(value, { stream: true });
			const lines = buffer.split("\n");
			buffer = lines.pop() ?? "";
			for (const line of lines) {
				if (line === "") {
					if (currentData) onEvent({
						event: currentEvent || "message",
						data: currentData,
						id: currentId || void 0
					});
					currentEvent = "";
					currentData = "";
					currentId = "";
					continue;
				}
				if (line.startsWith(":")) continue;
				const colonIndex = line.indexOf(":");
				if (colonIndex === -1) continue;
				const field = line.slice(0, colonIndex);
				const valueStart = line[colonIndex + 1] === " " ? colonIndex + 2 : colonIndex + 1;
				const fieldValue = line.slice(valueStart);
				switch (field) {
					case "event":
						currentEvent = fieldValue;
						break;
					case "data":
						currentData = currentData ? `${currentData}\n${fieldValue}` : fieldValue;
						break;
					case "id":
						currentId = fieldValue;
						break;
				}
			}
		}
	} finally {
		buffer += decoder.decode();
		reader.releaseLock();
	}
}
/** Parse SSE data field as JSON, returning null on failure. */
function parseSSEData(data) {
	try {
		return JSON.parse(data);
	} catch {
		return null;
	}
}
//#endregion
export { consumeSSEStream, parseSSEData };

//# sourceMappingURL=sse-consumer.js.map