import { useReducer } from 'react'
type State = {
    data?: any[],
    total?: number
}
export type Action = {
    type: 'set',
    params: any
}
export function reducer(state: State, action: Action) {
    let { type, params } = action
    let newState = JSON.parse(JSON.stringify(state))
    switch (type) {
        case 'set':
            Object.keys(params as State).forEach((key: string) => {
                newState[key] = (params as State)[key]
            })
            return newState
        default:
            return state;
    }
}
const defultData = { data: [], total: 0 }
export default function useDB() {
    const [state, dispatch] = useReducer(reducer, defultData)
    return {
        state,
        dispatch
    }
}
