import { getToken, setToken, removeToken } from '@/utils/auth'
import Cache from '@/utils/cache'
const getDefaultState = () => {
  return {
    token: getToken(),
    info: Cache.get('userInfo')
  }
}

const state = getDefaultState()

const mutations = {
  SET_TOKEN: (state, token) => {
    setToken(token)
    state.token = token
  },
  SET_INFO: (state, info) => {
    Cache.set('userInfo', info)
    state.info = info
  },
  CLEAR_INFO: (state) => {
    removeToken()
    Cache.remove('userInfo')
    state.token = null
    state.info = null
  }
}

const actions = {
  token({ commit }, token) {
    commit('SET_TOKEN', token)
  },
  info({ commit }, info) {
    commit('SET_INFO', info)
  },
  clear({ commit }) {
    commit('CLEAR_INFO')
  }
}

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

