import _extends from 'babel-runtime/helpers/extends';
import _slicedToArray from 'babel-runtime/helpers/slicedToArray';
import _defineProperty from 'babel-runtime/helpers/defineProperty';
var __rest = this && this.__rest || function (s, e) {
    var t = {};
    for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
    }if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
        if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
    }
    return t;
};
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 '../../config-provider/SizeContext';
import LocaleReceiver from 'antd/es/locale-provider/LocaleReceiver';
import defaultLocale from '../../locale/default';
// @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 === undefined ? '' : _props$className,
        _props$size = props.size,
        size = _props$size === undefined ? contextSize : _props$size,
        form = props.form,
        _props$dataBinding = props.dataBinding,
        dataBinding = _props$dataBinding === undefined ? 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 === undefined ? 'horizontal' : _props$layout,
        scrollToFirstError = props.scrollToFirstError,
        requiredMark = props.requiredMark,
        onFinishFailed = props.onFinishFailed,
        onValuesChange = props.onValuesChange,
        restFormProps = __rest(props, ["prefixCls", "className", "size", "form", "dataBinding", "colon", "labelAlign", "labelCol", "wrapperCol", "hideRequiredMark", "layout", "scrollToFirstError", "requiredMark", "onFinishFailed", "onValuesChange"]);

    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, prefixCls + '-' + layout, true), _defineProperty(_classNames, prefixCls + '-hide-required-mark', mergedRequiredMark === false), _defineProperty(_classNames, prefixCls + '-rtl', direction === 'rtl'), _defineProperty(_classNames, prefixCls + '-' + 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 React.createElement(
        LocaleReceiver,
        { componentName: 'Form', defaultLocale: defaultLocale.Form },
        function (locale) {
            __INTERNAL__.locale = locale;
            return React.createElement(
                SizeContextProvider,
                { size: size },
                React.createElement(
                    FormContext.Provider,
                    { value: formContextValue },
                    React.createElement(FieldForm, _extends({ id: name }, restFormProps, { onFinishFailed: onInternalFinishFailed, onValuesChange: onInternalValuesChange, form: wrapForm, className: formClassName }))
                )
            );
        }
    );
};
var Form = React.forwardRef(InternalForm);
export { useForm, List };
export default Form;