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

export enum ActionTypes {
  addLang = 'addLang',
  addLangMain = 'addLangMain',
  addLangData = 'addLangData',
  addLangForms = 'addLangForms',
  removeLang = 'removeLang',
  editLang = 'editLang'
}

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

export interface Actions {
  [ActionTypes.addLang](
    { commit }: AppLangActionContext,
    payload: any
  ): void
  [ActionTypes.addLangMain](
    { commit }: AppLangActionContext,
    payload: any
  ): void
  [ActionTypes.addLangData](
    { commit }: AppLangActionContext,
    payload: any
  ): void
  [ActionTypes.addLangForms](
    { commit }: AppLangActionContext,
    payload: any
  ): void
  [ActionTypes.removeLang](
    { commit }: AppLangActionContext,
    payload: any
  ): void
  [ActionTypes.editLang](
    { commit }: AppLangActionContext,
    payload: any
  ): void
}

export const actions: ActionTree<State, RootState> & Actions = {
  [ActionTypes.addLang]({ commit }, payload) {
    commit(MutationTypes.addLang, payload)
  },
  [ActionTypes.addLangMain]({ commit }, payload) {
    commit(MutationTypes.addLangMain, payload)
  },
  [ActionTypes.addLangData]({ commit }, payload) {
    commit(MutationTypes.addLangData, payload)
  },
  [ActionTypes.addLangForms]({ commit }, payload) {
    commit(MutationTypes.addLangForms, payload)
  },
  [ActionTypes.removeLang]({ commit }, payload) {
    commit(MutationTypes.removeLang, payload)
  },
  [ActionTypes.editLang]({ commit }, payload) {
    commit(MutationTypes.editLang, payload)
  },
}
