import { getCredentialVault } from "./credential-vault.mjs";
import { guardedFetch } from "./endpoint-allowlist.mjs";
//#region extensions/crypto/src/services/typing-indicator.ts
/**
* Typing Indicator — sends Telegram "typing..." action for the full
* duration the agent is thinking/processing.
*
* Telegram's `sendChatAction` typing indicator expires after ~5 seconds,
* so we re-send it every 4.5s on a loop until explicitly stopped.
*
* Follows the same raw Bot API call pattern as telegram-draft-stream.ts.
* No new dependencies — uses guardedFetch.
*
* Lifecycle:
*   message_received → start(chatId)
*   message_sending  → stop(chatId)
*
* Safety:
*   - Max duration cap (5 minutes) prevents orphaned indicators
*   - Errors are swallowed (typing indicator is non-critical UX)
*   - Only activates for Telegram channel
*/
/** Re-send typing action every 4.5s (Telegram expires at ~5s). */
const REPEAT_MS = 4500;
/** Hard cap: stop indicator after 5 minutes even if not explicitly stopped. */
const MAX_DURATION_MS = 5 * 6e4;
const API_BASE = "https://api.telegram.org";
var TypingIndicatorService = class {
	active = /* @__PURE__ */ new Map();
	botToken = null;
	tokenResolved = false;
	/** Resolve the bot token lazily (only when first needed). */
	getToken() {
		if (this.tokenResolved) return this.botToken;
		this.tokenResolved = true;
		try {
			this.botToken = getCredentialVault().getSecret("bot.telegram.botToken", "typing-indicator") ?? null;
		} catch {
			this.botToken = process.env.TELEGRAM_BOT_TOKEN ?? null;
		}
		return this.botToken;
	}
	/**
	* Start the typing indicator for a chat.
	* Sends `sendChatAction` immediately, then repeats every 4.5s.
	*/
	start(chatId) {
		const token = this.getToken();
		if (!token) return;
		const key = String(chatId);
		if (this.active.has(key)) return;
		this.sendTyping(token, key);
		const timer = setInterval(() => this.sendTyping(token, key), REPEAT_MS);
		if (typeof timer.unref === "function") timer.unref();
		const safetyTimeout = setTimeout(() => this.stop(key), MAX_DURATION_MS);
		if (typeof safetyTimeout.unref === "function") safetyTimeout.unref();
		this.active.set(key, {
			timer,
			startedAt: Date.now(),
			safetyTimeout
		});
	}
	/** Stop the typing indicator for a chat. */
	stop(chatId) {
		const key = String(chatId);
		const entry = this.active.get(key);
		if (!entry) return;
		clearInterval(entry.timer);
		clearTimeout(entry.safetyTimeout);
		this.active.delete(key);
	}
	/** Stop all active indicators (for shutdown). */
	stopAll() {
		for (const [key] of this.active) this.stop(key);
	}
	/** Number of chats with active typing indicator (for testing). */
	get size() {
		return this.active.size;
	}
	/** Whether a chat has an active indicator (for testing). */
	isActive(chatId) {
		return this.active.has(String(chatId));
	}
	/** Send a single sendChatAction typing call. Fire-and-forget. */
	sendTyping(token, chatId) {
		guardedFetch(`${API_BASE}/bot${token}/sendChatAction`, {
			method: "POST",
			headers: { "Content-Type": "application/json" },
			body: JSON.stringify({
				chat_id: chatId,
				action: "typing"
			}),
			signal: AbortSignal.timeout(5e3)
		}).catch(() => {});
	}
};
let instance = null;
function getTypingIndicator() {
	if (!instance) instance = new TypingIndicatorService();
	return instance;
}
function resetTypingIndicator() {
	instance?.stopAll();
	instance = null;
}
//#endregion
export { getTypingIndicator, resetTypingIndicator };

//# sourceMappingURL=typing-indicator.mjs.map