import { checkFilterMatch } from "../../../utils/parseFilterConfig.mjs";
const VALIDATION_RULES = {
  // 身份证
  idCard: {
    pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
    message: "请输入正确的身份证号码"
  },
  // 统一社会信用代码
  creditCode: {
    pattern: /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/,
    message: "请输入正确的统一社会信用代码"
  },
  // 邮编
  zipCode: {
    pattern: /^[1-9]\d{5}$/,
    message: "请输入正确的邮政编码"
  },
  // 传真
  fax: {
    pattern: /^\d{3,4}-?\d{7,8}$/,
    message: "请输入正确的传真号码"
  },
  // 银行卡号
  bankCard: {
    pattern: /^[1-9]\d{15,18}$/,
    message: "请输入正确的银行卡号"
  },
  // 邮箱
  email: {
    pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
    message: "请输入正确的邮箱地址"
  },
  // 手机
  mobile: {
    pattern: /^1[3-9]\d{9}$/,
    message: "请输入正确的手机号码"
  },
  // 座机
  phone: {
    pattern: /^0\d{2,3}-?\d{7,8}$/,
    message: "请输入正确的座机号码"
  },
  // 手机+座机
  mobilePhone: {
    pattern: /^(1[3-9]\d{9})|(0\d{2,3}-?\d{7,8})$/,
    message: "请输入正确的联系电话"
  }
};
function generateFieldRules(component, formData, fields) {
  const rules = [];
  if (!component.fieldName) {
    return rules;
  }
  if (component.required) {
    rules.push({
      required: true,
      message: component.requiredMessage || `请输入${component.label}`,
      trigger: "blur"
    });
  }
  if (component.validationType && component.validationType !== "none") {
    if (component.validationType === "regex" && component.validationPattern) {
      try {
        const validator = (_rule, _value, callback) => {
          try {
            const isMatch = checkFilterMatch(component.validationPattern, formData.value, fields);
            if (!isMatch) {
              callback(new Error(component.validationMessage || "校验失败"));
            } else {
              callback();
            }
          } catch (error) {
            console.error("Validation function error:", error);
            callback(new Error(component.validationMessage || "校验函数执行出错"));
          }
        };
        rules.push({
          validator,
          trigger: "change"
        });
      } catch (e) {
        console.error("Invalid validation pattern:", component.validationPattern, e);
      }
    } else if (component.validationType === "custom" && component.customValidator) {
      try {
        const validator = createCustomValidator(component.customValidator);
        if (validator) {
          rules.push({
            validator,
            trigger: "blur"
          });
        }
      } catch (e) {
        console.error("Invalid custom validator:", e);
      }
    } else if (VALIDATION_RULES[component.validationType]) {
      const rule = VALIDATION_RULES[component.validationType];
      rules.push({
        pattern: rule.pattern,
        message: component.validationMessage || rule.message,
        trigger: "blur"
      });
    }
  }
  return rules;
}
function createCustomValidator(validatorStr) {
  if (!validatorStr || typeof validatorStr !== "string") {
    return null;
  }
  try {
    const validator = new Function("return " + validatorStr)();
    if (typeof validator === "function") {
      return (rule, value, callback) => {
        try {
          validator(rule, value, callback);
        } catch (error) {
          callback(new Error("校验函数执行出错: " + error.message));
        }
      };
    }
  } catch (e) {
    console.error("Failed to create validator from string:", e);
  }
  return null;
}
export {
  generateFieldRules
};
