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

export enum ActionTypes {
  addProfile = 'addProfile',
  removeProfile = 'removeProfile',
  editProfile = 'editProfile'
}

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

export interface Actions {
  [ActionTypes.addProfile](
    { commit }: AppProfileActionContext,
    payload: any
  ): void
  [ActionTypes.removeProfile](
    { commit }: AppProfileActionContext,
    payload: any
  ): void
  [ActionTypes.editProfile](
    { commit }: AppProfileActionContext,
    payload: any
  ): void
}

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