const BASE_URL = import.meta.env.VITE_API_BASE_URL ?? "http://localhost:8000";

export function createEventSource(): EventSource {
	return new EventSource(`${BASE_URL}/events/stream`, {
		withCredentials: true,
	});
}

export async function probeEventStreamStatus(): Promise<number | null> {
	try {
		const controller = new AbortController();
		const timer = setTimeout(() => controller.abort(), 2000);
		const res = await fetch(`${BASE_URL}/events/stream`, {
			credentials: "include",
			signal: controller.signal,
		});
		clearTimeout(timer);
		return res.status;
	} catch {
		return null;
	}
}
