"use strict";
const common_vendor = require("../common/vendor.js");
function useFormValidation(fieldConfigs = {}) {
  const errors = common_vendor.ref({});
  const configs = common_vendor.ref(fieldConfigs);
  const getFieldConfig = (fieldName) => {
    return configs.value[fieldName] || {};
  };
  const setFieldConfig = (fieldName, config) => {
    configs.value[fieldName] = config;
  };
  const setFieldConfigs = (newConfigs) => {
    configs.value = { ...configs.value, ...newConfigs };
  };
  common_vendor.provide("errors", errors);
  common_vendor.provide("getFieldConfig", getFieldConfig);
  const validateField = (fieldName, value, rules) => {
    if (!rules || !Array.isArray(rules)) {
      return true;
    }
    const fieldErrors = [];
    for (const rule of rules) {
      if (rule.required && (!value || value === "")) {
        fieldErrors.push(rule.message || "此字段为必填项");
        break;
      }
      if (!value || value === "") {
        continue;
      }
      if (rule.min && value.length < rule.min) {
        fieldErrors.push(rule.message || `最少输入${rule.min}个字符`);
        break;
      }
      if (rule.max && value.length > rule.max) {
        fieldErrors.push(rule.message || `最多输入${rule.max}个字符`);
        break;
      }
      if (rule.pattern && !rule.pattern.test(value)) {
        fieldErrors.push(rule.message || "格式不正确");
        break;
      }
      if (rule.validator && typeof rule.validator === "function") {
        const result = rule.validator(value);
        if (result !== true) {
          fieldErrors.push(result || rule.message || "校验失败");
          break;
        }
      }
    }
    if (fieldErrors.length > 0) {
      errors.value[fieldName] = fieldErrors;
      return false;
    } else {
      delete errors.value[fieldName];
      return true;
    }
  };
  common_vendor.provide("validateField", validateField);
  const getNestedFieldValue = (formData, fieldPath) => {
    const pathArray = fieldPath.split(".");
    let value = formData;
    for (const key of pathArray) {
      value = value == null ? void 0 : value[key];
    }
    return value;
  };
  const validateForm = (formData, formRules) => {
    common_vendor.index.__f__("log", "at composables/useFormValidation.js:101", "validateForm开始:", { formData, formRules });
    let isValid = true;
    for (const fieldName in formRules) {
      const fieldValue = getNestedFieldValue(formData, fieldName);
      const fieldRules = formRules[fieldName].rules || formRules[fieldName];
      common_vendor.index.__f__("log", "at composables/useFormValidation.js:109", `校验字段 ${fieldName}:`, { fieldValue, fieldRules });
      const fieldValid = validateField(fieldName, fieldValue, fieldRules);
      common_vendor.index.__f__("log", "at composables/useFormValidation.js:111", `字段 ${fieldName} 校验结果:`, fieldValid);
      if (!fieldValid) {
        isValid = false;
      }
    }
    common_vendor.index.__f__("log", "at composables/useFormValidation.js:118", "validateForm结束:", isValid);
    return isValid;
  };
  const clearErrors = () => {
    errors.value = {};
  };
  const clearFieldError = (fieldName) => {
    delete errors.value[fieldName];
  };
  return {
    errors,
    validateField,
    validateForm,
    clearErrors,
    clearFieldError,
    getFieldConfig,
    setFieldConfig,
    setFieldConfigs,
    configs
  };
}
exports.useFormValidation = useFormValidation;
//# sourceMappingURL=../../.sourcemap/mp-weixin/composables/useFormValidation.js.map
