const parseRule = (fieldType, { label, isRequired, ruleType }) => {
  const ruleList = [];
  if (isRequired) {
    switch (fieldType) {
      case "upload":
        ruleList.push({ required: true, message: `请选择要上传的文件` });
        break;
      default:
        ruleList.push({ required: true, message: `请填写${label}` });
    }
  }
  const errorMessage = `请填写正确的${label}`;
  const validateEmpty = false;
  switch (ruleType) {
    case "email":
      var pattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
      ruleList.push({
        pattern,
        message: errorMessage,
        validateEmpty
      });
      break;
    case "phone":
      var pattern = /^((\+?86)|(\(\+86\)))?(1[3-9]\d{9})$/;
      ruleList.push({
        pattern,
        message: errorMessage,
        validateEmpty
      });
      break;
    case "mobile":
      var pattern = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
      ruleList.push({
        pattern,
        message: errorMessage,
        validateEmpty
      });
      break;
    case "mobilephone":
      let validatorMobilephone = function(value) {
        var isPhone = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
        var isMob = /^((\+?86)|(\(\+86\)))?(1[3-9]\d{9})$/;
        if (isMob.test(value) || isPhone.test(value)) {
          return true;
        } else {
          return false;
        }
      };
      ruleList.push({
        validator: validatorMobilephone,
        message: errorMessage,
        validateEmpty
      });
      break;
    case "idNo":
      let validatorIdNo = function(value) {
        const reg15 = /^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$/;
        const reg18 = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
        if (reg15.test(value) || reg18.test(value)) {
          return true;
        } else {
          return false;
        }
      };
      ruleList.push({
        validator: validatorIdNo,
        message: errorMessage,
        validateEmpty
      });
      break;
    case "usci":
      var pattern = /^([0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}|[1-9]\d{14})$/;
      ruleList.push({
        pattern,
        message: errorMessage,
        validateEmpty
      });
      break;
    case "postcode":
      var pattern = /^[0-9]{6}$/;
      ruleList.push({
        pattern,
        message: errorMessage,
        validateEmpty
      });
      break;
    case "fax":
      var pattern = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
      ruleList.push({
        pattern,
        message: errorMessage,
        validateEmpty
      });
      break;
    case "bankcard":
      var pattern = /^[1-9]\d{7,29}$/;
      ruleList.push({
        pattern,
        message: errorMessage,
        validateEmpty
      });
      break;
  }
  return ruleList;
};
export {
  parseRule
};
