lib.ts

import { CAROUSEL } from '../../utility/constants/values';
import shuffle from '../../utility/lib/shuffle';

/**
 * filter videos that have maxres thumbnail
 * @param videos Videos to filter from
 * @param count Number of videos to filter
 */
export function filterVideoWithHdThumbnail(videos, count) {
  const filtered = [];
  const remaining = [];
  // Add randomness so that different results show up each time
  const shuffledList = shuffle(videos);

  for (let i = 0; i < shuffledList.length; i++) {
    const video = shuffledList[i];
    const { thumbnails } = video;

    if (thumbnails['maxres'] && filtered.length < count) {
      filtered.push(video);
      continue;
    }
    remaining.push(video);
  }

  return [filtered, remaining];
}

export function getVideoWithSelectedAttributes(video, category) {
  const { videoId, title, comedianId } = video;
  return {
    videoId,
    comedianId,
    title,
    url: getThumbnailUrl(category, video)
  };
}

export function getVideosWithSelectedAttributes(videos, category) {
  return videos.map((video) => {
    return getVideoWithSelectedAttributes(video, category);
  });
}

export function getThumbnailUrl(category: string, { thumbnails }) {
  const { high, maxres } = thumbnails;
  return category === CAROUSEL ? maxres.url : high.url;
}