import createNextState , { isDraft } from 'immer' ; import type { EntityState , PreventAny } from './models' ; import type { PayloadAction } from '../createAction' ; import { isFSA } from '../createAction' ; import { IsAny } from '../tsHelpers' ; export function createSingleArgumentStateOperator < V > ( mutator ) { const operator = createStateOperator ( ( _ , state ) => mutator ( state ) ) ; return function operation < S extends EntityState < V > > ( state ) : S { return operator ( state as S , undefined ) ; } ; } export function createStateOperator < V , R > ( mutator ) { return function operation < S extends EntityState < V > > ( state , arg ) : S { function isPayloadActionArgument ( arg ) { return isFSA ( arg ) ; } const runMutator = ( draft ) => { if ( isPayloadActionArgument ( arg ) ) { mutator ( arg . payload , draft ) ; } else { mutator ( arg , draft ) ; } } ; if ( isDraft ( state ) ) { runMutator ( state ) ; return state ; } else { return createNextState ( state , runMutator ) ; } } ; }