//#region extensions/telegram/src/dedupe.ts
/**
* Update Deduplication
* 
* Prevents processing duplicate Telegram updates.
* Telegram may occasionally send the same update multiple times.
*/
const DEFAULT_TTL_MS = 300 * 1e3;
const DEFAULT_MAX_SIZE = 2e3;
var DedupeCache = class {
	cache = /* @__PURE__ */ new Map();
	ttlMs;
	maxSize;
	constructor(options = {}) {
		this.ttlMs = options.ttlMs ?? DEFAULT_TTL_MS;
		this.maxSize = options.maxSize ?? DEFAULT_MAX_SIZE;
	}
	/**
	* Check if key exists and is not expired
	*/
	has(key) {
		this.cleanup();
		const entry = this.cache.get(key);
		if (!entry) return false;
		if (Date.now() - entry.timestamp > this.ttlMs) {
			this.cache.delete(key);
			return false;
		}
		return true;
	}
	/**
	* Add key to cache
	*/
	add(key) {
		this.cleanup();
		if (this.cache.size >= this.maxSize) {
			const oldestKey = this.cache.keys().next().value;
			if (oldestKey !== void 0) this.cache.delete(oldestKey);
		}
		this.cache.set(key, { timestamp: Date.now() });
	}
	/**
	* Check and add atomically - returns true if was already seen
	*/
	checkAndAdd(key) {
		if (this.has(key)) return true;
		this.add(key);
		return false;
	}
	/**
	* Clean up expired entries
	*/
	cleanup() {
		const now = Date.now();
		for (const [key, entry] of this.cache) if (now - entry.timestamp > this.ttlMs) this.cache.delete(key);
	}
	/**
	* Clear all entries
	*/
	clear() {
		this.cache.clear();
	}
	/**
	* Get cache stats
	*/
	getStats() {
		this.cleanup();
		return {
			size: this.cache.size,
			maxSize: this.maxSize,
			ttlMs: this.ttlMs
		};
	}
};
/**
* Build a unique key for a Telegram update
*/
function buildTelegramUpdateKey(ctx) {
	const updateId = ctx.update?.update_id;
	if (typeof updateId === "number") return `update:${updateId}`;
	const callbackId = ctx.callbackQuery?.id;
	if (callbackId) return `callback:${callbackId}`;
	const msg = ctx.update?.message;
	const chatId = msg?.chat?.id;
	const messageId = msg?.message_id;
	if (typeof chatId === "number" && typeof messageId === "number") return `message:${chatId}:${messageId}`;
}
const telegramUpdateDedupe = new DedupeCache({
	ttlMs: DEFAULT_TTL_MS,
	maxSize: DEFAULT_MAX_SIZE
});
//#endregion
export { DedupeCache, buildTelegramUpdateKey, telegramUpdateDedupe };

//# sourceMappingURL=dedupe.js.map