'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.List = exports.useForm = undefined;

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');

var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _react = require('react');

var React = _interopRequireWildcard(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _rcFieldForm = require('rc-field-form');

var _rcFieldForm2 = _interopRequireDefault(_rcFieldForm);

var _context = require('antd/lib/config-provider/context');

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

var _util = require('./util');

var _useForm3 = require('../hooks/useForm');

var _useForm4 = _interopRequireDefault(_useForm3);

var _SizeContext = require('../../config-provider/SizeContext');

var _SizeContext2 = _interopRequireDefault(_SizeContext);

var _LocaleReceiver = require('antd/es/locale-provider/LocaleReceiver');

var _LocaleReceiver2 = _interopRequireDefault(_LocaleReceiver);

var _default = require('../../locale/default');

var _default2 = _interopRequireDefault(_default);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var __rest = undefined && undefined.__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;
};

// @ts-ignore
var InternalForm = function InternalForm(props, ref) {
    var _classNames;

    var contextSize = React.useContext(_SizeContext2['default']);
    // @ts-ignore

    var _React$useContext = React.useContext(_context.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 = (0, _react.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 = (0, _classnames2['default'])(prefixCls, simplePrefixCls, (_classNames = {}, (0, _defineProperty3['default'])(_classNames, prefixCls + '-' + layout, true), (0, _defineProperty3['default'])(_classNames, prefixCls + '-hide-required-mark', mergedRequiredMark === false), (0, _defineProperty3['default'])(_classNames, prefixCls + '-rtl', direction === 'rtl'), (0, _defineProperty3['default'])(_classNames, prefixCls + '-' + size, size), _classNames), className);

    var _useForm = (0, _useForm4['default'])(form),
        _useForm2 = (0, _slicedToArray3['default'])(_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 = (0, _react.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];
            (0, _util.cascadeHandler)(wrapForm, allValues, allDependency, currentItem);
        }
    };
    (0, _react.useEffect)(function () {
        if (restFormProps.initialValues && Object.keys(allDependency).length) {
            var allValues = wrapForm.getFieldsValue();
            (0, _util.cascadeHandler)(wrapForm, allValues, allDependency);
        }
    }, []);
    return React.createElement(
        _LocaleReceiver2['default'],
        { componentName: 'Form', defaultLocale: _default2['default'].Form },
        function (locale) {
            __INTERNAL__.locale = locale;
            return React.createElement(
                _SizeContext.SizeContextProvider,
                { size: size },
                React.createElement(
                    _context2.FormContext.Provider,
                    { value: formContextValue },
                    React.createElement(_rcFieldForm2['default'], (0, _extends3['default'])({ id: name }, restFormProps, { onFinishFailed: onInternalFinishFailed, onValuesChange: onInternalValuesChange, form: wrapForm, className: formClassName }))
                )
            );
        }
    );
};
var Form = React.forwardRef(InternalForm);
exports.useForm = _useForm4['default'];
exports.List = _rcFieldForm.List;
exports['default'] = Form;