//#region src/utils/logger/log-stream.ts
const subscribers = /* @__PURE__ */ new Set();
Number.MAX_VALUE;
/**
* Subscribe to log events
* 
* @param subscriber - Callback function to receive log entries
* @returns Unsubscribe function
*/
function subscribeToLogs(subscriber) {
	subscribers.add(subscriber);
	return () => subscribers.delete(subscriber);
}
/**
* Get number of active subscribers
*/
function getSubscriberCount() {
	return subscribers.size;
}
/**
* Check if any subscribers are active
*/
function hasSubscribers() {
	return subscribers.size > 0;
}
/**
* Emit a log entry to all subscribers
* 
* @internal - Called by the logger integration
*/
function emitLogEntry(entry) {
	if (subscribers.size === 0) return;
	for (const subscriber of subscribers) try {
		subscriber(entry);
	} catch {}
}
/**
* Create a log entry from raw data
* 
* @internal
*/
function createLogEntry(level, message, context) {
	return {
		timestamp: (/* @__PURE__ */ new Date()).toISOString(),
		level,
		message,
		...context
	};
}
/**
* Create an SSE handler for log streaming
* 
* Usage:
* ```typescript
* import { createLogStreamHandler } from './log-stream.js';
* 
* app.get('/api/logs/stream', createLogStreamHandler());
* ```
*/
function createLogStreamHandler() {
	return (req) => {
		const url = new URL(req.url);
		const levelsParam = url.searchParams.get("levels");
		const moduleFilter = url.searchParams.get("module");
		const validLevels = [
			"trace",
			"debug",
			"info",
			"warn",
			"error",
			"fatal",
			"silent"
		];
		const allowedLevels = levelsParam ? levelsParam.split(",").filter((l) => validLevels.includes(l)) : [
			"info",
			"warn",
			"error",
			"fatal"
		];
		const stream = new ReadableStream({ start(controller) {
			const encoder = new TextEncoder();
			const sendEvent = (data) => {
				const message = `data: ${JSON.stringify(data)}\n\n`;
				controller.enqueue(encoder.encode(message));
			};
			sendEvent({
				type: "connected",
				message: "Log stream started"
			});
			const unsubscribe = subscribeToLogs((entry) => {
				if (!allowedLevels.includes(entry.level)) return;
				if (moduleFilter && entry.module !== moduleFilter && entry.prefix !== moduleFilter) return;
				sendEvent(entry);
			});
			req.signal.addEventListener("abort", () => {
				unsubscribe();
				try {
					controller.close();
				} catch {}
			});
			const heartbeat = setInterval(() => {
				try {
					sendEvent({
						type: "heartbeat",
						subscribers: getSubscriberCount()
					});
				} catch {
					clearInterval(heartbeat);
				}
			}, 3e4);
			req.signal.addEventListener("abort", () => {
				clearInterval(heartbeat);
			});
		} });
		return new Response(stream, { headers: {
			"Content-Type": "text/event-stream",
			"Cache-Control": "no-cache",
			"Connection": "keep-alive",
			"X-Accel-Buffering": "no"
		} });
	};
}
/**
* Create a Hono-compatible SSE handler for log streaming
* 
* Usage:
* ```typescript
* import { createLogSSEHandler } from './log-stream.js';
* 
* app.get('/api/logs/stream', createLogSSEHandler());
* ```
*/
function createLogSSEHandler() {
	return async (c) => {
		const url = new URL(c.req.url);
		const levelsParam = url.searchParams.get("levels");
		const moduleFilter = url.searchParams.get("module");
		const validLevels = [
			"trace",
			"debug",
			"info",
			"warn",
			"error",
			"fatal",
			"silent"
		];
		const allowedLevels = levelsParam ? levelsParam.split(",").filter((l) => validLevels.includes(l)) : [
			"info",
			"warn",
			"error",
			"fatal"
		];
		const stream = new ReadableStream({ start(controller) {
			const encoder = new TextEncoder();
			const sendEvent = (data) => {
				const message = `data: ${JSON.stringify(data)}\n\n`;
				controller.enqueue(encoder.encode(message));
			};
			sendEvent({
				type: "connected",
				message: "Log stream started"
			});
			const unsubscribe = subscribeToLogs((entry) => {
				if (!allowedLevels.includes(entry.level)) return;
				if (moduleFilter && entry.module !== moduleFilter && entry.prefix !== moduleFilter) return;
				sendEvent(entry);
			});
			c.req.raw.signal.addEventListener("abort", () => {
				unsubscribe();
				try {
					controller.close();
				} catch {}
			});
			const heartbeat = setInterval(() => {
				try {
					sendEvent({
						type: "heartbeat",
						subscribers: getSubscriberCount()
					});
				} catch {
					clearInterval(heartbeat);
				}
			}, 3e4);
			c.req.raw.signal.addEventListener("abort", () => {
				clearInterval(heartbeat);
			});
		} });
		return new Response(stream, { headers: {
			"Content-Type": "text/event-stream",
			"Cache-Control": "no-cache",
			"Connection": "keep-alive"
		} });
	};
}
var log_stream_default = {
	subscribeToLogs,
	getSubscriberCount,
	hasSubscribers,
	emitLogEntry,
	createLogEntry,
	createLogStreamHandler,
	createLogSSEHandler
};
//#endregion
export { createLogSSEHandler, log_stream_default as default };

//# sourceMappingURL=log-stream.js.map