// 效验身份证号码
export const isValidIdCard18 = (idCard) => {
  // 身份证号码正则表达式（18位）
  var regex = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))(([012]\d)|3[01])\d{3}(\d|X|x)$/

  // 校验长度，类型
  if (!regex.test(idCard)) {
    return false
  }

  // 提取出生年月日，并验证是否为有效日期
  var year = idCard.substring(6, 10)
  var month = idCard.substring(10, 12)
  var day = idCard.substring(12, 14)
  var date = new Date(year, month - 1, day)

  if (date.getFullYear() !== parseInt(year, 10) ||
    date.getMonth() !== parseInt(month, 10) - 1 ||
    date.getDate() !== parseInt(day, 10)) {
    return false
  }

  // 校验码部分
  var idCardBase = idCard.substring(0, 17)
  var weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  var checkCodes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
  var sum = 0
  for (var i = 0; i < idCardBase.length; i++) {
    sum += idCardBase[i] * weights[i]
  }
  var index = sum % 11
  var last = checkCodes[index]

  return last.toUpperCase() === idCard.substring(17).toUpperCase()
}

// 身份证号码转换*号
export const  maskNumber = (numberStr) => {
  if (numberStr.length < 2) {
    return numberStr
  }
  
  // 提取首尾字符
  const firstChar = numberStr[0]
  const lastChar = numberStr[numberStr.length - 1]

  const middleLength = numberStr.length - 2
  const middleStars = '*'.repeat(middleLength)
  
  return firstChar + middleStars + lastChar
}