"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "useForm", {
  enumerable: true,
  get: function get() {
    return _useForm3.default;
  }
});
exports.default = void 0;

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _react = _interopRequireDefault(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _components = require("../components");

var _context = require("../config-provider/context");

var _useForm3 = _interopRequireDefault(require("./hooks/useForm"));

var _context2 = require("./context");

var Form = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var form = props.form,
      name = props.name,
      customPrefixCls = props.prefixCls,
      onFinishFailed = props.onFinishFailed,
      scrollToFirstError = props.scrollToFirstError,
      layout = props.layout,
      labelCol = props.labelCol,
      wrapperCol = props.wrapperCol,
      colon = props.colon,
      className = props.className,
      disabled = props.disabled,
      restProps = (0, _objectWithoutProperties2.default)(props, ["form", "name", "prefixCls", "onFinishFailed", "scrollToFirstError", "layout", "labelCol", "wrapperCol", "colon", "className", "disabled"]);

  var _React$useContext = _react.default.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var prefixCls = getPrefixClassName('form', customPrefixCls);
  var formCls = (0, _classnames.default)(prefixCls, (0, _defineProperty2.default)({}, "".concat(prefixCls, "-").concat(layout), true), className);

  var _useForm = (0, _useForm3.default)(form),
      _useForm2 = (0, _slicedToArray2.default)(_useForm, 1),
      wrapForm = _useForm2[0];

  wrapForm.__INTERNAL__.name = name;

  _react.default.useImperativeHandle(ref, function () {
    return wrapForm;
  });

  var formContextVals = _react.default.useMemo(function () {
    return {
      name: name,
      labelCol: labelCol,
      wrapperCol: wrapperCol,
      disabled: disabled,
      layout: layout
    };
  }, [name, labelCol, wrapperCol, disabled, layout]);

  var onFormFinishFailed = function onFormFinishFailed(errInfo) {
    onFinishFailed === null || onFinishFailed === void 0 ? void 0 : onFinishFailed(errInfo);

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

  return /*#__PURE__*/_react.default.createElement(_context2.FormContext.Provider, {
    value: formContextVals
  }, /*#__PURE__*/_react.default.createElement(_components.RcForm // ref={formRef}
  , Object.assign({
    // ref={formRef}
    id: name,
    name: name,
    onFinishFailed: onFormFinishFailed,
    form: wrapForm,
    className: formCls
  }, restProps)));
});

Form.defaultProps = {
  scrollToFirstError: false,
  layout: 'horizontal',
  name: 'klein-form'
};
Form.displayName = 'Form';
var _default = Form;
exports.default = _default;