text: import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
  
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  
  import * as React from 'react';
  import useForm from './useForm';
  import FieldContext, { HOOK_MARK } from './FieldContext';
  import FormContext from './FormContext';
  import { isSimilar } from './utils/valueUtil';
  
  var Form = function Form(_ref, ref) {
    var name = _ref.name,
        initialValues = _ref.initialValues,
        fields = _ref.fields,
        form = _ref.form,
        children = _ref.children,
        _ref$component = _ref.component,
        Component = _ref$component === void 0 ? 'form' : _ref$component,
        validateMessages = _ref.validateMessages,
        _ref$validateTrigger = _ref.validateTrigger,
        validateTrigger = _ref$validateTrigger === void 0 ? 'onChange' : _ref$validateTrigger,
        onValuesChange = _ref.onValuesChange,
        _onFieldsChange = _ref.onFieldsChange,
        _onFinish = _ref.onFinish,
        onFinishFailed = _ref.onFinishFailed,
        restProps = _objectWithoutProperties(_ref, ["name", "initialValues", "fields", "form", "children", "component", "validateMessages", "validateTrigger", "onValuesChange", "onFieldsChange", "onFinish", "onFinishFailed"]);
  
    var formContext = React.useContext(FormContext); // We customize handle event since Context will makes all the consumer re-render:
    // https://reactjs.org/docs/context.html#contextprovider
  
    var _useForm = useForm(form),
        _useForm2 = _slicedToArray(_useForm, 1),
        formInstance = _useForm2[0];
  
    var _formInstance$getInte = formInstance.getInternalHooks(HOOK_MARK),
        useSubscribe = _formInstance$getInte.useSubscribe,
        setInitialValues = _formInstance$getInte.setInitialValues,
        setCallbacks = _formInstance$getInte.setCallbacks,
        setValidateMessages = _formInstance$getInte.setValidateMessages; // Pass ref with form instance
  
  
    React.useImperativeHandle(ref, function () {
      return formInstance;
    }); // Register form into Context
  
    React.useEffect(function () {
      formContext.registerForm(name, formInstance);
      return function () {
        formContext.unregisterForm(name);
      };
    }, [formContext, formInstance, name]); // Pass props to store
  
    setValidateMessages(_objectSpread({}, formContext.validateMessages, {}, validateMessages));
    setCallbacks({
      onValuesChange: onValuesChange,
      onFieldsChange: function onFieldsChange(changedFields) {
        formContext.triggerFormChange(name, changedFields);
  
        if (_onFieldsChange) {
          for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
            rest[_key - 1] = arguments[_key];
          }
  
          _onFieldsChange.apply(void 0, [changedFields].concat(rest));
        }
      },
      onFinish: function onFinish(values) {
        formContext.triggerFormFinish(name, values);
  
        if (_onFinish) {
          _onFinish(values);
        }
      },
      onFinishFailed: onFinishFailed
    }); // Set initial value, init store value when first mount
  
    var mountRef = React.useRef(null);
    setInitialValues(initialValues, !mountRef.current);
  
    if (!mountRef.current) {
      mountRef.current = true;
    } // Prepare children by `children` type
  
  
    var childrenNode = children;
    var childrenRenderProps = typeof children === 'function';
  
    if (childrenRenderProps) {
      var values = formInstance.getFieldsValue(true);
      childrenNode = children(values, formInstance);
    } // Not use subscribe when using render props
  
  
    useSubscribe(!childrenRenderProps); // Listen if fields provided. We use ref to save prev data here to avoid additional render
  
    var prevFieldsRef = React.useRef();
    React.useEffect(function () {
      if (!isSimilar(prevFieldsRef.current || [], fields || [])) {
        formInstance.setFields(fields || []);
      }
  
      prevFieldsRef.current = fields;
    }, [fields, formInstance]);
    var formContextValue = React.useMemo(function () {
      return _objectSpread({}, formInstance, {
        validateTrigger: validateTrigger
      });
    }, [formInstance, validateTrigger]);
    var wrapperNode = React.createElement(FieldContext.Provider, {
      value: formContextValue
    }, childrenNode);
  
    if (Component === false) {
      return wrapperNode;
    }
  
    return React.createElement(Component, Object.assign({}, restProps, {
      onSubmit: function onSubmit(event) {
        event.preventDefault();
        event.stopPropagation();
        formInstance.submit();
      }
    }), wrapperNode);
  };
  
  export default Form;