import type { StateCreator, StoreMutatorIdentifier } from '../vanilla.ts' import type { NamedSet } from './devtools.ts' type Write = Omit & U type Action = { type: string } type StoreRedux = { dispatch: (a: A) => A dispatchFromDevtools: true } type ReduxState = { dispatch: StoreRedux['dispatch'] } type WithRedux = Write> type Redux = < T, A extends Action, Cms extends [StoreMutatorIdentifier, unknown][] = [], >( reducer: (state: T, action: A) => T, initialState: T, ) => StateCreator>, Cms, [['zustand/redux', A]]> declare module '../vanilla' { interface StoreMutators { 'zustand/redux': WithRedux } } type ReduxImpl = ( reducer: (state: T, action: A) => T, initialState: T, ) => StateCreator, [], []> const reduxImpl: ReduxImpl = (reducer, initial) => (set, _get, api) => { type S = typeof initial type A = Parameters[1] ;(api as any).dispatch = (action: A) => { ;(set as NamedSet)((state: S) => reducer(state, action), false, action) return action } ;(api as any).dispatchFromDevtools = true return { dispatch: (...a) => (api as any).dispatch(...a), ...initial } } export const redux = reduxImpl as unknown as Redux