import { filterHash , mapHash } from '../util/object' ; import { EventDef } from '../structs/event-def' ; import { EventInstance , EventInstanceHash } from '../structs/event-instance' ; import { EventInput } from '../structs/event-parse' ; import { EventStore , mergeEventStores , createEmptyEventStore , filterEventStoreDefs , excludeSubEventStore , parseEvents , } from '../structs/event-store' ; import { Action } from './Action' ; import { EventSourceHash , EventSource } from '../structs/event-source' ; import { DateRange } from '../datelib/date-range' ; import { DateProfile } from '../DateProfileGenerator' ; import { DateEnv } from '../datelib/env' ; import { CalendarContext } from '../CalendarContext' ; import { expandRecurring } from '../structs/recurring-event' ; export function reduceEventStore ( eventStore , action , eventSources , dateProfile , context ) { switch ( action . type ) { case 'RECEIVE_EVENTS' : return receiveRawEvents ( eventStore , eventSources [ action . sourceId ] , action . fetchId , action . fetchRange , action . rawEvents , context ) ; case 'ADD_EVENTS' : return addEvent ( eventStore , action . eventStore , dateProfile ? dateProfile . activeRange : null , context ) ; case 'RESET_EVENTS' : return action . eventStore ; case 'MERGE_EVENTS' : return mergeEventStores ( eventStore , action . eventStore ) ; case 'PREV' : case 'NEXT' : case 'CHANGE_DATE' : case 'CHANGE_VIEW_TYPE' : if ( dateProfile ) { return expandRecurring ( eventStore , dateProfile . activeRange , context ) ; } return eventStore ; case 'REMOVE_EVENTS' : return excludeSubEventStore ( eventStore , action . eventStore ) ; case 'REMOVE_EVENT_SOURCE' : return excludeEventsBySourceId ( eventStore , action . sourceId ) ; case 'REMOVE_ALL_EVENT_SOURCES' : return filterEventStoreDefs ( eventStore , ( eventDef ) => ( ! eventDef . sourceId ) ) ; case 'REMOVE_ALL_EVENTS' : return createEmptyEventStore ( ) ; default : return eventStore ; } } function receiveRawEvents ( eventStore , eventSource , fetchId , fetchRange , rawEvents , context ) { if ( eventSource && fetchId === eventSource . latestFetchId ) { let subset = parseEvents ( transformRawEvents ( rawEvents , eventSource , context ) , eventSource , context ) ; if ( fetchRange ) { subset = expandRecurring ( subset , fetchRange , context ) ; } return mergeEventStores ( excludeEventsBySourceId ( eventStore , eventSource . sourceId ) , subset ) ; } return eventStore ; } function transformRawEvents ( rawEvents , eventSource , context ) { let calEachTransform = context . options . eventDataTransform ; let sourceEachTransform = eventSource ? eventSource . eventDataTransform : null ; if ( sourceEachTransform ) { rawEvents = transformEachRawEvent ( rawEvents , sourceEachTransform ) ; } if ( calEachTransform ) { rawEvents = transformEachRawEvent ( rawEvents , calEachTransform ) ; } return rawEvents ; } function transformEachRawEvent ( rawEvents , func ) { let refinedEvents ; if ( ! func ) { refinedEvents = rawEvents ; } else { refinedEvents = [ ] ; for ( let rawEvent of rawEvents ) { let refinedEvent = func ( rawEvent ) ; if ( refinedEvent ) { refinedEvents . push ( refinedEvent ) ; } else if ( refinedEvent == null ) { refinedEvents . push ( rawEvent ) ; } } } return refinedEvents ; } function addEvent ( eventStore , subset , expandRange , context ) { if ( expandRange ) { subset = expandRecurring ( subset , expandRange , context ) ; } return mergeEventStores ( eventStore , subset ) ; } export function rezoneEventStoreDates ( eventStore , oldDateEnv , newDateEnv ) { let { defs } = eventStore ; let instances = mapHash ( eventStore . instances , ( instance ) : EventInstance => { let def = defs [ instance . defId ] ; if ( def . allDay || def . recurringDef ) { return instance ; } return { ... instance , range : { start : newDateEnv . createMarker ( oldDateEnv . toDate ( instance . range . start , instance . forcedStartTzo ) ) , end : newDateEnv . createMarker ( oldDateEnv . toDate ( instance . range . end , instance . forcedEndTzo ) ) , } , forcedStartTzo : newDateEnv . canComputeOffset ? null : instance . forcedStartTzo , forcedEndTzo : newDateEnv . canComputeOffset ? null : instance . forcedEndTzo , } ; } ) ; return { defs , instances } ; } function excludeEventsBySourceId ( eventStore , sourceId ) { return filterEventStoreDefs ( eventStore , ( eventDef ) => eventDef . sourceId !== sourceId ) ; } export function excludeInstances ( eventStore , removals ) { return { defs : eventStore . defs , instances : filterHash ( eventStore . instances , ( instance ) => ! removals [ instance . instanceId ] ) , } ; }