import { MWSeasonWithEpisodeMeta } from "@/backend/metadata/types";
import { useVideoPlayerDescriptor } from "@/video/state/hooks";
import { useControls } from "@/video/state/logic/controls";
import { VideoPlayerMeta } from "@/video/state/types";
import { useEffect } from "react";

interface MetaControllerProps {
  data?: VideoPlayerMeta;
  seasonData?: MWSeasonWithEpisodeMeta;
}

function formatMetadata(
  props: MetaControllerProps
): VideoPlayerMeta | undefined {
  const seasonsWithEpisodes = props.data?.seasons?.map((v) => {
    if (v.id === props.seasonData?.id)
      return {
        ...v,
        episodes: props.seasonData.episodes,
      };
    return v;
  });

  if (!props.data) return undefined;

  return {
    meta: props.data.meta,
    episode: props.data.episode,
    seasons: seasonsWithEpisodes,
  };
}

export function MetaController(props: MetaControllerProps) {
  const descriptor = useVideoPlayerDescriptor();
  const controls = useControls(descriptor);

  useEffect(() => {
    controls.setMeta(formatMetadata(props));
  }, [props, controls]);

  return null;
}
