import { STORAGE } from '@/core/constants/storage-keys'

const emptyStats = () => ({
	order: { pay: 0, delive: 0, receiving: 0, evaluate: 0, customer: 0, whole: 0, groups: 0 },
	dynamic: { collection: 0, concern: 0, footprint: 0, coupon: 0, accountbank: 0 },
	notice: { order: 0, notice: 0, chat: 0 },
	logistics: []
})

export default {
	namespaced: true,
	state: emptyStats(),
	mutations: {
		replace(state, payload) {
			Object.assign(state, payload || emptyStats())
			uni.setStorageSync(STORAGE.stats, state)
		},
		merge(state, patch) {
			Object.keys(patch || {}).forEach((block) => {
				if (state[block] && typeof state[block] === 'object') {
					Object.assign(state[block], patch[block])
				}
			})
			uni.setStorageSync(STORAGE.stats, state)
		}
	},
	actions: {
		hydrate({ commit }) {
			const cached = uni.getStorageSync(STORAGE.stats)
			if (cached) commit('replace', cached)
		},
		merge({ commit }, patch) {
			commit('merge', patch)
		}
	}
}
