import { COMMON_SELECT,COMMON_SELECT2 } from '@/utils/select_data'
import { getDictList } from '@/api/common'

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

/**
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1e18, symbol: 'E' },
    { value: 1e15, symbol: 'P' },
    { value: 1e12, symbol: 'T' },
    { value: 1e9, symbol: 'G' },
    { value: 1e6, symbol: 'M' },
    { value: 1e3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}

/**
 * 保留2位小数点
 * @param {number} num
 * @returns
 */
export function numToFixed(num) {
  if (!num) return '0.00'
  if (isNaN(Number(num))) return num
  num = num.toFixed(2)
  if (num.includes('.')) {
    return num.slice(0, -3).replace(/(\d)(?=(\d{3})+$)/g, '$1,') + num.slice(-3)
  }
  return num.replace(/(\d)(?=(\d{3})+$)/g, '$1,')
}

/**
 * 截取到“日”的日期
 * yyyy-MM-dd HH:mm:ss  =>  yyyy-MM-dd
 * @param {String} date
 */
export function dateToDayFilter(date) {
  if (!date) {
    return ''
  }
  return date.slice(0, 10)
}

/**
 * 截取到“分”的日期
 * yyyy-MM-dd HH:mm:ss  =>  yyyy-MM-dd HH:mm
 * @param {String} date
 */
export function dateToMinFilter(date) {
  if (!date) {
    return ''
  }
  return date.slice(0, 16)
}

/**
 * 转换成“年月”的日期
 * yyyy-MM-dd HH:mm:ss  =>  yyyy年MM月
 */
export function dateToYearMonthFilter(date) {
  if (!date) {
    return ''
  }
  return date.slice(0, 4) + '年' + date.slice(5, 7) + '月'
}

/**
 * 密码
 */
export function passwordFilter(value) {
  if (value === null || value === '') return ''
  let password = '********'
  // for (let i = 0; i < value.length; i++) {
  //   password += '*'
  // }
  return password
}

/**
 * 订单状态
 */
export function ORDER_CURRENT_STATUS_FILTER(value) {
  let obj = COMMON_SELECT.ORDER_CURRENT_STATUS.find(o => o.value === value)
  return obj ? obj.label : ''
}

export function CODE_CHECK_STATUS_FILTER(value) {
  if(value === null || value === '') return ''
  let obj = COMMON_SELECT2.CODE_CHECK_STATUS.find(o => o.value == value);
  return obj ? obj.label : '';
}