import Router from 'next/router';

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 { featureFlags } from '@/modules/@common/helpers/featureFlags';

export const supportedNewFeedFormsByType = [
  TYPE_MRSS,
  TYPE_TIKTOK,
  TYPE_RSS,
  TYPE_SITEMAP,
  TYPE_STORY_API,
  TYPE_MANUAL,
  TYPE_YOUTUBE,
  TYPE_CSV,
  TYPE_VIDEO_API,
  TYPE_VIMEO,
  TYPE_MS_STREAM,
] as const;
export const supportedNewFeedFormRoutePathnames: Record<(typeof supportedNewFeedFormsByType)[number], string> = {
  [TYPE_MRSS]: '/feeds/[id]/mrss',
  [TYPE_TIKTOK]: '/feeds/[id]/tiktok',
  [TYPE_RSS]: '/feeds/[id]/rss',
  [TYPE_SITEMAP]: '/feeds/[id]/sitemap',
  [TYPE_STORY_API]: '/feeds/[id]/story-api',
  [TYPE_MANUAL]: '/feeds/[id]/manual',
  [TYPE_YOUTUBE]: '/feeds/[id]/youtube',
  [TYPE_CSV]: '/feeds/[id]/csv',
  [TYPE_VIDEO_API]: '/feeds/[id]/video-api',
  [TYPE_VIMEO]: '/feeds/[id]/vimeo',
  [TYPE_MS_STREAM]: '/feeds/[id]/ms-stream',
};

export const redirectToSupportedNewFeedForm = (
  type: (typeof supportedNewFeedFormsByType)[number],
  id: number | 'new',
): boolean => {
  const redirectUrlTemplate = supportedNewFeedFormRoutePathnames[type];

  if (!redirectUrlTemplate) {
    return false;
  }

  if (featureFlags.showOldFormsInFeedModule) {
    return false;
  }

  const url = redirectUrlTemplate.replace('[id]', id.toString());
  Router.push(url);

  return true;
};
