import { __assign, __rest } from "tslib";
import React, { useContext } from 'react';
import classNames from 'classnames';
import { FormInstanceContext } from './form-instance-context';
import { FormOptionsContext } from './form-options-context';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
import FormInstance from './form-instance';
var Form = function (props) {
    var _a = props.initialValues, initialValues = _a === void 0 ? {} : _a, _b = props.labelCol, labelCol = _b === void 0 ? { span: 8, offset: 0 } : _b, _c = props.wrapperCol, wrapperCol = _c === void 0 ? { span: 16, offset: 0 } : _c, _d = props.validateTrigger, validateTrigger = _d === void 0 ? 'onChange' : _d, _e = props.layout, layout = _e === void 0 ? 'horizontal' : _e, form = props.form, onFinish = props.onFinish, onFinishFailed = props.onFinishFailed, className = props.className, children = props.children, customisedCls = props.prefixCls, otherProps = __rest(props, ["initialValues", "labelCol", "wrapperCol", "validateTrigger", "layout", "form", "onFinish", "onFinishFailed", "className", "children", "prefixCls"]);
    var configContext = useContext(ConfigContext);
    var prefixCls = getPrefixCls('form', configContext.prefixCls, customisedCls);
    var cls = classNames(prefixCls, className, prefixCls + "_" + layout);
    var formRef = React.useRef(form ? form : new FormInstance(initialValues));
    var formOnSubmit = function (e) {
        e.preventDefault();
        var formIns = formRef.current;
        formIns.validateFields();
        var values = formIns.getFieldValues();
        var errors = formIns.getFiledErrors();
        if (Object.keys(errors).some(function (key) { return errors[key].length > 0; })) {
            onFinishFailed && onFinishFailed({ values: values, errors: errors });
        }
        else {
            onFinish && onFinish(values);
        }
    };
    var formOnReset = function () {
        formRef.current.resetFields();
    };
    return (React.createElement(FormInstanceContext.Provider, { value: formRef.current },
        React.createElement(FormOptionsContext.Provider, { value: { labelCol: labelCol, wrapperCol: wrapperCol, validateTrigger: validateTrigger, layout: layout } },
            React.createElement("form", __assign({}, otherProps, { className: cls, onSubmit: formOnSubmit, onReset: formOnReset }), children))));
};
Form.displayName = 'Form';
export default Form;
//# sourceMappingURL=form.js.map