import { EXTERNAL_ENDPOINT, NON_RECURRING } from '../constants';

export const getEventData = (state, updateSpecificProperties, curUserId) => {
  const source = updateSpecificProperties
    ? {
        ...state,
        ...state.prevValue,
        ...updateSpecificProperties.getProperties(state),
      }
    : state;

  const {
    name,
    description,
    timezone,
    schedules,
    streamId,
    playerId,
    indicationText,
    indicationColor,
    playerAspectRatio,
    guid,
    preEventText,
    preEventType,
    preEventId,
    postEventText,
    postEventType,
    postEventId,
    transitionText,
    countdown,
    userId,
    aspectRatio,
    recurrentEndDate,
    recurrentPeriod,
    displayViewersCounter,
    forcePoster,
    forcePosterType,
  } = source;

  return {
    name,
    description,
    timezone,
    liveEventSchedules: schedules.map((schedule) => {
      const newSchedule = {
        title: schedule.title,
        startTime: schedule.startTime,
        endTime: schedule.endTime,
        durationIn: schedule.durationIn,
      };

      if (schedule?.pcnId) {
        newSchedule.id = schedule.pcnId;
      }

      return newSchedule;
    }),
    streamId,
    playerId,
    indicationText,
    indicationColor,
    aspectRatio: aspectRatio || playerAspectRatio,
    guid,
    preEventText,
    preEventType,
    preEventId,
    postEventText,
    postEventType,
    postEventId,
    transitionText,
    countdown,
    userId: userId || curUserId,
    startTime: schedules[0]?.startTime ?? 0,
    endTime: schedules[schedules.length - 1]?.endTime ?? 0,
    recurrentEndDate: recurrentPeriod !== NON_RECURRING ? recurrentEndDate : null,
    recurrent: recurrentPeriod !== NON_RECURRING,
    displayViewersCounter,
    forcePoster,
    forcePosterType,
  };
};

export const getImageData = (state) => {
  const { name, contentOwner, url } = state;

  return {
    name,
    contentOwner,
    url,
  };
};

export const getStreamData = (state) => {
  const {
    streamId,
    name,
    url,
    description,
    schedules,
    region,
    delivery,
    publisherId,
    feedId,
    liveCcEnabled,
    liveCcLang,
    recurrentEndDate,
    recurrentPeriod,
    timezone,
  } = state;

  return {
    id: streamId,
    name,
    url: delivery === EXTERNAL_ENDPOINT ? url : null,
    description,
    delivery,
    schedules: schedules.map((schedule) => {
      const editedSchedule = {
        title: schedule.title,
        startTime: schedule.startTime,
        endTime: schedule.endTime,
        durationIn: schedule.durationIn,
      };
      if (schedule.id) {
        editedSchedule.id = schedule.id;
      }
      return editedSchedule;
    }),
    region,
    publisherId,
    feedId,
    liveCcEnabled: delivery !== EXTERNAL_ENDPOINT ? liveCcEnabled : null,
    liveCcLang: delivery !== EXTERNAL_ENDPOINT && liveCcEnabled ? liveCcLang : null,
    recurrent: recurrentPeriod !== NON_RECURRING,
    recurrentPeriod: recurrentPeriod !== NON_RECURRING ? recurrentPeriod : null,
    recurrentEndDate,
    timeZone: timezone,
  };
};
