import { getLogger } from "../../../logger.js";

//#region src/domains/run/stream/durable-stream-helper.ts
const logger = getLogger("WritableBackedVercelWriter");
const encoder = new TextEncoder();
/**
* A HonoSSEStream adapter that writes SSE-formatted data to a WDK WritableStream.
* Used during durable workflow execution so SSE events are persisted and
* clients can reconnect via getRun(runId).readable.
*/
var WritableBackedHonoSSEStream = class {
	writer;
	constructor(writable) {
		this.writer = writable.getWriter();
	}
	async writeSSE(message) {
		let text = "";
		if (message.event) text += `event: ${message.event}\n`;
		if (message.id) text += `id: ${message.id}\n`;
		text += `data: ${message.data}\n\n`;
		await this.writer.write(encoder.encode(text));
	}
	async sleep(ms) {
		await new Promise((resolve) => setTimeout(resolve, ms));
	}
	async close() {
		try {
			await this.writer.close();
		} catch {}
	}
	releaseLock() {
		this.writer.releaseLock();
	}
};
/**
* A VercelUIWriter adapter that writes Vercel data stream SSE events to a WDK WritableStream.
* Used during durable workflow execution via the /chat endpoint so the Vercel AI SDK client
* can consume the stream directly.
*/
var WritableBackedVercelWriter = class {
	writer;
	constructor(writable) {
		this.writer = writable.getWriter();
	}
	write(chunk) {
		const bytes = encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`);
		this.writer.write(bytes).catch((err) => {
			logger.warn({ err }, "Failed to write to durable stream");
		});
	}
	merge(_stream) {}
	releaseLock() {
		this.writer.releaseLock();
	}
	async close() {
		try {
			await this.writer.close();
		} catch {}
	}
};

//#endregion
export { WritableBackedHonoSSEStream, WritableBackedVercelWriter };