//#region src/preview/statsTrackingStrategy.ts
/**
* DOM mode stats tracking strategy.
* Tracks FPS, resolution, CPU pressure, and frame seek time.
*/
var DomStatsStrategy = class {
	constructor(options) {
		this.animationFrame = null;
		this.lastFrameTime = 0;
		this.frameIntervals = [];
		this.lastStatsUpdateTime = 0;
		this.currentStats = null;
		this.seekStartTime = 0;
		this.seekTimes = [];
		this.frameTaskCleanup = null;
		this.ROLLING_WINDOW_SIZE = 30;
		this.TARGET_FRAME_TIME_MS = 33.33;
		this.timegroup = options.timegroup;
		this.adaptiveTracker = options.adaptiveTracker;
	}
	start() {
		if (this.animationFrame !== null) return;
		const frameTaskCallback = async () => {
			if (this.seekStartTime > 0) {
				const seekTime = performance.now() - this.seekStartTime;
				this.seekTimes.push(seekTime);
				if (this.seekTimes.length > this.ROLLING_WINDOW_SIZE) this.seekTimes.shift();
				this.seekStartTime = 0;
			}
		};
		this.timegroup.addFrameTask(frameTaskCallback);
		this.frameTaskCleanup = () => {};
		let lastCurrentTimeMs = this.timegroup.currentTimeMs;
		const checkSeek = () => {
			const currentTimeMs = this.timegroup.currentTimeMs;
			if (currentTimeMs !== lastCurrentTimeMs) {
				this.seekStartTime = performance.now();
				lastCurrentTimeMs = currentTimeMs;
			}
		};
		const loop = (timestamp) => {
			if (this.animationFrame === null) return;
			checkSeek();
			if (this.lastFrameTime > 0) {
				const interval = timestamp - this.lastFrameTime;
				this.frameIntervals.push(interval);
				if (this.frameIntervals.length > this.ROLLING_WINDOW_SIZE) this.frameIntervals.shift();
			}
			this.lastFrameTime = timestamp;
			if (timestamp - this.lastStatsUpdateTime > 100) {
				this.lastStatsUpdateTime = timestamp;
				this.updateStats();
			}
			this.animationFrame = requestAnimationFrame(loop);
		};
		this.animationFrame = requestAnimationFrame(loop);
	}
	stop() {
		if (this.animationFrame !== null) {
			cancelAnimationFrame(this.animationFrame);
			this.animationFrame = null;
		}
		if (this.frameTaskCleanup) {
			this.frameTaskCleanup();
			this.frameTaskCleanup = null;
		}
		this.lastFrameTime = 0;
		this.frameIntervals = [];
		this.seekStartTime = 0;
		this.seekTimes = [];
		this.currentStats = null;
	}
	getStats() {
		return this.currentStats;
	}
	supportsStat(stat) {
		return stat === "fps" || stat === "resolution" || stat === "cpuPressure" || stat === "renderTime" || stat === "headroom";
	}
	updateStats() {
		const avgFrameInterval = this.frameIntervals.length > 0 ? this.frameIntervals.reduce((a, b) => a + b, 0) / this.frameIntervals.length : 16.67;
		const fps = avgFrameInterval > 0 ? 1e3 / avgFrameInterval : 0;
		const avgSeekTime = this.seekTimes.length > 0 ? this.seekTimes.reduce((a, b) => a + b, 0) / this.seekTimes.length : 0;
		const headroom = avgSeekTime > 0 ? this.TARGET_FRAME_TIME_MS - avgSeekTime : 0;
		const trackerStats = this.adaptiveTracker.getStats();
		const rect = this.timegroup.getBoundingClientRect();
		const renderWidth = Math.round(rect.width);
		const renderHeight = Math.round(rect.height);
		this.currentStats = {
			fps,
			avgRenderTime: avgSeekTime > 0 ? avgSeekTime : null,
			headroom: avgSeekTime > 0 ? headroom : null,
			pressureState: trackerStats.pressureState,
			pressureHistory: trackerStats.pressureHistory,
			renderWidth,
			renderHeight,
			resolutionScale: null
		};
	}
};

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