import { getLatestMediaEngine } from "../tasks/makeMediaEngineTask.js";
import { Task } from "@lit/task";
const makeAudioSegmentFetchTask = (host) => {
	return new Task(host, {
		args: () => [host.mediaEngineTask.value, host.audioSegmentIdTask.value],
		onError: (error) => {
			console.error("audioSegmentFetchTask error", error);
		},
		onComplete: (_value) => {},
		task: async (_, { signal }) => {
			const mediaEngine = await getLatestMediaEngine(host, signal);
			const segmentId = await host.audioSegmentIdTask.taskComplete;
			if (segmentId === void 0) {
				const rendition = mediaEngine.audioRendition;
				const debugInfo = {
					hasRendition: !!rendition,
					segmentDurationMs: rendition?.segmentDurationMs,
					segmentDurationsMs: rendition?.segmentDurationsMs?.length || 0,
					desiredSeekTimeMs: host.desiredSeekTimeMs,
					intrinsicDurationMs: host.intrinsicDurationMs
				};
				throw new Error(`Segment ID is not available for audio. Debug info: ${JSON.stringify(debugInfo)}`);
			}
			return mediaEngine.fetchMediaSegment(segmentId, mediaEngine.getAudioRendition(), signal);
		}
	});
};
export { makeAudioSegmentFetchTask };
