import Strings from '@/common/utils/strings.ts'

export default {
  mobile: (value: string) => {
    if (Strings.isBlank(value)) {
      return ''
    }

    if (!/^1[3-9]\d{9}$/.test(value)) {
      return value
    }
    return value.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
  },
  idcard: (value: string) => {
    if (Strings.isBlank(value)) {
      return ''
    }
    if (value.length !== 18) {
      return value
    }
    return value.replace(/(\d{6})\d{8}(\d{4})/, '$1********$2')
  },
  email: (value: string) => {
    if (Strings.isBlank(value)) {
      return ''
    }
    const emailRegex = /^(\w)\w*@(\w+\.\w+)$/
    if (!emailRegex.test(value)) {
      return value
    }
    return value.replace(emailRegex, '$1****@$2')
  },
  bankCard: (value: string) => {
    if (Strings.isBlank(value)) {
      return ''
    }
    if (value.length < 10) {
      return value
    }
    return value.replace(/(\d{6})\d+(\d{4})$/, '$1****$2')
  },
  name: (value: string) => {
    if (Strings.isBlank(value)) {
      return ''
    }
    if (value.length === 1) {
      return value
    } else if (value.length === 2) {
      return value.replace(/(.)./, '$1*')
    } else {
      return value.replace(/(.).*(.)/, '$1*$2')
    }
  },
}
