
/**
 * 遍历组件导出模块
 * @param {folderList} vueComponent
 * @returns
 */
export function loadDynamicComponents (vueComponent) {
  // const vueComponent = require.context(`@/components/Dynamic/${folder}`, true, /\.vue$/)
  const autoComponent = {}
  vueComponent.keys().forEach(key => {
    const component = vueComponent(key)
    autoComponent[key.replace(/^\.\/(.*)\.\w+$/, '$1')] = component.default
  })
  return autoComponent
}

/**
 * 唯一id用于echarts
 */
export function generateUUID () {
  let d = new Date().getTime()
  if (window.performance && typeof window.performance.now === 'function') {
    d += performance.now()
    // use high-precision timer if available
  }
  const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    const r = (d + Math.random() * 16) % 16 | 0
    d = Math.floor(d / 16)
    return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16)
  })
  return uuid.replace(/-/g, '')
}

export function isIE () {
  return !!document.documentMode
}

/**
 *  驼峰转下划线
 * @param {String} str
 * @returns
 */
export function camelToUnderscore (str) {
  return str.replace(/([A-Z])/g, '_$1').toLowerCase()
}

/**
 *  下划线转驼峰
 * @param {String} str
 * @returns
 */
export function underscoreToCamel (str) {
  return str.replace(/_(\w)/g, (_, letter) => letter.toUpperCase())
}

// 文件流下载文件
export function downloadFile (data, filename, type = 'application/vnd.ms-excel') {
  const blob = new Blob([data], { type })
  if (window.navigator.msSaveOrOpenBlob) {
    // 兼容ie11
    try {
      window.navigator.msSaveOrOpenBlob(blob, filename)
    } catch (e) {
      console.log(e)
    }
    return
  }
  const objectUrl = URL.createObjectURL(blob)
  const link = document.createElement('a')
  link.href = objectUrl
  link.download = filename
  link.click()
  URL.revokeObjectURL(objectUrl)
}

/**
 * 文件流转json
 * @param {blob} blob
 * @returns
 */
export function blobToJson (blob) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()

    reader.onload = function (event) {
      const jsonString = event.target.result
      const jsonObject = JSON.parse(jsonString)
      resolve(jsonObject)
    }

    reader.onerror = function (error) {
      reject(error)
    }

    reader.readAsText(blob)
  })
}

/**
 * 获取当前元素距离 document 顶部的距离
 * @param { DOM } element
 * @returns
 */
export function getElementDistanceToTop (element) {
  let distance = 0
  while (element) {
    distance += element.offsetTop
    element = element.offsetParent
  }
  return distance
}

/**
 * 金额千分位分割
 * @param {*} number
 * @returns
 */
export function formatCurrency (number) {
  if (!number) {
    return number
  }
  const numberStr = number.toString()

  // 使用正则表达式将数字分成整数部分和小数部分
  const [integerPart, decimalPart] = numberStr.split('.')
  // 将整数部分转换为字符串，并每三位添加逗号
  const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
  if (!decimalPart) {
    return `${formattedInteger}.00`
  }
  // 将整数和小数部分组合回去
  return `${formattedInteger}.${decimalPart}`
}
