import {
  TYPE_CSV,
  TYPE_MANUAL,
  TYPE_MRSS,
  TYPE_MS_STREAM,
  TYPE_RSS,
  TYPE_SITEMAP,
  TYPE_STORY_API,
  TYPE_TIKTOK,
  TYPE_VIDEO_API,
  TYPE_VIMEO,
  TYPE_YOUTUBE,
} from '@/modules/feeds/constants';

import type { FeedType } from '@/modules/feeds/types';

import * as selectors from '../../redux/selectors';

import getAllFields from './getAllFields';
import getCsvFields, { CsvFeedPayloadType } from './getCsvFields';
import getManualFields, { ManualFeedPayloadType } from './getManualFields';
import getMrssFields, { MrssFeedPayloadType } from './getMrssFields';
import getMsStreamFields, { MsStreamFeedPayloadType } from './getMsStreamFields';
import getRssFields, { RssFeedPayloadType } from './getRssFields';
import getSitemapFields, { SitemapFeedPayloadType } from './getSitemapFields';
import getStoryApiFields, { StoryApiFeedPayloadType } from './getStoryApiFields';
import getTikTokFields, { TikTokFeedPayloadType } from './getTikTokFields';
import getVideoApiFields, { VideoApiFeedPayloadType } from './getVideoApiFields';
import getVimeoApiFields, { VimeoFeedPayloadType } from './getVimeoFields';
import getYoutubeFields, { YoutubeFeedPayloadType } from './getYoutubeFields';

export function getRequestPayload(
  type: FeedType,
  state: RootState,
):
  | MrssFeedPayloadType
  | Partial<TikTokFeedPayloadType>
  | RssFeedPayloadType
  | SitemapFeedPayloadType
  | StoryApiFeedPayloadType
  | ManualFeedPayloadType
  | YoutubeFeedPayloadType
  | CsvFeedPayloadType
  | VideoApiFeedPayloadType
  | VimeoFeedPayloadType
  | MsStreamFeedPayloadType
  | null {
  const fields = getAllFields(state);
  const isSelfServeUser = selectors.isSelfServeUserSelector(state);
  const metadataPlatformModels = selectors.metadataPlatformModelsSelector(state);

  if (type === TYPE_MRSS) return getMrssFields(fields, isSelfServeUser, metadataPlatformModels);
  if (type === TYPE_TIKTOK) return getTikTokFields(fields, isSelfServeUser, metadataPlatformModels);
  if (type === TYPE_RSS) return getRssFields(fields);
  if (type === TYPE_SITEMAP) return getSitemapFields(fields);
  if (type === TYPE_STORY_API) return getStoryApiFields(fields);
  if (type === TYPE_MANUAL) return getManualFields(fields, metadataPlatformModels);
  if (type === TYPE_YOUTUBE) return getYoutubeFields(fields, metadataPlatformModels);
  if (type === TYPE_CSV) return getCsvFields(fields, metadataPlatformModels);
  if (type === TYPE_VIDEO_API) return getVideoApiFields(fields, metadataPlatformModels);
  if (type === TYPE_VIMEO) return getVimeoApiFields(fields, metadataPlatformModels);
  if (type === TYPE_MS_STREAM) return getMsStreamFields(fields, isSelfServeUser, metadataPlatformModels);

  return null;
}
