import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import React from 'react';
import classNames from 'classnames';
import { RcForm } from '../components';
import { ConfigContext } from '../config-provider/context';
import useForm from './hooks/useForm';
import { FormContext } from './context';
var Form = /*#__PURE__*/React.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 = _objectWithoutProperties(props, ["form", "name", "prefixCls", "onFinishFailed", "scrollToFirstError", "layout", "labelCol", "wrapperCol", "colon", "className", "disabled"]);

  var _React$useContext = React.useContext(ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var prefixCls = getPrefixClassName('form', customPrefixCls);
  var formCls = classNames(prefixCls, _defineProperty({}, "".concat(prefixCls, "-").concat(layout), true), className);

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

  wrapForm.__INTERNAL__.name = name;
  React.useImperativeHandle(ref, function () {
    return wrapForm;
  });
  var formContextVals = React.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.createElement(FormContext.Provider, {
    value: formContextVals
  }, /*#__PURE__*/React.createElement(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';
export default Form;
export { useForm };