"use strict";
const React = require("react");
const get = require("../../../../../../../lodash@4.17.21/node_modules/lodash/get.js");
const is = require("../../_util/is.js");
const context = require("../context.js");
const warning = require("../../_util/warning.js");
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = { error };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var useWatch = function(field, form) {
  var formCtx = React.useContext(context.FormContext);
  var formInstance = form || formCtx.store;
  var _a = __read(React.useState(function() {
    var fieldValues = formInstance === null || formInstance === void 0 ? void 0 : formInstance.getFieldsValue([].concat(field));
    if (is.isString(field)) {
      return get(fieldValues, field);
    }
    return fieldValues;
  }), 2), value = _a[0], setValue = _a[1];
  var fieldRef = React.useRef(field);
  fieldRef.current = field;
  var valueRef = React.useRef(JSON.stringify(value));
  React.useEffect(function() {
    if (!formInstance) {
      warning(true, "formInstance is not available");
      return;
    }
    var registerWatcher = (formInstance === null || formInstance === void 0 ? void 0 : formInstance.getInnerMethods(true)).registerWatcher;
    var updateValue = function() {
      var field2 = fieldRef.current;
      var formValues = formInstance.getFieldsValue([].concat(field2));
      var newValue = formValues;
      if (!is.isArray(field2)) {
        newValue = get(formValues, field2);
      }
      var newValueString = JSON.stringify(newValue);
      if (valueRef.current !== newValueString) {
        setValue(newValue);
        valueRef.current = newValueString;
      }
    };
    updateValue();
    var cancelWatch = registerWatcher && registerWatcher(updateValue);
    return function() {
      cancelWatch && cancelWatch();
    };
  }, []);
  return value;
};
const useWatch$1 = useWatch;
module.exports = useWatch$1;
//# sourceMappingURL=useWatch.js.map
