// import parseTime, formatTime and set to filter
export { parseTime, formatTime }
  from '@/utils'

/**
 * 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, ','))
}

/**
 * 10000 => "10,000.00"
 * @param {number} num
 */
export function toThousandFloatFilter(num) {
  let result = toThousandFilter(num)
  const temp = result.split('.')
  if (temp.length === 1) {
    result += '.00'
  } else if (temp.length > 1) {
    if (temp[1].length < 2) {
      result += '0'
    }
  }
  return result
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}
/**
 * 处理金额
 * @param {String} string
 */
export function amountfilters(value) {
  // 处理金额
  if (value) {
    return Number(value).toFixed(2)
  } else {
    return '0.00'
  }
}
/**
 * 订单状态
 * @param {String} string
 */
export function orderStatusFilter(status) {
  const map = {
    '0': '待确认',
    '1': '待支付',
    '2': '交易中',
    '4': '交易完成',
    '5': '订单完成',
    '6': '已关闭',
    '7': '交易全退',
    '8': '退货退款',
    '11': '待收款、待发货',
    '12': '待收款、部分发货',
    '13': '待收款、全部发货',
    '21': '部分收款、待发货',
    '22': '部分收款、部分发货',
    '23': '部分收款、全部发货',
    '31': '全部收款、待发货',
    '32': '全部收款、部分发货',
    '33': '全部收款、全部发货'
  }
  return map[status]
}
