import { useQuery } from "@tanstack/react-query";
import TikTokOEmbed from "services/api/tiktok-embed.api";

const TIKTOK_METADATA_CACHE_TIME = 1000 * 60 * 60 * 6; // 6 hours, thumbnail url expires after 6 hours

export const useTikTokMetadata = (videoUrl: string) => {
  return useQuery({
    queryKey: ["tiktok-metadata", videoUrl],
    queryFn: () => TikTokOEmbed.metadata(videoUrl),
    refetchOnWindowFocus: false,
    staleTime: TIKTOK_METADATA_CACHE_TIME,
    cacheTime: TIKTOK_METADATA_CACHE_TIME,
    retry: 4,
    retryDelay(failureCount) {
      const baseDelay = 1000; // 1 second
      const maxDelay = 10000; // 10 seconds
      const exponentialDelay = Math.min(
        baseDelay * Math.pow(2, failureCount - 1),
        maxDelay
      );
      return exponentialDelay;
    },
  });
};
