//#region src/elements/EFMedia/SegmentTransport.ts
function resolveRenditionId(track) {
	if (track.role === "audio") return "audio";
	if (track.role === "scrub") return "scrub";
	if (typeof track.id === "string") return track.id;
	if (track.id === -1) return "scrub";
	if (track.id === 2) return "audio";
	return "high";
}
function createUrlTransport(opts) {
	const { fetcher, src, templates, audioTrackId, videoTrackId } = opts;
	function buildSegmentUrl(segmentId, track) {
		const renditionId = resolveRenditionId(track);
		const template = segmentId === "init" ? templates.initSegment : templates.mediaSegment;
		const trackId = typeof track.id === "number" ? track.id : track.role === "audio" ? audioTrackId : videoTrackId;
		return template.replace("{rendition}", renditionId).replace("{segmentId}", segmentId.toString()).replace("{src}", src).replace("{trackId}", trackId?.toString() ?? "");
	}
	return {
		async fetchInitSegment(track, signal) {
			const url = buildSegmentUrl("init", track);
			return fetcher.fetchArrayBuffer(url, signal);
		},
		async fetchMediaSegment(segmentId, track, signal) {
			const url = buildSegmentUrl(segmentId, track);
			return fetcher.fetchArrayBuffer(url, signal);
		},
		isCached(segmentId, track) {
			const url = buildSegmentUrl(segmentId, track);
			return fetcher.has(url);
		}
	};
}
function createByteRangeTransport(data, fileId, apiHost, fetcher) {
	function buildTrackUrl(trackId) {
		return `${apiHost}/api/v1/files/${fileId}/tracks/${trackId}`;
	}
	function getTrackId(track) {
		const trackId = typeof track.id === "number" ? track.id : Number.parseInt(track.id, 10);
		if (Number.isNaN(trackId)) throw new Error(`Invalid track ID: ${track.id}`);
		return trackId;
	}
	return {
		async fetchInitSegment(track, signal) {
			const trackId = getTrackId(track);
			const trackData = data[trackId];
			if (!trackData) throw new Error(`Track ${trackId} not found`);
			const { offset, size } = trackData.initSegment;
			const url = buildTrackUrl(trackId);
			return (await fetcher.fetchArrayBuffer(url, signal)).slice(offset, offset + size);
		},
		async fetchMediaSegment(segmentId, track, signal) {
			const trackId = getTrackId(track);
			const trackData = data[trackId];
			if (!trackData) throw new Error(`Track ${trackId} not found`);
			const segment = trackData.segments[segmentId];
			if (!segment) throw new Error(`Segment ${segmentId} not found for track ${trackId}`);
			const url = buildTrackUrl(trackId);
			return (await fetcher.fetchArrayBuffer(url, signal)).slice(segment.offset, segment.offset + segment.size);
		},
		isCached(_segmentId, track) {
			const url = buildTrackUrl(getTrackId(track));
			return fetcher.has(url);
		}
	};
}

//#endregion
export { createByteRangeTransport, createUrlTransport };
//# sourceMappingURL=SegmentTransport.js.map