
import {
  COMMON_SELECT
} from '@/utils/select_data'

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


/**
 * 截取到“日”的日期
 * 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 MEMBER_CURRENT_STATUS_FILTER(value) {
  let obj = COMMON_SELECT.MEMBER_CURRENT_STATUS.find(o => o.value === value);
  return obj ? obj.label : ''
}

/**
 * 会员的类型
 */
export function MEMBER_CURRENT_TYPE_FILTER(value) {
  let obj = COMMON_SELECT.MEMBER_CURRENT_TYPE.find(o => o.value === value);
  return obj ? obj.label : ''
}

/**
 * 订单状态
 */
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 EXPRESS_CURRENT_STATE_FILTER(value) {
  let obj = COMMON_SELECT.EXPRESS_CURRENT_STATE.find(o => o.value === value);
  return obj ? obj.label : ''
}

/**
 * 维护订单状态
 */
export function ORDER_REFUND_CURRENT_STATUS_FILTER(value) {
  let obj = COMMON_SELECT.ORDER_REFUND_CURRENT_STATUS.find(o => o.value === value);
  return obj ? obj.label : ''
}

/**
 * 结算状态
 */
export function SETTLEMENT_CURRENT_STATUS_FILTER(value) {
  let obj = COMMON_SELECT.SETTLEMENT_CURRENT_STATUS.find(o => o.value === value);
  return obj ? obj.label : ''
}

/**
 * 退款方式
 */
export function REFUND_CURRENT_TYPE_FILTER(value) {
  let obj = COMMON_SELECT.REFUND_CURRENT_TYPE.find(o => o.value === value);
  return obj ? obj.label : ''
}

/**
 * 券标志
 */
export function COUPON_CURRENT_FLAG_FILTER(value) {
  let obj = COMMON_SELECT.COUPON_CURRENT_FLAG.find(o => o.value === value);
  return obj ? obj.label : ''
}

/**
 * 秒杀活动状态
 */
export function SEC_KILL_CURRENT_STATUS_FILTER(value) {
  let obj = COMMON_SELECT.SEC_KILL_CURRENT_STATUS.find(o => o.value === value);
  return obj ? obj.label : ''
}

/**
 * 消息类型
 */
export function NOTICE_CURRENT_TYPE_FILTER(value) {
  let obj = COMMON_SELECT.NOTICE_CURRENT_TYPE.find(o => o.value === value);
  return obj ? obj.label : ''
}

/**
 * 线下销售状态
 */
 export function OFFLINE_STATUS_FILTER(value) {
  let obj = COMMON_SELECT.OFFLINE_STATUS.find(o => o.value == value);
  return obj ? obj.label : ''
}

/**
 * 线下销售类型
 */
 export function OFFLINE_TYPE_FILTER(value) {
  let obj = COMMON_SELECT.OFFLINE_TYPE.find(o => o.value === value);
  return obj ? obj.label : ''
}

/**
 * 优惠码状态
 */
 export function DISCODE_STATUS_FILTER(value) {
  let obj = COMMON_SELECT.DISCODE_STATUS.find(o => o.value === value);
  return obj ? obj.label : ''
}