import { makeScope } from '../makeScope' ; import { ICompiledDefinition } from '../types/definition' ; import { IModelFunction , IModelDefinition , IIntermediateModel } from '../types/model' ; import { getActionType } from '../utils' ; export const Model = { fromDefinition : < LocalState > ( compiledDefinition , namespacing ) : IModelDefinition < LocalState > => ( { kind : 'definition' , ... compiledDefinition . generate ( namespacing ) , } ) , fromFunction : ( fn , namespacing ) : IModelFunction => { const type = getActionType ( namespacing ) ; const { reducer , action } = makeScope ( namespacing , true ) ( type , fn ) ; return { kind : 'function' , action , reducers : { [ type ] : reducer } , } ; } , update : < LocalState > ( rootModel , field , model ) : IIntermediateModel => { const { actions , initialState , reducers , selectors } = model ; return { ... rootModel , reducers : { ... rootModel . reducers , ... reducers , } , actions : { ... rootModel . actions , [ field ] : actions , } , selectors : { ... rootModel . selectors , [ field ] : selectors , } , initialState : { ... rootModel . initialState , [ field ] : initialState , } , } ; } } ;