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

export enum ActionTypes {
  addData = 'addData',
  removeData = 'removeData',
  editData = 'editData'
}

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

export interface Actions {
  [ActionTypes.addData](
    { commit }: AppDataActionContext,
    payload: any
  ): void
  [ActionTypes.removeData](
    { commit }: AppDataActionContext,
    payload: any
  ): void
  [ActionTypes.editData](
    { commit }: AppDataActionContext,
    payload: any
  ): void
}

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