/**
 * 检验数据是否为空
 */
export const isEmpty = obj => {
	if (obj === '' || obj === null || obj === undefined) {
		return true
	} else if (obj.constructor === Array && obj.length === 0) {
		return true
	} else if (obj.constructor === Object && Object.keys(obj).length === 0) {
		return true
	} else {
		return false
	}
}

/**
 * 生成UUID
 */
export const generateUUID = () => {
	let d = new Date().getTime()
	let uuid = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		// let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		let r = (d + Math.random() * 16) % 16 | 0
		d = Math.floor(d / 16)
		return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16)
	})
	return uuid
}

/**
 * 移除空字符
 */
export const removeNullCharacter = str => {
	return str.replace(/\\u([0-9]|[a-fA-F])([0-9]|[a-fA-F])([0-9]|[a-fA-F])([0-9]|[a-fA-F])/g, '')
}

/**
 * 格式化空字符为空字符串
 */
export const formatNullCharacter = str => {
	if (!str) return ''
	return JSON.parse(removeNullCharacter(JSON.stringify(str)))
}

/**
 * 节流函数
 * fn是我们需要包装的事件回调, interval是时间间隔的阈值
 */
export const throttle = (fn, interval) => {
	// last为上一次触发回调的时间
	let last = 0

	interval = interval || 1000

	// 将throttle处理结果当作函数返回
	return function() {
		// 保留调用时的this上下文
		let context = this
		// 保留调用时传入的参数
		let args = arguments
		// 记录本次触发回调的时间
		let now = +new Date()

		// 判断上次触发的时间和本次触发的时间差是否小于时间间隔的阈值
		if (now - last >= interval) {
			// 如果时间间隔大于我们设定的时间间隔阈值，则执行回调
			last = now
			fn.apply(context, args)
		}
	}
}
/**
 * 格式化得到aid值
 * @param {Object} buffer
 */
export const ab2hex = function(buffer) {
	let hexArr = Array.prototype.map.call(
		new Uint8Array(buffer),

		function(bit) {
			return ('00' + bit.toString(16)).slice(-2);
		}
	);
	return hexArr.join('');
};


/**
 * 字符串转ArrayBuffer
 * @param {Object} str
 */
export function stringToArrayBuffer(str) {
	var bytes = new Array();
	let len, c;
	len = str.length;
	for (let i = 0; i < len; i++) {
		c = str.charCodeAt(i);
		if (c >= 0x010000 && c <= 0x10FFFF) {
			bytes.push(((c >> 18) & 0x07) | 0xF0);
			bytes.push(((c >> 12) & 0x3F) | 0x80);
			bytes.push(((c >> 6) & 0x3F) | 0x80);
			bytes.push((c & 0x3F) | 0x80);
		} else if (c >= 0x000800 && c <= 0x00FFFF) {
			bytes.push(((c >> 12) & 0x0F) | 0xE0);
			bytes.push(((c >> 6) & 0x3F) | 0x80);
			bytes.push((c & 0x3F) | 0x80);
		} else if (c >= 0x000080 && c <= 0x0007FF) {
			bytes.push(((c >> 6) & 0x1F) | 0xC0);
			bytes.push((c & 0x3F) | 0x80);
		} else {
			bytes.push(c & 0xFF);
		}
	}
	let array = new Int8Array(bytes.length);
	for (let i in bytes) {
		array[i] = bytes[i];
	}
	return array.buffer;
}

/**
 * ArrayBuffer转字符串
 * @param {Object} arr
 */
export function arrayBufferToString(arr) {
	if (typeof arr === 'string') {
		return arr;
	}
	let dataview = new DataView(arr);
	let ints = new Uint8Array(arr.byteLength);
	for (let i = 0; i < ints.length; i++) {
		ints[i] = dataview.getUint8(i);
	}
	arr = ints;
	let str = '',
		_arr = arr;
	for (let i = 0; i < _arr.length; i++) {
		let one = _arr[i].toString(2),
			v = one.match(/^1+?(?=0)/);
		if (v && one.length == 8) {
			let bytesLength = v[0].length;
			let store = _arr[i].toString(2).slice(7 - bytesLength);
			for (let st = 1; st < bytesLength; st++) {
				store += _arr[st + i].toString(2).slice(2);
			}
			str += String.fromCharCode(parseInt(store, 2));
			i += bytesLength - 1;
		} else {
			str += String.fromCharCode(_arr[i]);
		}
	}
	return str;
}

export function byteLength(str) {
	// 匹配所有的单字节字符和双字节字符
	const matches = str.match(/[^\x00-\xff]/g);
	// 计算字符串的字节长度
	return str.length + (matches ? matches.length : 0);
}