import { ElMessage } from 'element-plus'
import { reg } from 'lz-utils-lib'

/**
 * @name 判断当前key是否为空值
 * @param value
 * @returns boolean true是，false否
 */
export const formatEmptyKeys = (obj: any) => {
  for (const key in obj) {
    if (
      !Object.prototype.hasOwnProperty.call(obj, key) ||
      obj[key] === null ||
      obj[key] === undefined ||
      obj[key] === ''
    ) {
      return true
    }
  }
  return false
}

/**
 * @name 验证对象中只能存在一个key
 * @param value
 * @returns boolean true是，false否
 */
export const formatValueObject = (obj: { [x: string]: string }) => {
  let hasValue = false
  for (const value of Object.values(obj)) {
    if (!['', undefined].includes(value)) {
      if (hasValue) {
        return false // 发现多个有值的属性
      }
      hasValue = true
    }
  }
  return hasValue // 确保只有一个有值的属性
}


/**
 * @name 验证对象中是否有key为空
 * @param key
 * @returns boolean true是空，false否
 */
export const formatObjHasEmptyKey = (obj: any) => {
  for (const key in obj) {
    if (obj[key] === '') {
      delete obj[key]
    }
  }
  return !Object.keys(obj).length || Object.keys(obj).some(key => obj[key] === '')
}

/**
 * 验证参数中不能为空
 */
interface SearchParams {
  mobile?: string;
  idNumber?: string;
  uuid?: string;
  type?: number;
}
export const validateSearchParams = (params: SearchParams): boolean => {
  if (!formatValueObject(params as { [x: string]: string })) {
    ElMessage.warning('请输入一项查询项')
    return false
  }

  if (params.mobile && !reg.isMobile(params.mobile)) {
    ElMessage.warning('请输入正确的手机号')
    return false
  }

  if (params.idNumber && !/^[1-9]\d{5}(18|19|20|21|22)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/.test(params.idNumber)) {
    ElMessage.warning('请输入正确的身份证号')
    return false
  }

  if (params.uuid && !/^\d+$/.test(params.uuid)) {
    ElMessage.warning('请输入正确的客户号')
    return false
  }

  return true
}