import api from '../../api'

const state = {
  menuInfo: JSON.parse(localStorage.getItem('menuInfo') || '[]')
}

// getters
const getters = {
  menuInfo: (state, getters, rootState) => {
    return state.menuInfo.length > 0 ? state.menuInfo : JSON.parse(localStorage.getItem('menuInfo') || '[]')
  }
}

// actions
const actions = {
  updateMenuInfo ({ commit }) {
    api.login.privMenu({}).then(res => {
      commit('setMenuInfo', res.data)
    })
  }
}

// mutations
const mutations = {
  setMenuInfo (state, menu) {
    state.menuInfo = menu
    localStorage.setItem('menuInfo', JSON.stringify(menu))
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
