import { IgnorableError } from "../../EFMedia.js";
import { Task } from "@lit/task";
const makeVideoSeekTask = (host) => {
	return new Task(host, {
		args: () => [host.desiredSeekTimeMs, host.videoInputTask.value],
		onError: (error) => {
			if (error instanceof IgnorableError) console.info("videoSeekTask aborted");
			console.error("videoSeekTask error", error);
		},
		onComplete: (_value) => {},
		task: async ([targetSeekTimeMs], { signal }) => {
			await host.mediaEngineTask.taskComplete;
			signal.throwIfAborted();
			await host.videoSegmentIdTask.taskComplete;
			signal.throwIfAborted();
			await host.videoSegmentFetchTask.taskComplete;
			signal.throwIfAborted();
			await host.videoInitSegmentFetchTask.taskComplete;
			signal.throwIfAborted();
			const videoInput = await host.videoInputTask.taskComplete;
			signal.throwIfAborted();
			if (!videoInput) throw new Error("Video input is not available");
			const videoTrack = await videoInput.getFirstVideoTrack();
			if (!videoTrack) throw new Error("Video track is not available");
			signal.throwIfAborted();
			const sample = await videoInput.seek(videoTrack.id, targetSeekTimeMs);
			signal.throwIfAborted();
			if (sample === void 0 && signal.aborted) return void 0;
			if (sample === void 0) throw new Error("Video seek failed to find sample");
			return sample;
		}
	});
};
export { makeVideoSeekTask };
