import { ActionContext, ActionTree } from 'vuex'
import { Mutations, MutationTypes } from './mutations'
import { State } from './index'
import { RootState } from '~/store/index'

export enum ActionTypes {
  addCheck = 'addCheck',
  removeCheck = 'removeCheck',
  editCheck = 'editCheck'
}

type AppCheckActionContext = {
  commit<K extends keyof Mutations>(
    key: K,
    payload: Parameters<Mutations[K]>[1]
  ): ReturnType<Mutations[K]>
} & Omit<ActionContext<State, RootState>, 'commit'>

export interface Actions {
  [ActionTypes.addCheck](
    { commit }: AppCheckActionContext,
    payload: any
  ): void
  [ActionTypes.removeCheck](
    { commit }: AppCheckActionContext,
    payload: any
  ): void
  [ActionTypes.editCheck](
    { commit }: AppCheckActionContext,
    payload: any
  ): void
}

export const actions: ActionTree<State, RootState> & Actions = {
  [ActionTypes.addCheck]({ commit }, payload) {
    commit(MutationTypes.addCheck, payload)
  },
  [ActionTypes.removeCheck]({ commit }, payload) {
    commit(MutationTypes.removeCheck, payload)
  },
  [ActionTypes.editCheck]({ commit }, payload) {
    commit(MutationTypes.editCheck, payload)
  },
}
