/**
 * 判断url是否是http或https
 * @param {string} path
 * @returns {Boolean}
 */
export function isHttp(url) {
  return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
}

/**
 * 判断path是否为外链
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  if (!email) return false
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email.trim())
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

/**
 * Visa 卡号校验
 */
export function validVisaCardNo(arg) {
  return /^(?:4[0-9]{12}(?:[0-9]{3})?)$/.test(arg)
}

/**
 * MasterCard 卡号校验
 */
export function validMasterCardNo(arg) {
  return /^(?:5[1-5][0-9]{14})$/.test(arg)
}

// 英名验证
export function enName(rule, value, callback) {
  if (!value) {
    callback(new Error('Recipient name must be in English'))
  } else {
    const reg = /^[a-zA-Z0-9\s`~!@#$%^&*()_+-={}\[\]:;"'<>,.?/|\\]+$/ // 验证有空格的英文名
    if (value.length < 100 && reg.test(value)) {
      callback()
    } else {
      return callback(new Error('Please enter an English name'))
    }
  }
}
// 英名验证
export function enName2(rule, value, callback) {
  if (!value) {
    callback()
  } else {
    const reg = /^[a-zA-Z0-9\s`~!@#$%^&*()_+-={}\[\]:;"'<>,.?/|\\]+$/ // 验证有空格的英文名
    if (value.length < 100 && reg.test(value)) {
      callback()
    } else {
      return callback(new Error('Please enter an English name'))
    }
  }
}

// 英名地址验证
export function enAddress(rule, value, callback) {
  if (!value) {
    callback(new Error('Address must be in English'))
  } else {
    //const reg = /^[a-zA-Z]+$/ // 验证没有空格的英文
    //const reg = /^[a-zA-Z0-9][A-Za-z0-9\s-]*[a-zA-Z0-9]*$/ // 验证有空格的英文
    //const reg = /^[\x20-\x7E]+$/; //ASCII码验证
    const reg = /^[a-zA-Z0-9\s`~!@#$%^&*()_+-={}\[\]:;"'<>,.?/|\\]+$/
    if (value.length < 100 && reg.test(value)) {
      callback()
    } else {
      return callback(new Error('Please enter an English address'))
    }
  }
}


// 英名地址验证
export function enAddress2(rule, value, callback) {
  if (!value) {
    callback()
  } else {
    const reg = /^[a-zA-Z0-9\s`~!@#$%^&*()_+-={}\[\]:;"'<>,.?/|\\]+$/
    if (value.length < 100 && reg.test(value)) {
      callback()
    } else {
      return callback(new Error('Please enter an English address'))
    }
  }
}

// 联系方式验证
export function validContact(rule, value, callback) {
  if (!value) {
    callback(new Error('Contact information cannot contain Chinese characters'))
  } else {
    const reg = /^[a-zA-Z0-9\s`~!@#$%^&*()_+-={}\[\]:;"'<>,.?/|\\]+$/
    if (value.length < 100 && reg.test(value)) {
      callback()
    } else {
      return callback(new Error('Please enter contact information without Chinese characters'))
    }
  }
}

// 联系方式验证
export function validContact2(rule, value, callback) {
  if (!value) {
    callback()
  } else {
    const reg = /^[a-zA-Z0-9\s`~!@#$%^&*()_+-={}\[\]:;"'<>,.?/|\\]+$/
    if (value.length < 100 && reg.test(value)) {
      callback()
    } else {
      return callback(new Error('Please enter contact information without Chinese characters'))
    }
  }
}

// 邮编效验
export function zipCheck(rule, value, callback) {
  if (!value) return callback(new Error("ZIP code is required"));
  if (value.length < 5 || value.length > 10)
    return callback(new Error("ZIP code length must be 5 to 10 characters"))
  if (!/^[\d-]+$/.test(value))
    return callback(new Error("Invalid ZIP code, numbers only"))
  callback()
}

// 邮编效验
export function zipCheck2(rule, value, callback) {
  if (!value) return callback(); 
  if (value.length < 5 || value.length > 10)
    return callback(new Error("ZIP code length must be 5 to 10 characters"))
  if (!/^[\d-]+$/.test(value))
    return callback(new Error("Invalid ZIP code, numbers only"))
  callback()
}
