import { ACCESS_LEVEL_HUB, ACCESS_LEVEL_PRIVATE } from '@/modules/feeds/Editor/constants';

import { AllFieldsType } from '@/modules/feeds/Editor/helpers/requestPayload/getAllFields';

export type SitemapFeedPayloadType = {
  id?: number;
  type: string;
  accountId?: number;
  name: string;
  description: string;
  lang: string;
  accessLevel: string;
  accessPublishers?: { publisherId: number }[];
  accessVideoOwnerId?: number | null;
  accessAllPublishers: boolean;
  url: string;
  user: string;
  password: string;
  auth_method: string;
  use_for_download: boolean;
  schedule_status: boolean;
  schedule_type: string;
  schedule_value: string;
  schedule_frequency: number;

  maxStories: number;
  videoDuration: number;
  aspectRatio: string;
  fit: string;
  videoMaxZoom: number;

  fileSelection: string;
  resolution: string;
  resolutionValue: number;
  minResolutionValue: number;
  bitrate: number;
  minBitrate: number;

  keywords_parser_type: string;
  contentXmlStructure: string;
  thumbnailsXmlStructure: string;
  createClip: boolean;
  immediateAvailability: boolean;
  skipTaggingForLongClips: boolean;
  maxDurationForTagging: number;
  importPlot: boolean;
  keywords: string;
  videoFileType: string;
  jsRendering: boolean;
};

const getSitemapFields = (fields: AllFieldsType): SitemapFeedPayloadType => {
  const payload: SitemapFeedPayloadType = {
    type: fields.type,
    name: fields.name,
    description: fields.description,
    lang: fields.lang,
    url: fields.url,
    auth_method: fields.authMethod,
    schedule_type: fields.scheduleType,
    accessLevel: fields.accessLevel,
    accessAllPublishers: fields.accessAllPublishers,
    user: fields.user,
    password: fields.password,
    use_for_download: fields.isUseForDownload,
    schedule_status: fields.scheduleStatus,
    schedule_value: fields.scheduleValue,
    schedule_frequency: fields.scheduleFrequency,
    fileSelection: fields.fileSelection,
    resolution: fields.resolution,
    resolutionValue: fields.resolutionValue,
    minResolutionValue: fields.minResolutionValue,
    bitrate: fields.bitrate,
    minBitrate: fields.minBitrate,
    maxStories: fields.maxStories,
    videoDuration: fields.videoDuration,
    aspectRatio: fields.aspectRatio,
    fit: fields.fit,
    videoMaxZoom: fields.videoMaxZoom,
    videoFileType: fields.videoFileType,
    jsRendering: fields.jsRendering,

    // default values
    contentXmlStructure: 'DEFAULT',
    thumbnailsXmlStructure: 'DEFAULT',
    keywords_parser_type: 'DEFAULT',
    createClip: true,
    immediateAvailability: false,
    skipTaggingForLongClips: true,
    maxDurationForTagging: 300,
    importPlot: true,
    keywords: '',
  };

  if (fields.id) {
    payload.id = fields.id;
  }

  if (fields.account?.id && !fields.id) {
    payload.accountId = fields.account.id;
  }

  if (fields.accessLevel === ACCESS_LEVEL_PRIVATE && fields.accessVideoOwner?.id) {
    payload.accessVideoOwnerId = fields.accessVideoOwner.id;
  }

  if (fields.accessLevel === ACCESS_LEVEL_HUB && fields.accessPublishers?.length && !fields.accessAllPublishers) {
    payload.accessPublishers = fields.accessPublishers.map((hub) => ({ publisherId: hub.id }));
  }

  return payload;
};

export default getSitemapFields;
