import { EventDef , EventDefHash } from './event-def' ; import { EventInstance , EventInstanceHash } from './event-instance' ; import { EventInput , parseEvent , EventTuple , buildEventRefiners } from './event-parse' ; import { filterHash } from '../util/object' ; import { CalendarContext } from '../CalendarContext' ; import { EventSource } from './event-source' ; export interface EventStore { defs : EventDefHash ; instances : EventInstanceHash ; } export function parseEvents ( rawEvents , eventSource , context , allowOpenRange ? ) { let eventStore = createEmptyEventStore ( ) ; let eventRefiners = buildEventRefiners ( context ) ; for ( let rawEvent of rawEvents ) { let tuple = parseEvent ( rawEvent , eventSource , context , allowOpenRange , eventRefiners ) ; if ( tuple ) { eventTupleToStore ( tuple , eventStore ) ; } } return eventStore ; } export function eventTupleToStore ( tuple , eventStore = createEmptyEventStore ( ) ) { eventStore . defs [ tuple . def . defId ] = tuple . def ; if ( tuple . instance ) { eventStore . instances [ tuple . instance . instanceId ] = tuple . instance ; } return eventStore ; } export function getRelevantEvents ( eventStore , instanceId ) { let instance = eventStore . instances [ instanceId ] ; if ( instance ) { let def = eventStore . defs [ instance . defId ] ; let newStore = filterEventStoreDefs ( eventStore , ( lookDef ) => isEventDefsGrouped ( def , lookDef ) ) ; newStore . defs [ def . defId ] = def ; newStore . instances [ instance . instanceId ] = instance ; return newStore ; } return createEmptyEventStore ( ) ; } function isEventDefsGrouped ( def0 , def1 ) { return Boolean ( def0 . groupId && def0 . groupId === def1 . groupId ) ; } export function createEmptyEventStore ( ) { return { defs : { } , instances : { } } ; } export function mergeEventStores ( store0 , store1 ) { return { defs : { ... store0 . defs , ... store1 . defs } , instances : { ... store0 . instances , ... store1 . instances } , } ; } export function filterEventStoreDefs ( eventStore , filterFunc ) { let defs = filterHash ( eventStore . defs , filterFunc ) ; let instances = filterHash ( eventStore . instances , ( instance ) => ( defs [ instance . defId ] ) ) ; return { defs , instances } ; } export function excludeSubEventStore ( master , sub ) { let { defs , instances } = master ; let filteredDefs = { } ; let filteredInstances = { } ; for ( let defId in defs ) { if ( ! sub . defs [ defId ] ) { filteredDefs [ defId ] = defs [ defId ] ; } } for ( let instanceId in instances ) { if ( ! sub . instances [ instanceId ] && filteredDefs [ instances [ instanceId ] . defId ] ) { filteredInstances [ instanceId ] = instances [ instanceId ] ; } } return { defs : filteredDefs , instances : filteredInstances , } ; }