import { Task } from "@lit/task";
import type { MediaEngine } from "../../../transcoding/types";
import type { EFVideo } from "../../EFVideo";
import { getLatestMediaEngine } from "../tasks/makeMediaEngineTask";

export const makeVideoInitSegmentFetchTask = (
  host: EFVideo,
): Task<readonly [MediaEngine | undefined], ArrayBuffer> => {
  return new Task(host, {
    args: () => [host.mediaEngineTask.value] as const,
    onError: (error) => {
      console.error("videoInitSegmentFetchTask error", error);
    },
    onComplete: (_value) => {},
    task: async ([_mediaEngine], { signal }) => {
      const mediaEngine = await getLatestMediaEngine(host, signal);
      return mediaEngine.fetchInitSegment(
        mediaEngine.getVideoRendition(),
        signal,
      );
    },
  });
};
