import { ActionCreator , PayloadType } from "./action-creator" ; export type Mappable = Record < string , ActionCreator < any > > ; export function mapActions < T extends Mappable > ( map , namespace ? ) { return Object . keys ( map ) . map ( v => ( { key : v , action : map [ v ] } ) ) . reduce ( ( acc , item ) => { const action = item . action ; return { ... acc , [ item . key ] : function ( this , payload ) { return this . $store . dispatch ( action ( payload , { namespace : namespace || action . namespace } ) ) ; } } ; } , { } ) as any ; } export function mapMutations < T extends Mappable > ( map , namespace ? ) { return Object . keys ( map ) . map ( v => ( { key : v , action : map [ v ] } ) ) . reduce ( ( acc , item ) => { const action = item . action ; return { ... acc , [ item . key ] : function ( this , payload ) { return this . $store . commit ( action ( payload , { namespace : namespace || action . namespace } ) ) ; } } ; } , { } ) as any ; } export const createNamespacedHelpers = ( namespace ) => ( { mapMutations : < T extends Mappable > ( map ) => mapMutations ( map , namespace ) , mapActions : < T extends Mappable > ( map ) => mapActions ( map , namespace ) } ) ;