import { getToken, setToken, getAuthorize, setAuthorize } from '@/utils/auth'
import Cache from '@/utils/cache'
const getDefaultState = () => {
  return {
    token: getToken(),
    authorize: getAuthorize(),
    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
  },
  SET_AUTHORIZE: (state) => {
    setAuthorize()
    state.authorize = true
  }
}

const actions = {
  authorize({ commit }) {
    commit('SET_AUTHORIZE')
  },
  token({ commit }, token) {
    commit('SET_TOKEN', token)
  },
  info({ commit }, info) {
    commit('SET_INFO', info)
  }
}

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

