import { STORE_ID, PLATFORM, REFEREE_ID } from '@/store/mutation-types'
import storage from '@/utils/storage'

const app = {
  state: {
    // 当前商城的ID
    storeId: null,
    // 当前终端平台
    platform: '',
    // 推荐人ID
    refereeId: null
  },

  mutations: {
    SET_STORE_ID: (state, value) => {
      state.storeId = value
    },
    SET_PLATFORM: (state, value) => {
      state.platform = value
    },
    SET_REFEREE_ID: (state, value) => {
      state.refereeId = value
    }
  },

  actions: {

    // 记录推荐人ID
    SaveRefereeId({ commit }, value) {
      const store = this
      const refereeId = parseInt(value)
      return new Promise((resolve, reject) => {
        if (refereeId > 0 && store.getters.userId != refereeId) {
          // 保存推荐人ID到缓存
          storage.set(REFEREE_ID, refereeId)
          // 记录到store全局变量
          commit('SET_REFEREE_ID', refereeId)
          resolve()
        }
      })
    }

  }
}

export default app
