//#region src/elements/EFMedia/shared/AudioSpanUtils.ts
/**
* Fetch audio segment data using MediaEngine
* Pure function with explicit dependencies
*/
const fetchAudioSegmentData = async (segmentIds, mediaEngine, signal) => {
	const audioTrack = mediaEngine.tracks.audio;
	if (!audioTrack) throw new Error("Audio track not available");
	const segmentData = /* @__PURE__ */ new Map();
	const fetchPromises = segmentIds.map(async (segmentId) => {
		return [segmentId, await mediaEngine.transport.fetchMediaSegment(segmentId, audioTrack, signal)];
	});
	const fetchedSegments = await Promise.all(fetchPromises);
	signal.throwIfAborted();
	for (const [segmentId, arrayBuffer] of fetchedSegments) segmentData.set(segmentId, arrayBuffer);
	return segmentData;
};
/**
* Create audio span blob from init segment and media segments
* Pure function for blob creation
*/
const createAudioSpanBlob = (initSegment, mediaSegments) => {
	const chunks = [initSegment, ...mediaSegments];
	return new Blob(chunks, { type: "audio/mp4" });
};
/**
* Fetch audio spanning a time range
* Main function that orchestrates segment calculation, fetching, and blob creation
*/
const fetchAudioSpanningTime = async (host, fromMs, toMs, signal) => {
	if (fromMs >= toMs || fromMs < 0) throw new Error(`Invalid time range: fromMs=${fromMs}, toMs=${toMs}`);
	const mediaEngine = await host.getMediaEngine(signal);
	signal.throwIfAborted();
	const audioTrack = mediaEngine?.tracks.audio;
	if (!audioTrack) return;
	const initSegment = await mediaEngine.transport.fetchInitSegment(audioTrack, signal);
	signal.throwIfAborted();
	if (!initSegment) return;
	const segmentRanges = mediaEngine.index.segmentsInRange(fromMs, toMs, audioTrack);
	if (segmentRanges.length === 0) throw new Error(`No segments found for time range ${fromMs}-${toMs}ms`);
	const segmentIds = segmentRanges.map((r) => r.segmentId);
	const segmentData = await fetchAudioSegmentData(segmentIds, mediaEngine, signal);
	const blob = createAudioSpanBlob(initSegment, segmentIds.map((id) => {
		const segment = segmentData.get(id);
		if (!segment) throw new Error(`Missing segment data for segment ID ${id}`);
		return segment;
	}));
	return {
		startMs: Math.min(...segmentRanges.map((r) => r.startMs)),
		endMs: Math.max(...segmentRanges.map((r) => r.endMs)),
		blob
	};
};

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