import { default as Immutable , RecordOf } from "immutable" ; import { Action } from "redux" ; import { MythicAction , Myths , ReducerDefinition } from "./types" ; export const makeReducer = < PKG extends string , NAME extends string , STATE , PROPS > ( reduce , appliesTo ) => reduce === undefined ? ( state , _action ) => state : ( state , action ) => { if ( appliesTo ( action ) ) { return ( reduce ( state , action as MythicAction < PKG , NAME , PROPS > ) || state ) ; } else { return state ; } } ; export const makeRootReducer = < PKG extends string , STATE > ( myths , initialState ) => ( state = Immutable . Record < STATE > ( initialState ) ( ) , action ) => { const applicableMyths = Object . values ( myths ) . filter ( myth => myth . appliesTo ( action ) ) ; if ( applicableMyths . length === 0 ) { return state ; } else if ( applicableMyths [ 0 ] . reduce === undefined ) { return state ; } else { return applicableMyths [ 0 ] . reduce ( state , action ) ; } } ;