/**
* Fetch audio segment data using MediaEngine
* Pure function with explicit dependencies
*/
const fetchAudioSegmentData = async (segmentIds, mediaEngine, signal) => {
	const audioRendition = mediaEngine.audioRendition;
	if (!audioRendition) throw new Error("Audio rendition not available");
	const segmentData = /* @__PURE__ */ new Map();
	const fetchPromises = segmentIds.map(async (segmentId) => {
		const arrayBuffer = await mediaEngine.fetchMediaSegment(segmentId, audioRendition, signal);
		return [segmentId, arrayBuffer];
	});
	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.mediaEngineTask.taskComplete;
	const initSegment = await host.audioInitSegmentFetchTask.taskComplete;
	if (!mediaEngine?.audioRendition) throw new Error("Audio rendition not available");
	if (!initSegment) throw new Error("Audio init segment is not available");
	const segmentRanges = mediaEngine.calculateAudioSegmentRange(fromMs, toMs, mediaEngine.audioRendition, host.intrinsicDurationMs || 1e4);
	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 orderedSegments = segmentIds.map((id) => {
		const segment = segmentData.get(id);
		if (!segment) throw new Error(`Missing segment data for segment ID ${id}`);
		return segment;
	});
	const blob = createAudioSpanBlob(initSegment, orderedSegments);
	const actualStartMs = Math.min(...segmentRanges.map((r) => r.startMs));
	const actualEndMs = Math.max(...segmentRanges.map((r) => r.endMs));
	return {
		startMs: actualStartMs,
		endMs: actualEndMs,
		blob
	};
};
export { fetchAudioSpanningTime };
