import { BaseMediaEngine } from "./BaseMediaEngine.js";
var JitMediaEngine = class JitMediaEngine extends BaseMediaEngine {
	static async fetch(host, urlGenerator, url) {
		const engine = new JitMediaEngine(host, urlGenerator);
		const data = await engine.fetchManifest(url);
		engine.data = data;
		return engine;
	}
	constructor(host, urlGenerator) {
		super(host);
		this.data = {};
		this.urlGenerator = urlGenerator;
	}
	get durationMs() {
		return this.data.durationMs;
	}
	get src() {
		return this.data.sourceUrl;
	}
	get audioRendition() {
		if (!this.data.audioRenditions || this.data.audioRenditions.length === 0) return void 0;
		const rendition = this.data.audioRenditions[0];
		if (!rendition) return void 0;
		return {
			id: rendition.id,
			trackId: void 0,
			src: this.data.sourceUrl,
			segmentDurationMs: rendition.segmentDurationMs,
			segmentDurationsMs: rendition.segmentDurationsMs
		};
	}
	get videoRendition() {
		if (!this.data.videoRenditions || this.data.videoRenditions.length === 0) return void 0;
		const rendition = this.data.videoRenditions[0];
		if (!rendition) return void 0;
		return {
			id: rendition.id,
			trackId: void 0,
			src: this.data.sourceUrl,
			segmentDurationMs: rendition.segmentDurationMs,
			segmentDurationsMs: rendition.segmentDurationsMs
		};
	}
	get templates() {
		return this.data.endpoints;
	}
	async fetchInitSegment(rendition, signal) {
		if (!rendition.id) throw new Error("Rendition ID is required for JIT metadata");
		const url = this.urlGenerator.generateSegmentUrl("init", rendition.id, this);
		return this.fetchMedia(url, signal);
	}
	async fetchMediaSegment(segmentId, rendition) {
		if (!rendition.id) throw new Error("Rendition ID is required for JIT metadata");
		const url = this.urlGenerator.generateSegmentUrl(segmentId, rendition.id, this);
		return this.fetchMedia(url);
	}
	computeSegmentId(desiredSeekTimeMs, rendition) {
		if (desiredSeekTimeMs > this.durationMs) return void 0;
		if (rendition.segmentDurationsMs && rendition.segmentDurationsMs.length > 0) {
			let cumulativeTime = 0;
			for (let i = 0; i < rendition.segmentDurationsMs.length; i++) {
				const segmentDuration = rendition.segmentDurationsMs[i];
				if (segmentDuration === void 0) throw new Error("Segment duration is required for JIT metadata");
				const segmentStartMs$1 = cumulativeTime;
				const segmentEndMs = cumulativeTime + segmentDuration;
				const isLastSegment = i === rendition.segmentDurationsMs.length - 1;
				const includesEndTime = isLastSegment && desiredSeekTimeMs === this.durationMs;
				if (desiredSeekTimeMs >= segmentStartMs$1 && (desiredSeekTimeMs < segmentEndMs || includesEndTime)) return i + 1;
				cumulativeTime += segmentDuration;
				if (cumulativeTime >= this.durationMs) break;
			}
			return void 0;
		}
		if (!rendition.segmentDurationMs) throw new Error("Segment duration is required for JIT metadata");
		const segmentIndex = Math.floor(desiredSeekTimeMs / rendition.segmentDurationMs);
		const segmentStartMs = segmentIndex * rendition.segmentDurationMs;
		if (segmentStartMs >= this.durationMs) return void 0;
		return segmentIndex + 1;
	}
};
export { JitMediaEngine };
