import q from "../../@babel/runtime/helpers/esm/extends.mjs";
import m from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import z from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import B from "../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import * as t from "react";
import G from "./useForm.mjs";
import J, { HOOK_MARK as Q } from "./FieldContext.mjs";
import U from "./FormContext.mjs";
import { isSimilar as X } from "./utils/valueUtil.mjs";
import Y from "./ListContext.mjs";
var Z = ["name", "initialValues", "fields", "form", "preserve", "children", "component", "validateMessages", "validateTrigger", "onValuesChange", "onFieldsChange", "onFinish", "onFinishFailed"], ue = function(e, E) {
  var s = e.name, I = e.initialValues, l = e.fields, S = e.form, T = e.preserve, d = e.children, p = e.component, F = p === void 0 ? "form" : p, H = e.validateMessages, h = e.validateTrigger, C = h === void 0 ? "onChange" : h, y = e.onValuesChange, V = e.onFieldsChange, b = e.onFinish, A = e.onFinishFailed, c = B(e, Z), o = t.useContext(U), $ = G(S), j = z($, 1), r = j[0], a = r.getInternalHooks(Q), w = a.useSubscribe, D = a.setInitialValues, K = a.setCallbacks, N = a.setValidateMessages, O = a.setPreserve, k = a.destroyForm;
  t.useImperativeHandle(E, function() {
    return r;
  }), t.useEffect(function() {
    return o.registerForm(s, r), function() {
      o.unregisterForm(s);
    };
  }, [o, r, s]), N(m(m({}, o.validateMessages), H)), K({
    onValuesChange: y,
    onFieldsChange: function(n) {
      if (o.triggerFormChange(s, n), V) {
        for (var i = arguments.length, M = new Array(i > 1 ? i - 1 : 0), u = 1; u < i; u++)
          M[u - 1] = arguments[u];
        V.apply(void 0, [n].concat(M));
      }
    },
    onFinish: function(n) {
      o.triggerFormFinish(s, n), b && b(n);
    },
    onFinishFailed: A
  }), O(T);
  var f = t.useRef(null);
  D(I, !f.current), f.current || (f.current = !0), t.useEffect(
    function() {
      return k;
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    []
  );
  var g, R = typeof d == "function";
  if (R) {
    var L = r.getFieldsValue(!0);
    g = d(L, r);
  } else
    g = d;
  w(!R);
  var x = t.useRef();
  t.useEffect(function() {
    X(x.current || [], l || []) || r.setFields(l || []), x.current = l;
  }, [l, r]);
  var W = t.useMemo(function() {
    return m(m({}, r), {}, {
      validateTrigger: C
    });
  }, [r, C]), P = /* @__PURE__ */ t.createElement(Y.Provider, {
    value: null
  }, /* @__PURE__ */ t.createElement(J.Provider, {
    value: W
  }, g));
  return F === !1 ? P : /* @__PURE__ */ t.createElement(F, q({}, c, {
    onSubmit: function(n) {
      n.preventDefault(), n.stopPropagation(), r.submit();
    },
    onReset: function(n) {
      var i;
      n.preventDefault(), r.resetFields(), (i = c.onReset) === null || i === void 0 || i.call(c, n);
    }
  }), P);
};
export {
  ue as default
};
