// import Vue from 'vue'
import * as types from './mutations-types'
import storage from '../util/storage'
import {defaultUserInfo} from '../util/config'

export default {
  /**
   * 保存main vue 实例化对象
   * @param state
   * @param self
   */
  [types.SET_SELF](state, self) {
    state.self = self
  },

  /**
   * 设置 用户登录token
   * @param state
   * @param userToken
   */
  [types.SET_USER_TOKEN](state, userToken) {
    state.userToken = userToken
    if (userToken != null)
      storage.setCache(storage.type.USER_TOKEN, userToken)
    else {
      storage.delCache(storage.type.USER_TOKEN)
      //初始化 userInfo
      state.userInfo = Object.assign({}, defaultUserInfo)
    }
  },

  /**
   * 设置 用户资料
   * @param state
   * @param userInfo
   */
  [types.SET_USER_INFO](state, userInfo) {
    Object.assign(state.userInfo, userInfo)
	if (userInfo != null)
	  storage.setCache(storage.type.USER_INFO, userInfo)
	else {
	  storage.delCache(storage.type.USER_INFO)
	}
  },
  
}
