import * as flatten from 'array-flatten' ; import createEvents from './create-events' ; import getEventsWithMatchingKey from './get-events-with-matching-key' ; import registerEvents from './register-events' ; import { EventDefinition , EventsMap , EventsMapper , Extensions , Target , } from './types' ; /**
 * Create a meta reducer that synchronizes actions to analytics events.
 */ function createMetaReducer ( eventsMap , target , extensions = { } ) { const getEvents = typeof eventsMap === 'function' ? action => flatten < EventDefinition > ( [ eventsMap ( action ) ] ) : action => getEventsWithMatchingKey ( eventsMap , action . type ) ; return function ( reducer ) { return function ( prevState , action ) { const nextState = reducer ( prevState , action ) ; const events = createEvents ( getEvents ( action ) , prevState , action , nextState ) ; registerEvents ( events , target , extensions , prevState , action , nextState ) ; return nextState ; } ; } ; } export default createMetaReducer ;