import { roundToMilliseconds } from "./EFMedia/shared/PrecisionUtils.js";

//#region src/elements/SampleBuffer.ts
var SampleBuffer = class {
	constructor(bufferSize = 10) {
		this.buffer = [];
		this.bufferSize = bufferSize;
	}
	push(sample) {
		const currentBuffer = [...this.buffer];
		currentBuffer.push(sample);
		if (currentBuffer.length > this.bufferSize) {
			const shifted = currentBuffer.shift();
			if (shifted) try {
				shifted.close();
			} catch (_error) {}
		}
		this.buffer = currentBuffer;
	}
	clear() {
		const toClose = this.buffer;
		this.buffer = [];
		for (const sample of toClose) try {
			sample.close();
		} catch (_error) {}
	}
	peek() {
		return this.buffer[0];
	}
	find(desiredSeekTimeMs) {
		const currentBuffer = [...this.buffer];
		if (currentBuffer.length === 0) return void 0;
		const targetTimeMs = roundToMilliseconds(desiredSeekTimeMs);
		for (const sample of currentBuffer) {
			const sampleStartMs = roundToMilliseconds((sample.timestamp || 0) * 1e3);
			const sampleEndMs = roundToMilliseconds(sampleStartMs + roundToMilliseconds((sample.duration || 0) * 1e3));
			if (targetTimeMs >= sampleStartMs && targetTimeMs < sampleEndMs) return sample;
		}
	}
	get length() {
		return this.buffer.length;
	}
	get firstTimestamp() {
		return this.buffer[0]?.timestamp || 0;
	}
	getContents() {
		return [...this.buffer];
	}
};

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