import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'

import state from './state'
import mutations from './mutations'
import actions from './actions'
import getters from './getters'

Vue.use(Vuex)

export default new Vuex.Store({
    state,
    mutations,
    getters,
    actions,
    plugins: [
        createPersistedState({
            storage: {
                // 将数据持久化到wx.storage
                getItem: (key) => uni.getStorageSync(key),
                setItem: (key, value) => uni.setStorageSync(key, value),
                removeItem: (key) => uni.removeStorageSync(key)
            },
            reducer: (val) => {
                // 只储存state中的部分数据
                return {
                    systemInfo: val.systemInfo,
                    isIphoneX: val.isIphoneX,
                    openId: val.openId,
                    sessionKey: val.sessionKey,
                    unionId: val.unionId,
                    token: val.token,
                    userInfo: val.userInfo,
                    pid: val.pid,
                    channel: val.channel,
                    adid: val.adid,
                    clickId: val.clickId,
                    callback: val.callback,
                    pageConfig: val.pageConfig,
                    closeVibrator: !!val.closeVibrator,
                    closeAnimal: !!val.closeAnimal,
                    hide: val.hide,
                    shareUserId: val.shareUserId
                }
            }
        })
    ]
})
