//#region src/transcoding/cache/URLTokenDeduplicator.ts
var URLTokenDeduplicator = class {
	constructor() {
		this.tokenCache = /* @__PURE__ */ new Map();
	}
	/**
	* Get or create a URL token with global deduplication
	* Multiple requests for the same cache key will share the same token promise
	*/
	async getToken(cacheKey, tokenFactory, parseExpiration) {
		const now = Date.now();
		const cached = this.tokenCache.get(cacheKey);
		if (cached && now < cached.expiration) return cached.tokenPromise;
		const tokenPromise = tokenFactory().then(async (token) => {
			const expiration = parseExpiration(token);
			this.tokenCache.set(cacheKey, {
				tokenPromise,
				expiration
			});
			return token;
		}).catch((error) => {
			this.tokenCache.delete(cacheKey);
			throw error;
		});
		this.tokenCache.set(cacheKey, {
			tokenPromise,
			expiration: now + 6e4
		});
		return tokenPromise;
	}
	/**
	* Clear all cached tokens (used in testing)
	*/
	clear() {
		this.tokenCache.clear();
	}
	/**
	* Get number of cached tokens
	*/
	getCachedCount() {
		return this.tokenCache.size;
	}
	/**
	* Check if a token is cached and valid
	*/
	hasValidToken(cacheKey) {
		const cached = this.tokenCache.get(cacheKey);
		if (!cached) return false;
		return Date.now() < cached.expiration;
	}
	/**
	* Get all cached token keys
	*/
	getCachedKeys() {
		return Array.from(this.tokenCache.keys());
	}
	/**
	* Remove expired tokens from cache
	*/
	cleanup() {
		const now = Date.now();
		for (const [key, entry] of this.tokenCache.entries()) if (now >= entry.expiration) this.tokenCache.delete(key);
	}
};
const globalURLTokenDeduplicator = new URLTokenDeduplicator();

//#endregion
export { globalURLTokenDeduplicator };
//# sourceMappingURL=URLTokenDeduplicator.js.map