recommendations.ts

// prettier-ignore
import { 
  TRENDING, POPULAR, LATEST, MARVELOUS, LENGTHY, MULTISTARRER, CAROUSEL,
  POPULAR_TITLE, TRENDING_TITLE, LATEST_TITLE, MARVELOUS_TITLE, LENGTHY_TITLE, MULTISTARRER_TITLE
} from '../../utility/constants/values';
import { firstItemsFirst, getComedianVideosMap } from '../helper/orderUniqueFirst';

import { getVideosWithSelectedAttributes, filterVideoWithHdThumbnail } from './lib';

function getTitleForCategory(category: string) {
  switch (category) {
    case TRENDING:
      return TRENDING_TITLE;
    case POPULAR:
      return POPULAR_TITLE;
    case LATEST:
      return LATEST_TITLE;
    case MARVELOUS:
      return MARVELOUS_TITLE;
    case LENGTHY:
      return LENGTHY_TITLE;
    case MULTISTARRER:
      return MULTISTARRER_TITLE;
  }
}

function keepOnlyFew(videos) {
  return videos.slice(0, 100);
}

function getComedianMapWithOrderedVideos(comedianMap) {
  // TODO
  return comedianMap;
}

function orderUniqueFirst(videos) {
  const comedianMap = getComedianVideosMap(videos);
  const sortedMap = getComedianMapWithOrderedVideos(comedianMap);
  return firstItemsFirst(sortedMap);
}

function getVideos(category: string, videos) {
  const unorderedVideos = getVideosWithSelectedAttributes(videos, category);
  if (category === CAROUSEL) {
    return unorderedVideos;
  }
  // ORDERING OF VIDEOS
  const orderedVideos = orderUniqueFirst(unorderedVideos);
  return keepOnlyFew(orderedVideos);
}

export default function recommendationsMapper(recommendations) {
  const transformedRecommendations = {};
  const { trending, latest } = recommendations;

  // carousel list creation
  // prettier-ignore
  const [filteredTrending, remainingTrending] = filterVideoWithHdThumbnail(trending,2);
  // prettier-ignore
  const [filteredLatest, remainingLatest] = filterVideoWithHdThumbnail(latest,3);

  const carouselVideos = [...filteredTrending, ...filteredLatest];
  transformedRecommendations[CAROUSEL] = {
    list: getVideos(CAROUSEL, carouselVideos)
  };

  // update recommendation list with remaining
  recommendations.trending = remainingTrending;
  recommendations.latest = remainingLatest;

  // process recommendation by category
  for (const [key, value] of Object.entries(recommendations)) {
    transformedRecommendations[key] = {
      title: getTitleForCategory(key),
      list: getVideos(key, value)
    };
  }

  return transformedRecommendations;
}