import { Task } from "@lit/task";

import { EFMedia } from "../../EFMedia";
import type { EFVideo } from "../../EFVideo";
import { BufferedSeekingInput } from "../BufferedSeekingInput";
import type { InputTask } from "../shared/MediaTaskUtils";

export const makeScrubVideoInputTask = (host: EFVideo): InputTask => {
  return new Task<
    readonly [ArrayBuffer | undefined, ArrayBuffer | undefined],
    BufferedSeekingInput
  >(host, {
    args: () =>
      [
        host.scrubVideoInitSegmentFetchTask.value,
        host.scrubVideoSegmentFetchTask.value,
      ] as const,
    onError: (error) => {
      console.error("scrubVideoInputTask error", error);
    },
    onComplete: (_value) => {},
    task: async () => {
      const initSegment =
        await host.scrubVideoInitSegmentFetchTask.taskComplete;
      const segment = await host.scrubVideoSegmentFetchTask.taskComplete;
      if (!initSegment || !segment) {
        throw new Error("Scrub init segment or segment is not available");
      }

      // Get startTimeOffsetMs from the scrub rendition if available
      const mediaEngine = await host.mediaEngineTask.taskComplete;
      const scrubRendition = mediaEngine.getScrubVideoRendition();
      const startTimeOffsetMs = scrubRendition?.startTimeOffsetMs;

      const input = new BufferedSeekingInput(
        await new Blob([initSegment, segment]).arrayBuffer(),
        {
          videoBufferSize: EFMedia.VIDEO_SAMPLE_BUFFER_SIZE,
          audioBufferSize: EFMedia.AUDIO_SAMPLE_BUFFER_SIZE,
          startTimeOffsetMs,
        },
      );
      return input;
    },
  });
};
