import {
	defineStore
} from "pinia";
import {
	getCommonConfig
} from '@/api/init.js'
const $map = {
	882: 'isWxIosPay',
	833: 'checkIosLogin',
	834: 'checkIosPay',
	835: 'checkWxLogin',
	836: 'checkPhoneLogin',
	108: 'isOpenWxWebAutoLogin',
	817: 'zhengbu',
	818: 'danbu',
	252: 'adUnitId',
	821: 'playType',
	251: 'isGuanggao',
	254: 'isGuanggaody',
	202: 'kefu',
	249: 'moreSearch', //热搜词
	206: 'kefuPhone',
	204: 'kefuUrl',
	203: 'kefuAppId',
	248: 'isVips',
	49: 'AppUrl',
	823: 'OfferID',
	824: 'payEnv',
	825: 'moneyTips',
	855: 'kmPaySel',
	849: 'homeTypeSel',
	856: 'syPaySel',
	857: 'imId',
	858: 'isAccountPay',
	860: 'dyadUnitId',
	881: '',
	109: '',
	922: 'withdrawNum',
	500: 'payTips',
	914: 'goldBili', // 金币比例
	921: 'freeDuration' //看广告免费分钟时长
}

export const useCommonStore = defineStore("common", {
	state() {
		return {
			payTips: '付款完成后不要忘记抽红包哦',
			goldBili: 0,
			sysInfo: {},
			isIos: false,
			isExamines: 0
		};
	},
	actions: {
		async init() {
			this.sysInfo = uni.getSystemInfoSync()
			this.isIos = this.sysInfo.platform == 'ios'
			const res = await getCommonConfig()
			if (res) {
				for (let i in $map) {
					const key = $map[i]
					if (key) {
						this[key] = res[i]
						uni.setStorageSync(key, res[i])
					}
				}
			}
		},
		setversion(a, b) {
			return new Promise(async (resolve, reject) => {
				this.isExamines = await this.compare(a, b)
				console.log('判斷值-' + this.isExamines)
				resolve(this.isExamines)
			})
		},
		// 判断版本号
		compare(version1, version2) {
			let arr1 = version1.split(".");
			let arr2 = version2.split(".");
			let length = Math.max(arr1.length, arr2.length);
			for (let i = 0; i < length; i++) {
				const n1 = Number(arr1[i] || 0)
				const n2 = Number(arr2[i] || 0)
				// version1 > version2 返回1，如果 version1 < version2 返回-1，不然返回0
				// 当前版 n1 接口版本是 n2   
				// 1小于2 更新弹窗、抽奖等显示
				if (n1 < n2) return 1
				// 1大于2 更新弹窗、抽奖不显示
				if (n1 > n2) return -1
			}
			// APP等于2 更新弹窗不显示‘抽奖显示
			return 0
		}

	},
	getters: {
		isIosExamine: function() {
			console.log(this.isExamines)
			if (!this.isIos) {
				return true
			}
			if (this.isExamines == -1) {
				return false
			}
			return true
		}
	},
	unistorage: true, // 开启后对 state 的数据读写都将持久化
});