import H from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import { warningOnce as E } from "../../rc-util/es/warning.mjs";
import { useRef as l, useState as M, useMemo as A, useContext as D, useEffect as K } from "react";
import L, { HOOK_MARK as T } from "./FieldContext.mjs";
import { isFormInstance as j } from "./utils/typeUtil.mjs";
import { getNamePath as q } from "./utils/valueUtil.mjs";
import J from "../../rc-util/es/utils/get.mjs";
function x(r) {
  try {
    return JSON.stringify(r);
  } catch {
    return Math.random();
  }
}
var $ = process.env.NODE_ENV !== "production" ? function(r) {
  var e = r.join("__RC_FIELD_FORM_SPLIT__"), t = l(e);
  E(t.current === e, "`useWatch` is not support dynamic `namePath`. Please provide static instead.");
} : function() {
};
function X() {
  for (var r = arguments.length, e = new Array(r), t = 0; t < r; t++)
    e[t] = arguments[t];
  var c = e[0], p = e[1], s = p === void 0 ? {} : p, g = j(s) ? {
    form: s
  } : s, h = g.form, F = M(), d = H(F, 2), a = d[0], _ = d[1], V = A(function() {
    return x(a);
  }, [a]), f = l(V);
  f.current = V;
  var N = D(L), o = h || N, i = o && o._init;
  process.env.NODE_ENV !== "production" && E(e.length === 2 ? h ? i : !0 : i, "useWatch requires a form instance since it can not auto detect from context.");
  var m = q(c), S = l(m);
  return S.current = m, $(m), K(
    function() {
      if (i) {
        var W = o.getFieldsValue, P = o.getInternalHooks, y = P(T), w = y.registerWatch, R = function(v, u) {
          var n = g.preserve ? u : v;
          return typeof c == "function" ? c(n) : J(n, S.current);
        }, C = w(function(O, v) {
          var u = R(O, v), n = x(u);
          f.current !== n && (f.current = n, _(u));
        }), I = R(W(), W(!0));
        return a !== I && _(I), C;
      }
    },
    // We do not need re-register since namePath content is the same
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [i]
  ), a;
}
export {
  X as default,
  x as stringify
};
