import Vue from 'vue'
import Vuex from 'vuex'

import { LANGUAGES } from '@/js/lang'
import loginModule from './modules/login'
import partyBuilderModule from './modules/party-builder'
import popupsModule from './modules/popups'

Vue.use(Vuex)

export function createStore() {
  return new Vuex.Store({
    state() {
      return {
        lang: LANGUAGES.EN,
      }
    },
    getters: {
      getLang: state => state.lang,
    },
    mutations: {
      setLang(state, value) {
        state.lang = value
      },
      setUserId(state, value) {
        if (state.login) state.login.userId = value
      },
      logout(state) {
        if (state.login) {
          state.login.userId = null
          state.login.username = ''
        }
      },
      show_modal_login(state, value) {
        if (state.popups) state.popups.showModalLogin = value
      },
      removeMessage(state, key) {
        if (state.popups && state.popups.messages) {
          Vue.delete(state.popups.messages, key)
        }
      },
    },
    modules: {
      login: loginModule,
      party_builder: partyBuilderModule,
      popups: popupsModule,
    },
  })
}

// Экспорт по умолчанию для удобного импорта
export default createStore