"use strict";
const common_vendor = require("../../common/vendor.js");
const __default__ = {
  styleIsolation: "shared"
};
const _sfc_main = /* @__PURE__ */ Object.assign(__default__, {
  __name: "index",
  props: {
    field: {
      type: String,
      required: false
    },
    rules: {
      type: Array,
      default: () => []
    }
  },
  setup(__props) {
    const props = __props;
    const isReadonly = common_vendor.computed(() => !props.field);
    const validateField = common_vendor.inject("validateField", () => {
    });
    const errors = common_vendor.inject("errors", common_vendor.ref({}));
    const formData = common_vendor.inject("formData", common_vendor.ref({}));
    const getFieldConfig = common_vendor.inject("getFieldConfig", () => ({}));
    const fieldConfig = common_vendor.computed(() => {
      if (isReadonly.value || !props.field)
        return {};
      return getFieldConfig(props.field) || {};
    });
    const finalRules = common_vendor.computed(() => {
      if (isReadonly.value)
        return [];
      return props.rules.length > 0 ? props.rules : fieldConfig.value.rules || [];
    });
    const hasError = common_vendor.computed(() => {
      return props.field && errors.value[props.field] && errors.value[props.field].length > 0;
    });
    const errorMessage = common_vendor.computed(() => {
      return hasError.value ? errors.value[props.field][0] : "";
    });
    const isRequired = common_vendor.computed(() => {
      return finalRules.value.some((rule) => rule.required);
    });
    const getCurrentFieldValue = () => {
      if (!formData.value || !props.field)
        return void 0;
      const fieldPath = props.field.split(".");
      let value = formData.value;
      for (const key of fieldPath) {
        value = value == null ? void 0 : value[key];
      }
      return value;
    };
    const showRequiredLine = common_vendor.computed(() => {
      if (!isRequired.value)
        return false;
      if (hasError.value)
        return false;
      const fieldValue = getCurrentFieldValue();
      if (fieldValue !== void 0 && fieldValue !== null && fieldValue !== "") {
        return false;
      }
      return true;
    });
    const handleInput = () => {
    };
    const handleBlur = () => {
    };
    if (!isReadonly.value && props.field) {
      common_vendor.provide("onInput", handleInput);
      common_vendor.provide("onBlur", handleBlur);
      common_vendor.provide("fieldName", props.field);
    }
    common_vendor.watch(() => getCurrentFieldValue(), (newValue, oldValue) => {
      if (isReadonly.value || !props.field)
        return;
      if (oldValue !== void 0 && newValue !== oldValue) {
        if (finalRules.value.length > 0) {
          validateField(props.field, newValue, finalRules.value);
        }
      }
    }, { flush: "post" });
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: !isReadonly.value && showRequiredLine.value
      }, !isReadonly.value && showRequiredLine.value ? {} : {}, {
        b: !isReadonly.value && hasError.value
      }, !isReadonly.value && hasError.value ? {} : {}, {
        c: isReadonly.value
      }, isReadonly.value ? {} : {}, {
        d: !isReadonly.value && hasError.value
      }, !isReadonly.value && hasError.value ? {
        e: common_vendor.t(errorMessage.value)
      } : {}, {
        f: common_vendor.gei(_ctx, "")
      });
    };
  }
});
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-711d776c"]]);
wx.createComponent(Component);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/components/Td/index.js.map
