/**
 * @desc 函数防抖
 * @param func 函数
 * @param wait 延迟执行毫秒数
 * @param immediate true 表立即执行，false 表非立即执行
 */
export function debounce(func, wait, immediate = true) {
  let timeout
  return function () {
    if (timeout) clearTimeout(timeout)
    if (immediate) {
      var callNow = !timeout
      timeout = setTimeout(() => {
        timeout = null
      }, wait)
      if (callNow) func.apply(this, arguments)
    } else {
      timeout = setTimeout(function () {
        func.apply(context, args)
      }, wait)
    }
  }
}


// 获取完整的url，根据环境进行配置
export function getFullUrl(url) {
  let fullUrl = ''
  if (url && url.indexOf('http') != -1 && url.indexOf('files') != -1) {
    let urlArr = url.split('files')
    fullUrl = '/files' + urlArr[1]
  } else {
    fullUrl = url
  }
  return fullUrl
}


/**
 * 预览文件 
 * @param href 预览地址
 * @param previewName 预览文件用户看到的名称
 */
 export function previewFile(href, previewName) {
  let a = document.createElement("a");
  a.href = href;
  a.target = '_blank'
  a.download = previewName;
  a.style.display = "none";
  document.body.appendChild(a);
  a.click();
  a.remove();
}


/**
 * 增加千位分割符 
 * @param num 
 */
export function addSeparator(num){
  var res=num.toString().replace(/\d+/, function(n){ // 先提取整数部分
       return n.replace(/(\d)(?=(\d{3})+$)/g,function($1){
          return $1+",";
        });
  })
  return res;
}


/**
 * 增加万单位 
 * @param num 
 */
export function formatNum(num) {
	num = Number(num);
  console.log(num);
	if (num == 0) {
		return num + '';
	} else
	if (num > 0 && num < 10000) {
		return num + '';
	} else {
		return (num / 10000).toFixed(1) + 'w'; 
	}
}