
import {
  http
} from '@/utils/request'

const getDefaultState = () => {
  return {
    platform: '',
    system: '',
    article: '',//隐私协议相关
    hiddenConfig: {},
    config:{}
  }
}


const state = getDefaultState()

const mutations = {
  SET_PLATFORM(state, value) {
    state.platform = value
  },
  SET_SYSTEM(state, value) {
    state.system = value
  },
  SET_HEIGHT(state, value) {
    state.screenHeight = value
  },
  //协议
  SET_ARTICLE(state, value) {
    state.article = value
  },
  SET_HIDDENCONFIG(state, value) {
    state.hiddenConfig = value
  },
  SET_CONFIG(state, value) {
    state.config = value
  }
}

const actions = {
  getSys({ commit }) {
    const { platform, system, screenHeight } = uni.getSystemInfoSync()
    commit('SET_PLATFORM', platform)
    commit('SET_SYSTEM', system)
  },

  //获取协议
  getArticle({ commit }) {
    return new Promise((resolve, reject) => {
      http.get('/v1/article-config').then(res => {
        commit('SET_ARTICLE', res)
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },

  async getConfit({ commit }) {
    const resData = await http.get('/v1/configs')
    // #ifdef APP-PLUS
    const { platform } = uni.getSystemInfoSync()
    const data = resData[platform]
    if (process.env.VUE_APP_VERSION_CODE >= data.v) {
      commit('SET_HIDDENCONFIG', data)
    }
    // #endif
    commit('SET_CONFIG', resData)
  }

}

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