import { createDraftStreamLoop } from "./draft-stream-loop.js";
//#region extensions/telegram/src/draft-stream-lifecycle.ts
/**
* Finalizable draft lifecycle helpers for draft streaming.
*/
function createFinalizableDraftStreamControlsForState(params) {
	const loop = createDraftStreamLoop({
		throttleMs: params.throttleMs,
		isStopped: () => params.state.stopped,
		sendOrEditStreamMessage: params.sendOrEditStreamMessage
	});
	const update = (text) => {
		if (params.state.stopped || params.state.final) return;
		loop.update(text);
	};
	const stop = async () => {
		params.state.final = true;
		await loop.flush();
	};
	const stopForClear = async () => {
		params.state.stopped = true;
		loop.stop();
		await loop.waitForInFlight();
	};
	return {
		loop,
		update,
		stop,
		stopForClear
	};
}
async function clearFinalizableDraftMessage(params) {
	await params.stopForClear();
	const messageId = params.readMessageId();
	params.clearMessageId();
	if (!params.isValidMessageId(messageId)) return;
	try {
		await params.deleteMessage(messageId);
		params.onDeleteSuccess?.(messageId);
	} catch (err) {
		params.warn?.(`${params.warnPrefix}: ${err instanceof Error ? err.message : String(err)}`);
	}
}
function createFinalizableDraftLifecycle(params) {
	const controls = createFinalizableDraftStreamControlsForState({
		throttleMs: params.throttleMs,
		state: params.state,
		sendOrEditStreamMessage: params.sendOrEditStreamMessage
	});
	const clear = async () => {
		await clearFinalizableDraftMessage({
			stopForClear: controls.stopForClear,
			readMessageId: params.readMessageId,
			clearMessageId: params.clearMessageId,
			isValidMessageId: params.isValidMessageId,
			deleteMessage: params.deleteMessage,
			onDeleteSuccess: params.onDeleteSuccess,
			warn: params.warn,
			warnPrefix: params.warnPrefix
		});
	};
	return {
		...controls,
		clear
	};
}
//#endregion
export { clearFinalizableDraftMessage, createFinalizableDraftLifecycle, createFinalizableDraftStreamControlsForState };

//# sourceMappingURL=draft-stream-lifecycle.js.map