import { IMG_CND_Prefix } from './constant'
/**
 *  精度
 * @param {*} num
 * @param {*} precision
 * @returns
 */
export const strip = (num, precision = 12) => {
  return +parseFloat(num.toPrecision(precision))
}
/**
 *
 * @param {千位分割付} number
 * @returns
 */
export const numberWithCommas = ({ number = 0, digit = 2 }) => {
  if (!Number.isNaN(Number(number))) {
    // return parseFloat(number).toFixed(digit).replace(/^\B(?<!\.\d*)(?=(\d{3})+(?!\d))$/g, ",");
    return parseFloat(number).toLocaleString('zh', { minimumFractionDigits: digit })
  }
  else {
    return 0.0
  }
}
/**
 * 移除井号
 * @param {String} val code 编码
 * @returns
 */
export const formatRemoveHashTag = (val = '') => {
  // console.log('移除标签',val,val.endsWith("#"));
  return val.endsWith('#') ? val.replace('#', '') : val
}

/**
 * 格式化编码+名称显示方式
 * @param {String} code 编码
 * @param {String} name 名称
 * @param {*} mode 模式 both:code + 名称 name: 仅显示名称
 * @returns
 */
export const formatHashTag = (code = '', name = '', mode = 'both') => {
  if (mode == 'both') {
    return `${formatRemoveHashTag(code)}# ${name}`
  }
  else if (mode == 'name') {
    return `${name}`
  }
}

const Digit = 10 * 10

/**
 * 重量 进退位 单位
 */
export const weightDigit = 1000

/**
 * 厘米进退位单位
 */
export const meterDigit = 100

/**
 * 除以
 * @param {*} val
 * @param {*} digit
 * @param {*} priceStatus //是否显示千位分割
 * @returns
 */
export const formatPriceDiv = (val, digit = Digit, priceStatus = false) => {
  const res = strip(Number(val / digit)) || 0
  return priceStatus ? numberWithCommas({ number: res }) : res
}
/**
 * 乘以
 * @param {*} val
 * @param {*} digit
 * @returns
 */
export const formatPriceMul = (val, digit = Digit) => {
  return strip(Number(val * digit)) || 0
}

/**
 * 格式化长度 (乘以)
 * @param {Number} val
 * @returns
 */
export const formatMeterMul = (val, digit = meterDigit) => {
  return strip(Number(val * digit)) || 0
}

/**
 * 格式化长度 (除以)
 * @param {*} val
 */
export const formatMeterDiv = (val, digit = meterDigit) => {
  return strip(Number(val / digit)) || 0
}

/**
 * 格式化重量单位 (乘以)
 * @param {Number} val
 * @returns
 */
export const formatWeightMul = (val, digit = weightDigit) => {
  return strip(Number(val * digit)) || 0
}

/**
 * 格式化重量单位 (除以)
 * @param {*} val
 */
export const formatWeightDiv = (val, digit = weightDigit) => {
  return strip(Number(val / digit)) || 0
}

export const formatDateTime = (val, fmt = 'YYYY-MM-DD HH:mm:ss') => {
  if (val) {
    const time = new Date(val)
    const Y = time.getFullYear()
    const M = time.getMonth() + 1
    const d = time.getDate()
    const h = time.getHours()
    const m = time.getMinutes()
    const s = time.getSeconds()

    fmt = fmt
      .replace('YYYY', Y)
      .replace('MM', M.toString().padStart(2, '0'))
      .replace('DD', d.toString().padStart(2, '0'))
      .replace('HH', h.toString().padStart(2, '0'))
      .replace('mm', m.toString().padStart(2, '0'))
      .replace('ss', s.toString().padStart(2, '0'))
    console.log('fmt', fmt)
    return fmt
  }
  else {
    return val
  }
}

/**
 * 数值保留两位小数
 * @param {*} x
 * @returns
 */
export const toDecimal2 = (x) => {
  let f = parseFloat(x)
  if (Number.isNaN(f)) {
    return 0
  }
  f = `${f}`
  const index = f.lastIndexOf('.')
  if (index >= 0) {
    const decimal = f.substring(index + 1)
    if (decimal.length == 1) {
      f = `${f.substring(0, index + 1) + decimal}0`
    }
    else {
      f = f.substring(0, index + 1) + decimal.substring(0, 2)
    }
  }
  return f
}

/**
 * 转换金额单位
 * @param {*} num 金额 / 数值
 * @param {*} digit 转换单位
 * @returns
 */
export const formatMillionYuan = (num, digit = 10000) => {
  return num / digit > 1 ? { num: toDecimal2(num / digit), million: true } : { num, million: false }
}

// 匹配前缀 例如：//xxx.com  或者 http://xxx.com 或者 https://xxx.com
// eslint-disable-next-line no-useless-escape
const URL_REGEXP = /(https?:)?\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&=]*)/gi
/**
 * 判断是否有前缀
 * @param {string} url url路径
 */
export const withBaseUrl = function(url) {
  return URL_REGEXP.test(url)
}
/**
 * 去除前缀 //xxx.com  或者 http://xxx.com 或者 https://xxx.com
 * @param {string} url url路径
 */
export const breakupUrl = (url) => {
  return url?.replace(URL_REGEXP, '') ?? url
}
/**
 * 拼接前缀 http://xxx.com + /asdfsafdas/154531asdf465413.png
 * @param {string} url url路径
 */
export const jointUrl = (url) => {
  return withBaseUrl(url) ? url : `${IMG_CND_Prefix}${url}`
}

export const formatUrl = (url, suffix = '!w200') => {
  if (url) {
    url = url.includes(',') ? url.split(',')[0] : url
    return withBaseUrl(url) ? url + suffix : jointUrl(url) + suffix
  }
  else {
    return `${IMG_CND_Prefix}/mall/no_img.png`
  }
}

/**
 * 格式化图片路径
 * @param {*} url
 * @status true|false
 * @returns
 */
export const formatImgUrl = (url, suffix = '!w200') => {
  if (url) {
    return url.includes('http') ? url + suffix : IMG_CND_Prefix + url + suffix
  }
  else {
    return `${IMG_CND_Prefix}/mall/no_img.png`
  }
}

/**
 *
 * @param {纹理图} imgurl
 * @param {rgb} rgb
 * @param {} suffix
 * @returns 1 有纹理图，2 有rgb 3默认图
 */
export const isLabImage = (imgurl, rgb, suffix = '!w200') => {
  if (imgurl) {
    return { status: 1, value: `${IMG_CND_Prefix}/${imgurl}${suffix}` }
  }
  else if (rgb.r != 0 || rgb.g != 0 || rgb.b != 0) {
    return { status: 2, value: rgb }
  }
  else {
    return { status: 3, value: `${IMG_CND_Prefix}/mall/no_img.png` }
  }
}

// 计算数值大于指定值
export const setPriceUnit = (num, cls) => {
  if (cls) {
    const res = formatMillionYuan(num, 1000)
    return {
      cls: res.million ? cls : '',
      num: res.num,
    }
  }
  else if (num > 100000000) {
    const res = formatMillionYuan(num, 100000000)
    return {
      cls: res.million ? 's-e' : '',
      num: res.num,
    }
  }
  else {
    const res = formatMillionYuan(num)
    return {
      cls: res.million ? 's-w' : '',
      num: res.num,
    }
  }
}

// 不四舍五入, val:原数据,num:保留位数
const noScale = (val, num = 2) => {
  val = parseFloat(val)
  num = parseInt(num)
  const power = Math.pow(10, num)
  val = `${val * power}`
  val = val.split('.')[0]
  val = parseInt(val) / power
  return val
}

// 处理数据单位(自然数)
export const dataUnit = (val) => {
  let newVal = ''
  val = parseFloat(val)
  if (val >= 100000000) {
    newVal = `${noScale(val / 100000000)}亿`
  }
  else if (val >= 10000000) {
    newVal = `${noScale(val / 10000000)}kw`
  }
  else if (val >= 10000) {
    newVal = `${noScale(val / 10000)}w`
  }
  else if (val >= 1000) {
    newVal = `${noScale(val / 1000)}k`
  }
  else {
    newVal = val
  }
  return newVal
}
