import { getPro } from '@build/version';

/**
 * @description 获取localStorage
 * @param {String} key Storage名称
 * @return string
 */
export const localGet = (key: string) => {
	const value = window.localStorage.getItem(key);
	try {
		return JSON.parse(window.localStorage.getItem(key) as string);
	} catch (error) {
		return value;
	}
};

/**
 * @description 存储localStorage
 * @param {String} key Storage名称
 * @param {any} value Storage值
 * @return void
 */
export const localSet = (key: string, value: any) => {
	window.localStorage.setItem(key, JSON.stringify(value));
};

/**
 * @description 清除localStorage
 * @param {String} key Storage名称
 * @return void
 */
export const localRemove = (key: string) => {
	window.localStorage.removeItem(key);
};

/**
 * @description 清除所有localStorage
 * @return void
 */
export const localAllClear = () => {
	window.localStorage.clear();
};

/**
 * @description 判断数据类型
 * @param {Any} val 需要判断类型的数据
 * @return string
 */
export const isType = (val: any) => {
	if (val === null) return 'null';
	if (typeof val === 'object') return typeof val;
	return Object.prototype.toString.call(val).slice(8, -1).toLocaleLowerCase();
};

/**
 * 获取UUID
 * @returns UUID
 */
export const getUUID = () => {
	return crypto.randomUUID();
};

/**
 * * 判断是否是最新版
 */
export const currentVersionIsNew = async () => {
	const version = await getPro();
	// 如果不是强制刷新页面
	if (version.new) location.reload();
};
