//#region src/elements/EFMedia/videoTasks/MainVideoInputCache.ts
/**
* Cache for main video BufferedSeekingInput instances
* Main video segments are typically 2s long, so we can reuse the same input
* for multiple frames within that segment (e.g., 60 frames at 30fps)
*/
var MainVideoInputCache = class {
	#cache = /* @__PURE__ */ new Map();
	#pendingPromises = /* @__PURE__ */ new Map();
	#maxCacheSize = 10;
	/**
	* Create a cache key that uniquely identifies a segment
	*/
	#getCacheKey(src, segmentId, renditionId) {
		return `${src}:${renditionId || "default"}:${segmentId}`;
	}
	/**
	* Get or create BufferedSeekingInput for a main video segment.
	*
	* Uses promise deduplication to prevent race conditions when multiple
	* concurrent requests arrive for the same segment. Without this,
	* the first segment often fails when DevTools is closed because:
	* 1. Video display and thumbnail extraction both request segment 0
	* 2. Both find cache empty and start createInputFn()
	* 3. Both create separate instances, causing conflicts
	*/
	async getOrCreateInput(src, segmentId, renditionId, createInputFn) {
		const cacheKey = this.#getCacheKey(src, segmentId, renditionId);
		const cached = this.#cache.get(cacheKey);
		if (cached) return cached;
		const pending = this.#pendingPromises.get(cacheKey);
		if (pending) return pending;
		const promise = createInputFn().then((input) => {
			this.#pendingPromises.delete(cacheKey);
			if (input) {
				this.#cache.set(cacheKey, input);
				if (this.#cache.size > this.#maxCacheSize) {
					const oldestKey = this.#cache.keys().next().value;
					if (oldestKey !== void 0) this.#cache.delete(oldestKey);
				}
			}
			return input;
		}).catch((error) => {
			this.#pendingPromises.delete(cacheKey);
			throw error;
		});
		this.#pendingPromises.set(cacheKey, promise);
		return promise;
	}
	/**
	* Clear the entire cache (called when video changes)
	*/
	clear() {
		this.#cache.clear();
		this.#pendingPromises.clear();
	}
	/**
	* Get cache statistics
	*/
	getStats() {
		return {
			size: this.#cache.size,
			pendingSize: this.#pendingPromises.size,
			cacheKeys: Array.from(this.#cache.keys())
		};
	}
};

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