import { addDays } from 'date-fns' ; import { getDtendProperty } from 'proton-shared/lib/calendar/vcalConverter' ; import { CalendarEvent } from 'proton-shared/lib/interfaces/calendar' ; import { getOccurrences } from 'proton-shared/lib/calendar/recurring' ; import { fromUTCDate , toUTCDate } from 'proton-shared/lib/date/timezone' ; import { VcalDateOrDateTimeProperty , VcalVeventComponent } from 'proton-shared/lib/interfaces/calendar/VcalModel' ; import { getIsAllDay , getPropertyTzid , getRecurrenceId } from 'proton-shared/lib/calendar/vcalHelper' ; import parseMainEventData from '../event/parseMainEventData' ; import deleteFutureRecurrence from '../recurrence/deleteFutureRecurrence' ; import { toExdate } from '../recurrence/helper' ; import { CalendarEventRecurring } from '../../../interfaces/CalendarEvents' ; export const getOriginalEvent = ( recurrences ) => { return recurrences . find ( ( Event ) => { const component = parseMainEventData ( Event ) ; if ( ! component ) { return false ; } return ! getRecurrenceId ( component ) ; } ) ; } ; export const getCurrentEvent = ( originalComponent , recurrence ) => { const isAllDay = getIsAllDay ( originalComponent ) ; const { localStart , localEnd } = recurrence ; const correctedLocalEnd = isAllDay ? addDays ( localEnd , 1 ) : localEnd ; const recurrenceDtstart = toExdate ( fromUTCDate ( localStart ) , isAllDay , getPropertyTzid ( originalComponent . dtstart ) ) ; const recurrenceDtend = toExdate ( fromUTCDate ( correctedLocalEnd ) , isAllDay , getPropertyTzid ( getDtendProperty ( originalComponent ) ) ) ; return { ... originalComponent , dtstart : recurrenceDtstart , dtend : recurrenceDtend , } ; } ; export const getRecurrenceEvents = ( recurrences , originalEvent ) => { return recurrences . filter ( ( Event ) => Event . ID !== originalEvent . ID ) ; } ; export const getExdatesAfter = ( exdates , date ) => { if ( ! Array . isArray ( exdates ) ) { return [ ] ; } return exdates . filter ( ( exdate ) => { const recurrenceId = toUTCDate ( exdate . value ) ; return recurrenceId >= date ; } ) ; } ; export const getRecurrenceEventsAfter = ( recurrences , date ) => { return recurrences . filter ( ( Event ) => { const component = parseMainEventData ( Event ) ; if ( ! component ) { return false ; } const rawRecurrenceId = getRecurrenceId ( component ) ; if ( ! rawRecurrenceId || ! rawRecurrenceId . value ) { return false ; } const recurrenceId = toUTCDate ( rawRecurrenceId . value ) ; return recurrenceId >= date ; } ) ; } ; export const getHasFutureOption = ( veventComponent , recurrence ) => { if ( recurrence . occurrenceNumber === 1 ) { return false ; } const veventFutureDeleted = deleteFutureRecurrence ( veventComponent , recurrence . localStart , recurrence . occurrenceNumber ) ; return getOccurrences ( { component : veventFutureDeleted , maxCount : 2 } ) . length >= 1 ; } ;