import { getLatestMediaEngine } from "../tasks/makeMediaEngineTask.js";
import { Task } from "@lit/task";
const makeVideoSegmentFetchTask = (host) => {
	return new Task(host, {
		args: () => [host.mediaEngineTask.value, host.videoSegmentIdTask.value],
		onError: (error) => {
			console.error("videoSegmentFetchTask error", error);
		},
		onComplete: (_value) => {},
		task: async (_, { signal }) => {
			const mediaEngine = await getLatestMediaEngine(host, signal);
			const segmentId = await host.videoSegmentIdTask.taskComplete;
			if (segmentId === void 0) {
				const rendition = mediaEngine.videoRendition;
				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 video. Debug info: ${JSON.stringify(debugInfo)}`);
			}
			return mediaEngine.fetchMediaSegment(segmentId, mediaEngine.getVideoRendition(), signal);
		}
	});
};
export { makeVideoSegmentFetchTask };
