import { setCookie, getCookie } from './cookie';

export const APPTYPE = 3;

// 帮助函数
export const guid = () => {
	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
		/[xy]/g,
		function (c) {
			var r = (Math.random() * 16) | 0,
				v = c == 'x' ? r : (r & 0x3) | 0x8;
			return v.toString(16);
		},
	);
};

// 获取mid
export const getMid = async () => {
	let _mid;
	let _cookie_mid = await getCookie('mid');
	if (_cookie_mid) {
		_mid = _cookie_mid;
	} else {
		_mid = guid();
		console.log('_mid', _mid);
		await setCookie('mid', _mid);
	}
	return _mid;
};

// 获取host
export const getEnvConfig = () => {
	let host, logHost;

	// console.log('NODE_ENV=', process.env.NUXT_ENV.MODE);

	// @ts-ignore
	switch (process.env.NUXT_ENV.MODE) {
		case `prod`:
			host = `https://api.denetme.net`;
			logHost = 'https://log.denetme.net';
			break;
		case `pre`:
			host = `https://preapi.denetme.net`;
			logHost = 'https:/prelog.denetme.net';
			break;
		default:
			host = `https://testapi.denetme.net`;
			logHost = 'https://testlog.denetme.net';
			break;
	}

	return {
		host,
		logHost,
	};
};
