"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Form = void 0;
var _tslib = require("tslib");
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _list = _interopRequireDefault(require("../list"));
var _rcFieldForm = _interopRequireDefault(require("rc-field-form"));
var _context = require("./context");
var _withDefaultProps = require("../../utils/with-default-props");
var _header = require("./header");
var _configProvider = require("../config-provider");
var _deepmerge = _interopRequireDefault(require("deepmerge"));
var _formArray = require("./form-array");
var _traverseReactNode = require("../../utils/traverse-react-node");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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 classPrefix = 'adm-form';
const defaultProps = _context.defaultFormContext;
const Form = exports.Form = (0, _react.forwardRef)((p, ref) => {
  const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
  const {
      className,
      style,
      hasFeedback,
      children,
      layout,
      footer,
      mode,
      disabled,
      requiredMarkStyle
    } = props,
    formProps = (0, _tslib.__rest)(props, ["className", "style", "hasFeedback", "children", "layout", "footer", "mode", "disabled", "requiredMarkStyle"]);
  const {
    locale
  } = (0, _configProvider.useConfig)();
  const validateMessages = (0, _react.useMemo)(() => (0, _deepmerge.default)(locale.Form.defaultValidateMessages, formProps.validateMessages || {}), [locale.Form.defaultValidateMessages, formProps.validateMessages]);
  const lists = [];
  let currentHeader = null;
  let items = [];
  let count = 0;
  function collect() {
    if (items.length === 0) return;
    count += 1;
    lists.push(_react.default.createElement(_list.default, {
      header: currentHeader,
      key: count,
      mode: mode
    }, items));
    items = [];
  }
  (0, _traverseReactNode.traverseReactNode)(props.children, child => {
    if (_react.default.isValidElement(child)) {
      if (child.type === _header.Header) {
        collect();
        currentHeader = child.props.children;
        return;
      }
      if (child.type === _formArray.FormArray) {
        collect();
        lists.push(child);
        return;
      }
    }
    items.push(child);
  });
  collect();
  return _react.default.createElement(_rcFieldForm.default, Object.assign({
    className: (0, _classnames.default)(classPrefix, className),
    style: style,
    ref: ref
  }, formProps, {
    validateMessages: validateMessages
  }), _react.default.createElement(_context.FormContext.Provider, {
    value: {
      name: formProps.name,
      hasFeedback,
      layout,
      requiredMarkStyle,
      disabled
    }
  }, lists), footer && _react.default.createElement("div", {
    className: `${classPrefix}-footer`
  }, footer));
});