import { MutationTree } from 'vuex'
import { State } from './index'
import { AppLang, AppCtxLang, LangMain, LangData, LangForms } from '~/store/types'

export enum MutationTypes {
  addLang = 'addLang',
  addLangMain = 'addLangMain',
  addLangData = 'addLangData',
  addLangForms = 'addLangForms',
  removeLang = 'removeLang',
  editLang = 'editLang'
}
interface LangMap {
  key: string
  lang: AppLang
}
interface LangSection {
  key: string
  section: any
}
interface CtxLangMap {
  key: string
  lang: AppCtxLang
}

export type Mutations<S = State> = {
  [MutationTypes.addLang](state: S, lang_map: LangMap): void
  [MutationTypes.addLangMain](state: S, lang_map: LangSection): void
  [MutationTypes.addLangData](state: S, lang_map: LangSection): void
  [MutationTypes.addLangForms](state: S, lang_map: LangSection): void
  [MutationTypes.removeLang](state: S, lang_map: CtxLangMap): void
  [MutationTypes.editLang](state: S, lang_map: CtxLangMap): void
}

const get_lang_map = (state: State, mapkey: string): AppLang => {
  const lng = state.lang.get(mapkey)
  return lng || { main: {}, data: {}, forms: {} }
}

export const mutations: MutationTree<State> & Mutations = {
  [MutationTypes.addLang](state: State, lang_map: LangMap) {
    state.lang.set(lang_map.key, lang_map.lang)
  },
  [MutationTypes.addLangMain](state, lang_map: LangSection) {
    const lng = get_lang_map(state, lang_map.key)
    lng.main = lang_map.section
    state.lang.set(lang_map.key, lng)
  },
  [MutationTypes.addLangData](state, lang_map: LangSection) {
    const lng = get_lang_map(state, lang_map.key)
    lng.data = lang_map.section
    state.lang.set(lang_map.key, lng)
  },
  [MutationTypes.addLangForms](state, lang_map: LangSection) {
    const lng = get_lang_map(state, lang_map.key)
    lng.data = lang_map.section
    state.lang.set(lang_map.key, lng)
  },
  [MutationTypes.removeLang](state: State, lang_map: CtxLangMap) {
    const lang = get_lang_map(state, lang_map.key)
    const key = lang_map.lang.lng.key
    switch (lang_map.lang.ctx) {
      case 'main':
        if (lang.main[key]) {
          delete lang.main[key]
          state.lang.set(lang_map.key, lang)
        }
        break
      case 'data':
        if (lang.data[key]) {
          delete lang.data[key]
          state.lang.set(lang_map.key, lang)
        }
        break
      case 'forms':
        if (lang.forms[key]) {
          delete lang.forms[key]
          state.lang.set(lang_map.key, lang)
        }
        break
    }
  },
  [MutationTypes.editLang](state: State, lang_map: CtxLangMap) {
    const lang = get_lang_map(state, lang_map.key)
    const key = lang_map.lang.lng.key
    const val = lang_map.lang.lng.text
    switch (lang_map.lang.ctx) {
      case 'main':
        if (lang.main[key]) {
          lang.main[key] = val
          state.lang.set(lang_map.key, lang)
        }
        break
      case 'data':
        if (lang.data[key]) {
          lang.data[key] = val
          state.lang.set(lang_map.key, lang)
        }
        break
      case 'forms':
        if (lang.forms[key]) {
          lang.forms[key] = val
          state.lang.set(lang_map.key, lang)
        }
        break
    }
  },
}
