"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Watcher = exports.FormSubscribe = void 0;
var _react = _interopRequireWildcard(require("react"));
var _rcFieldForm = require("rc-field-form");
var _ahooks = require("ahooks");
var _useIsomorphicUpdateLayoutEffect = require("../../utils/use-isomorphic-update-layout-effect");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const FormSubscribe = props => {
  const update = (0, _ahooks.useUpdate)();
  const form = (0, _react.useContext)(_rcFieldForm.FieldContext);
  const value = form.getFieldsValue(props.to);
  // Memo to avoid useless render
  const childNode = _react.default.useMemo(() => props.children(value, form), [JSON.stringify(value), props.children]);
  return _react.default.createElement(_react.default.Fragment, null, childNode, props.to.map(namePath => _react.default.createElement(Watcher, {
    key: namePath.toString(),
    form: form,
    namePath: namePath,
    onChange: update
  })));
};
exports.FormSubscribe = FormSubscribe;
const Watcher = exports.Watcher = (0, _react.memo)(props => {
  const value = (0, _rcFieldForm.useWatch)(props.namePath, props.form);
  (0, _useIsomorphicUpdateLayoutEffect.useIsomorphicUpdateLayoutEffect)(() => {
    props.onChange();
  }, [value]);
  return null;
});