var _excluded = ["prefixCls", "className", "size", "form", "dataBinding", "colon", "labelAlign", "labelCol", "wrapperCol", "hideRequiredMark", "layout", "scrollToFirstError", "requiredMark", "onFinishFailed", "onValuesChange"];

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

// @ts-nocheck
import * as React from 'react';
import { useMemo, useEffect } from 'react';
import classNames from 'classnames';
import FieldForm, { List } from 'rc-field-form';
import { ConfigContext } from "antd/es/config-provider/context";
import { FormContext } from './context';
import { cascadeHandler } from './util';
import useForm from '../hooks/useForm';
import SizeContext, { SizeContextProvider } from "antd/es/config-provider/SizeContext";
import LocaleReceiver from 'antd/es/locale-provider/LocaleReceiver';
import defaultLocale from "antd/es/locale/zh_CN"; // @ts-ignore

var InternalForm = function InternalForm(props, ref) {
  var _classNames;

  var contextSize = React.useContext(SizeContext); // @ts-ignore

  var _React$useContext = React.useContext(ConfigContext),
      getPrefixCls = _React$useContext.getPrefixCls,
      direction = _React$useContext.direction,
      contextForm = _React$useContext.form;

  var name = props.name;

  var customizePrefixCls = props.prefixCls,
      _props$className = props.className,
      className = _props$className === void 0 ? '' : _props$className,
      _props$size = props.size,
      size = _props$size === void 0 ? contextSize : _props$size,
      form = props.form,
      _props$dataBinding = props.dataBinding,
      dataBinding = _props$dataBinding === void 0 ? false : _props$dataBinding,
      colon = props.colon,
      labelAlign = props.labelAlign,
      labelCol = props.labelCol,
      wrapperCol = props.wrapperCol,
      hideRequiredMark = props.hideRequiredMark,
      _props$layout = props.layout,
      layout = _props$layout === void 0 ? 'horizontal' : _props$layout,
      scrollToFirstError = props.scrollToFirstError,
      requiredMark = props.requiredMark,
      onFinishFailed = props.onFinishFailed,
      onValuesChange = props.onValuesChange,
      restFormProps = _objectWithoutProperties(props, _excluded);

  var mergedRequiredMark = useMemo(function () {
    if (requiredMark !== undefined) {
      return requiredMark;
    }

    if (contextForm && contextForm.requiredMark !== undefined) {
      return contextForm.requiredMark;
    }

    if (hideRequiredMark) {
      return false;
    }

    return true;
  }, [hideRequiredMark, requiredMark, contextForm]);
  var prefixCls = getPrefixCls('form', customizePrefixCls);
  var simplePrefixCls = getPrefixCls('simple-form', customizePrefixCls);
  var formClassName = classNames(prefixCls, simplePrefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(layout), true), _defineProperty(_classNames, "".concat(prefixCls, "-hide-required-mark"), mergedRequiredMark === false), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(size), size), _classNames), className);

  var _useForm = useForm(form),
      _useForm2 = _slicedToArray(_useForm, 1),
      wrapForm = _useForm2[0];

  var __INTERNAL__ = wrapForm.__INTERNAL__,
      setFieldValue = wrapForm.setFieldValue;
  var allDependency = __INTERNAL__.allDependency,
      changeHandler = __INTERNAL__.changeHandler,
      itemRef = __INTERNAL__.itemRef;
  __INTERNAL__.name = name;
  var formContextValue = useMemo(function () {
    return {
      name: name,
      allDependency: allDependency,
      changeHandler: changeHandler,
      setFieldValue: setFieldValue,
      dataBinding: dataBinding,
      labelAlign: labelAlign,
      labelCol: labelCol,
      wrapperCol: wrapperCol,
      vertical: layout === 'vertical',
      colon: colon,
      requiredMark: mergedRequiredMark,
      itemRef: itemRef
    };
  }, [name, allDependency, changeHandler, setFieldValue, dataBinding, labelAlign, labelCol, wrapperCol, layout, colon, mergedRequiredMark]);
  React.useImperativeHandle(ref, function () {
    return wrapForm;
  });

  var onInternalFinishFailed = function onInternalFinishFailed(errorInfo) {
    if (onFinishFailed) {
      onFinishFailed(errorInfo);
    }

    if (scrollToFirstError && errorInfo.errorFields.length) {
      wrapForm.scrollToField(errorInfo.errorFields[0].name);
    }
  };

  var onInternalValuesChange = function onInternalValuesChange(changedValues, allValues) {
    if (onValuesChange) {
      onValuesChange(changedValues, allValues);
    }

    if (Object.keys(allDependency).length) {
      var currentItem = Object.keys(changedValues)[0];
      cascadeHandler(wrapForm, allValues, allDependency, currentItem);
    }
  };

  useEffect(function () {
    if (restFormProps.initialValues && Object.keys(allDependency).length) {
      var allValues = wrapForm.getFieldsValue();
      cascadeHandler(wrapForm, allValues, allDependency);
    }
  }, []);
  return /*#__PURE__*/React.createElement(LocaleReceiver, {
    componentName: "Form",
    defaultLocale: defaultLocale.Form
  }, function (locale) {
    __INTERNAL__.locale = locale;
    return /*#__PURE__*/React.createElement(SizeContextProvider, {
      size: size
    }, /*#__PURE__*/React.createElement(FormContext.Provider, {
      value: formContextValue
    }, /*#__PURE__*/React.createElement(FieldForm, _extends({
      id: name
    }, restFormProps, {
      onFinishFailed: onInternalFinishFailed,
      onValuesChange: onInternalValuesChange,
      form: wrapForm,
      className: formClassName
    }))));
  });
};

var Form = /*#__PURE__*/React.forwardRef(InternalForm);
export { useForm, List };
export default Form;