import B from "../../@babel/runtime/helpers/esm/extends.mjs";
import J from "../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import S from "../../@babel/runtime/helpers/esm/regeneratorRuntime.mjs";
import Q from "../../@babel/runtime/helpers/esm/asyncToGenerator.mjs";
import L from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import I from "../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import X from "../../@babel/runtime/helpers/esm/classCallCheck.mjs";
import Z from "../../@babel/runtime/helpers/esm/createClass.mjs";
import a from "../../@babel/runtime/helpers/esm/assertThisInitialized.mjs";
import ee from "../../@babel/runtime/helpers/esm/inherits.mjs";
import re from "../../@babel/runtime/helpers/esm/createSuper.mjs";
import n from "../../@babel/runtime/helpers/esm/defineProperty.mjs";
import te from "../../rc-util/es/Children/toArray.mjs";
import ie from "../../rc-util/es/isEqual.mjs";
import { warningOnce as K } from "../../rc-util/es/warning.mjs";
import * as k from "react";
import Y, { HOOK_MARK as U } from "./FieldContext.mjs";
import ne from "./ListContext.mjs";
import { toArray as W } from "./utils/typeUtil.mjs";
import { validateRules as ae } from "./utils/validateUtil.mjs";
import { getNamePath as A, containsNamePath as H, defaultGetValueFromEvent as oe } from "./utils/valueUtil.mjs";
import le from "../../rc-util/es/utils/get.mjs";
var se = ["name"], c = [];
function q(M, T, p, g, e, v) {
  return typeof M == "function" ? M(T, p, "source" in v ? {
    source: v.source
  } : {}) : g !== e;
}
var D = /* @__PURE__ */ function(M) {
  ee(p, M);
  var T = re(p);
  function p(g) {
    var e;
    if (X(this, p), e = T.call(this, g), n(a(e), "state", {
      resetCount: 0
    }), n(a(e), "cancelRegisterFunc", null), n(a(e), "mounted", !1), n(a(e), "touched", !1), n(a(e), "dirty", !1), n(a(e), "validatePromise", void 0), n(a(e), "prevValidating", void 0), n(a(e), "errors", c), n(a(e), "warnings", c), n(a(e), "cancelRegister", function() {
      var t = e.props, r = t.preserve, i = t.isListField, s = t.name;
      e.cancelRegisterFunc && e.cancelRegisterFunc(i, r, A(s)), e.cancelRegisterFunc = null;
    }), n(a(e), "getNamePath", function() {
      var t = e.props, r = t.name, i = t.fieldContext, s = i.prefixName, o = s === void 0 ? [] : s;
      return r !== void 0 ? [].concat(I(o), I(r)) : [];
    }), n(a(e), "getRules", function() {
      var t = e.props, r = t.rules, i = r === void 0 ? [] : r, s = t.fieldContext;
      return i.map(function(o) {
        return typeof o == "function" ? o(s) : o;
      });
    }), n(a(e), "refresh", function() {
      e.mounted && e.setState(function(t) {
        var r = t.resetCount;
        return {
          resetCount: r + 1
        };
      });
    }), n(a(e), "metaCache", null), n(a(e), "triggerMetaEvent", function(t) {
      var r = e.props.onMetaChange;
      if (r) {
        var i = L(L({}, e.getMeta()), {}, {
          destroy: t
        });
        ie(e.metaCache, i) || r(i), e.metaCache = i;
      } else
        e.metaCache = null;
    }), n(a(e), "onStoreChange", function(t, r, i) {
      var s = e.props, o = s.shouldUpdate, w = s.dependencies, $ = w === void 0 ? [] : w, m = s.onReset, V = i.store, h = e.getNamePath(), F = e.getValue(t), E = e.getValue(V), _ = r && H(r, h);
      switch (i.type === "valueUpdate" && i.source === "external" && F !== E && (e.touched = !0, e.dirty = !0, e.validatePromise = null, e.errors = c, e.warnings = c, e.triggerMetaEvent()), i.type) {
        case "reset":
          if (!r || _) {
            e.touched = !1, e.dirty = !1, e.validatePromise = void 0, e.errors = c, e.warnings = c, e.triggerMetaEvent(), m == null || m(), e.refresh();
            return;
          }
          break;
        case "remove": {
          if (o) {
            e.reRender();
            return;
          }
          break;
        }
        case "setField": {
          var u = i.data;
          if (_) {
            "touched" in u && (e.touched = u.touched), "validating" in u && !("originRCField" in u) && (e.validatePromise = u.validating ? Promise.resolve([]) : null), "errors" in u && (e.errors = u.errors || c), "warnings" in u && (e.warnings = u.warnings || c), e.dirty = !0, e.triggerMetaEvent(), e.reRender();
            return;
          } else if ("value" in u && H(r, h, !0)) {
            e.reRender();
            return;
          }
          if (o && !h.length && q(o, t, V, F, E, i)) {
            e.reRender();
            return;
          }
          break;
        }
        case "dependenciesUpdate": {
          var R = $.map(A);
          if (R.some(function(N) {
            return H(i.relatedFields, N);
          })) {
            e.reRender();
            return;
          }
          break;
        }
        default:
          if (_ || (!$.length || h.length || o) && q(o, t, V, F, E, i)) {
            e.reRender();
            return;
          }
          break;
      }
      o === !0 && e.reRender();
    }), n(a(e), "validateRules", function(t) {
      var r = e.getNamePath(), i = e.getValue(), s = t || {}, o = s.triggerName, w = s.validateOnly, $ = w === void 0 ? !1 : w, m = Promise.resolve().then(/* @__PURE__ */ Q(/* @__PURE__ */ S().mark(function V() {
        var h, F, E, _, u, R, N;
        return S().wrap(function(f) {
          for (; ; )
            switch (f.prev = f.next) {
              case 0:
                if (e.mounted) {
                  f.next = 2;
                  break;
                }
                return f.abrupt("return", []);
              case 2:
                if (h = e.props, F = h.validateFirst, E = F === void 0 ? !1 : F, _ = h.messageVariables, u = h.validateDebounce, R = e.getRules(), o && (R = R.filter(function(d) {
                  return d;
                }).filter(function(d) {
                  var b = d.validateTrigger;
                  if (!b)
                    return !0;
                  var l = W(b);
                  return l.includes(o);
                })), !(u && o)) {
                  f.next = 10;
                  break;
                }
                return f.next = 8, new Promise(function(d) {
                  setTimeout(d, u);
                });
              case 8:
                if (e.validatePromise === m) {
                  f.next = 10;
                  break;
                }
                return f.abrupt("return", []);
              case 10:
                return N = ae(r, i, R, t, E, _), N.catch(function(d) {
                  return d;
                }).then(function() {
                  var d = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : c;
                  if (e.validatePromise === m) {
                    var b;
                    e.validatePromise = null;
                    var l = [], x = [];
                    (b = d.forEach) === null || b === void 0 || b.call(d, function(P) {
                      var O = P.rule.warningOnly, z = P.errors, G = z === void 0 ? c : z;
                      O ? x.push.apply(x, I(G)) : l.push.apply(l, I(G));
                    }), e.errors = l, e.warnings = x, e.triggerMetaEvent(), e.reRender();
                  }
                }), f.abrupt("return", N);
              case 13:
              case "end":
                return f.stop();
            }
        }, V);
      })));
      return $ || (e.validatePromise = m, e.dirty = !0, e.errors = c, e.warnings = c, e.triggerMetaEvent(), e.reRender()), m;
    }), n(a(e), "isFieldValidating", function() {
      return !!e.validatePromise;
    }), n(a(e), "isFieldTouched", function() {
      return e.touched;
    }), n(a(e), "isFieldDirty", function() {
      if (e.dirty || e.props.initialValue !== void 0)
        return !0;
      var t = e.props.fieldContext, r = t.getInternalHooks(U), i = r.getInitialValue;
      return i(e.getNamePath()) !== void 0;
    }), n(a(e), "getErrors", function() {
      return e.errors;
    }), n(a(e), "getWarnings", function() {
      return e.warnings;
    }), n(a(e), "isListField", function() {
      return e.props.isListField;
    }), n(a(e), "isList", function() {
      return e.props.isList;
    }), n(a(e), "isPreserve", function() {
      return e.props.preserve;
    }), n(a(e), "getMeta", function() {
      e.prevValidating = e.isFieldValidating();
      var t = {
        touched: e.isFieldTouched(),
        validating: e.prevValidating,
        errors: e.errors,
        warnings: e.warnings,
        name: e.getNamePath(),
        validated: e.validatePromise === null
      };
      return t;
    }), n(a(e), "getOnlyChild", function(t) {
      if (typeof t == "function") {
        var r = e.getMeta();
        return L(L({}, e.getOnlyChild(t(e.getControlled(), r, e.props.fieldContext))), {}, {
          isFunction: !0
        });
      }
      var i = te(t);
      return i.length !== 1 || !/* @__PURE__ */ k.isValidElement(i[0]) ? {
        child: i,
        isFunction: !1
      } : {
        child: i[0],
        isFunction: !1
      };
    }), n(a(e), "getValue", function(t) {
      var r = e.props.fieldContext.getFieldsValue, i = e.getNamePath();
      return le(t || r(!0), i);
    }), n(a(e), "getControlled", function() {
      var t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, r = e.props, i = r.trigger, s = r.validateTrigger, o = r.getValueFromEvent, w = r.normalize, $ = r.valuePropName, m = r.getValueProps, V = r.fieldContext, h = s !== void 0 ? s : V.validateTrigger, F = e.getNamePath(), E = V.getInternalHooks, _ = V.getFieldsValue, u = E(U), R = u.dispatch, N = e.getValue(), j = m || function(l) {
        return n({}, $, l);
      }, f = t[i], d = L(L({}, t), j(N));
      d[i] = function() {
        e.touched = !0, e.dirty = !0, e.triggerMetaEvent();
        for (var l, x = arguments.length, P = new Array(x), O = 0; O < x; O++)
          P[O] = arguments[O];
        o ? l = o.apply(void 0, P) : l = oe.apply(void 0, [$].concat(P)), w && (l = w(l, N, _(!0))), R({
          type: "updateValue",
          namePath: F,
          value: l
        }), f && f.apply(void 0, P);
      };
      var b = W(h || []);
      return b.forEach(function(l) {
        var x = d[l];
        d[l] = function() {
          x && x.apply(void 0, arguments);
          var P = e.props.rules;
          P && P.length && R({
            type: "validateField",
            namePath: F,
            triggerName: l
          });
        };
      }), d;
    }), g.fieldContext) {
      var v = g.fieldContext.getInternalHooks, C = v(U), y = C.initEntityValue;
      y(a(e));
    }
    return e;
  }
  return Z(p, [{
    key: "componentDidMount",
    value: function() {
      var e = this.props, v = e.shouldUpdate, C = e.fieldContext;
      if (this.mounted = !0, C) {
        var y = C.getInternalHooks, t = y(U), r = t.registerField;
        this.cancelRegisterFunc = r(this);
      }
      v === !0 && this.reRender();
    }
  }, {
    key: "componentWillUnmount",
    value: function() {
      this.cancelRegister(), this.triggerMetaEvent(!0), this.mounted = !1;
    }
  }, {
    key: "reRender",
    value: function() {
      this.mounted && this.forceUpdate();
    }
  }, {
    key: "render",
    value: function() {
      var e = this.state.resetCount, v = this.props.children, C = this.getOnlyChild(v), y = C.child, t = C.isFunction, r;
      return t ? r = y : /* @__PURE__ */ k.isValidElement(y) ? r = /* @__PURE__ */ k.cloneElement(y, this.getControlled(y.props)) : (K(!y, "`children` of Field is not validate ReactElement."), r = y), /* @__PURE__ */ k.createElement(k.Fragment, {
        key: e
      }, r);
    }
  }]), p;
}(k.Component);
n(D, "contextType", Y);
n(D, "defaultProps", {
  trigger: "onChange",
  valuePropName: "value"
});
function be(M) {
  var T = M.name, p = J(M, se), g = k.useContext(Y), e = k.useContext(ne), v = T !== void 0 ? A(T) : void 0, C = "keep";
  return p.isListField || (C = "_".concat((v || []).join("_"))), process.env.NODE_ENV !== "production" && p.preserve === !1 && p.isListField && v.length <= 1 && K(!1, "`preserve` should not apply on Form.List fields."), /* @__PURE__ */ k.createElement(D, B({
    key: C,
    name: v,
    isListField: !!e
  }, p, {
    fieldContext: g
  }));
}
export {
  be as default
};
