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

export enum ActionTypes {
  addDefs = 'addDefs',
  removeDefs = 'removeDefs',
  editDefs = 'editDefs'
}

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

export interface Actions {
  [ActionTypes.addDefs](
    { commit }: AppDefsActionContext,
    payload: any
  ): void
  [ActionTypes.removeDefs](
    { commit }: AppDefsActionContext,
    payload: any
  ): void
  [ActionTypes.editDefs](
    { commit }: AppDefsActionContext,
    payload: any
  ): void
}

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