
const sldStatCommonProperty = ''

import { useFiltersStore } from '@/store/filter.js'
import { post } from './request';

/**
 * 初始化统计
 * @params showDebug Boolean 是否开启统计日志，默认false 不开启
 * @zjf-2021-06-27
 */
export function initStat(showDebug = false, initStatCommonProperty) {
	//获取udid
	let uuid = ''
	uuid = getUUID();
	sldStatCommonProperty = {
		...initStatCommonProperty,
		uuid: uuid,
	}
	console.log(sldStatCommonProperty)
}

/**
 * 设置/更新统计的公共属性
 * @params data Object 要更新的属性数据
 * @zjf-2021-06-27
 */
 let StatCommonProperty = {}
export function updateStatCommonProperty(data) {
	let target = {};
	// const value = localStorage.getItem('sldStatCommonProperty');
	// if (value) {
	// 	target = JSON.parse(value);
	// }
	// target = {
	// 	...target,
	// 	...data
	//  }; //更新或者新增统计的公共属性
	// localStorage.sldStatCommonProperty = JSON.stringify(target);
	console.log(data)
	StatCommonProperty = data
	return data;
}

/**
 * 同步获取指定key对应的内容
 * @params key 指定的缓存key
 * @zjf-2021-06-27
 */
export function getStatStorage(key) {
	// let target = {};
	// const value = localStorage.getItem(key);
	// if (value) {
	// 	target = JSON.parse(value);
	// }
	// return target;
}

/**
 * 获取uuid
 * 如：1624819897644-1389918-0ed8161319cedb-22991203
 * Math.random().toString(16).replace('.', '')：0～1的随机数以十六进制显示，并去掉小数点，如：0.f03fb618bf531，并去掉小数点
 * @zjf-2021-06-27
 */
export function getUUID() {
	return "" + Date.now() + '-' + Math.floor(1e7 * Math.random()) + '-' + Math.random().toString(16).replace('.', '') +
		'-' + String(Math.random() * 31242).replace('.', '').slice(0, 8);
}



/**
 * 统计事件
 * @params params Object 参数
 * @zjf-2021-06-27
 */
export function sldStatEvent(data) {
	const filtersStore = useFiltersStore()
	let a =  {
		equipmentType: 1,//设备类型，1-pc，2-移动设备，3-其他
		source: 'pc',//终端名称，pc-pc；h5-H5；android-Android；ios-IOS；xcx-微信小程序
		memberId: filtersStore.getMemberInfo.memberId,//会员id默认为0
		ip: '',//移动端ip默认都为空
		cityCode: "",
		cityName: "",
		location: "",
		provinceCode: "",
		provinceName: "",
	  }
	//获取udid
	
	let uuid = ''
	if(filtersStore.getUUid != ''){
		uuid = filtersStore.getUUid
	}else{
		uuid = getUUID();
		filtersStore.setUUid(uuid)
	}
	let targetParams = {
		...a,
		uuid: uuid,
		...data,
		...StatCommonProperty
	};
	

	//发送请求
	post("v3/statistics/front/member/behavior/save", { u: base64Encode(JSON.stringify(targetParams)) }).then();
}
