"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Form", {
    enumerable: true,
    get: function() {
        return Form;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _react = /*#__PURE__*/ _interop_require_default._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _components = require("@tarojs/components");
var _context = require("./context");
var _useform = require("./useform");
var _typings = require("../../utils/typings");
var _index = /*#__PURE__*/ _interop_require_default._(require("../cell/index"));
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    labelPosition: 'right',
    starPosition: 'left',
    disabled: false,
    divider: false,
    validateTrigger: 'onChange',
    onReset: function onReset() {},
    onSubmit: function onSubmit() {},
    onFinish: function onFinish(values) {},
    onFinishFailed: function onFinishFailed(values, errorFields) {}
});
var PositionInfo = {
    top: 'form-layout-top',
    left: 'form-layout-left',
    right: 'form-layout-right'
};
var Form = /*#__PURE__*/ _react.default.forwardRef(function(props, ref) {
    var classPrefix = 'nut-form';
    var _ref = (0, _object_spread._)({}, defaultProps, props), className = _ref.className, style = _ref.style, footer = _ref.footer, children = _ref.children, initialValues = _ref.initialValues, divider = _ref.divider, disabled = _ref.disabled, onFinish = _ref.onFinish, onFinishFailed = _ref.onFinishFailed, validateTrigger = _ref.validateTrigger, labelPosition = _ref.labelPosition, starPosition = _ref.starPosition, onReset = _ref.onReset, onSubmit = _ref.onSubmit, form = _ref.form, rest = (0, _object_without_properties._)(_ref, [
        "className",
        "style",
        "footer",
        "children",
        "initialValues",
        "divider",
        "disabled",
        "onFinish",
        "onFinishFailed",
        "validateTrigger",
        "labelPosition",
        "starPosition",
        "onReset",
        "onSubmit",
        "form"
    ]);
    var formInstance;
    if (form !== undefined) {
        formInstance = form;
    } else {
        // eslint-disable-next-line react-hooks/rules-of-hooks
        ;
        var _$ref;
        _$ref = (0, _sliced_to_array._)((0, _useform.useForm)(), 1), formInstance = _$ref[0], _$ref;
    }
    _react.default.useImperativeHandle(ref, function() {
        return formInstance;
    });
    formInstance.starPosition = starPosition;
    var submit = formInstance.submit, resetFields = formInstance.resetFields;
    var _formInstance_getInternal = formInstance.getInternal(_useform.SECRET), setCallback = _formInstance_getInternal.setCallback, setInitialValues = _formInstance_getInternal.setInitialValues;
    // 设置校验后的回调，给组件的使用者暴露的接口
    setCallback({
        onFinish: onFinish,
        onFinishFailed: onFinishFailed
    });
    // 初始化 initialValues 和 store
    var mountRef = _react.default.useRef(false);
    setInitialValues(initialValues, !mountRef.current);
    if (!mountRef.current) {
        mountRef.current = true;
    }
    return /*#__PURE__*/ _react.default.createElement(_components.Form, (0, _object_spread_props._)((0, _object_spread._)({}, rest), {
        className: (0, _classnames.default)(classPrefix, PositionInfo[labelPosition], className),
        style: style,
        onSubmit: function onSubmit1(e) {
            e.preventDefault();
            e.stopPropagation();
            submit();
            onSubmit === null || onSubmit === void 0 ? void 0 : onSubmit();
        },
        onReset: function onReset1(e) {
            e.preventDefault();
            e.stopPropagation();
            resetFields();
            onReset === null || onReset === void 0 ? void 0 : onReset();
        }
    }), /*#__PURE__*/ _react.default.createElement(_index.default.Group, {
        divider: divider
    }, /*#__PURE__*/ _react.default.createElement(_context.Context.Provider, {
        value: {
            formInstance: formInstance,
            labelPosition: labelPosition,
            disabled: disabled,
            validateTrigger: validateTrigger
        }
    }, children), footer ? /*#__PURE__*/ _react.default.createElement(_index.default, {
        className: "".concat(classPrefix, "-footer")
    }, footer) : null));
});
Form.displayName = 'NutForm';
