import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import React from "react";
import classNames from "classnames";
import { Form as TForm } from "@tarojs/components";
import { Context } from "./context";
import { SECRET, useForm } from "./useform";
import { ComponentDefaults } from "../../utils/typings";
import Cell from "../cell/index";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    labelPosition: 'right',
    starPosition: 'left',
    disabled: false,
    divider: false,
    validateTrigger: 'onChange',
    onReset: function() {},
    onSubmit: function() {},
    onFinish: function(values) {},
    onFinishFailed: function(values, errorFields) {}
});
var PositionInfo = {
    top: 'form-layout-top',
    left: 'form-layout-left',
    right: 'form-layout-right'
};
export var Form = /*#__PURE__*/ React.forwardRef(function(props, ref) {
    var classPrefix = 'nut-form';
    var _ref = _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 = _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 = _sliced_to_array(useForm(), 1), formInstance = _$ref[0], _$ref;
    }
    React.useImperativeHandle(ref, function() {
        return formInstance;
    });
    formInstance.starPosition = starPosition;
    var submit = formInstance.submit, resetFields = formInstance.resetFields;
    var _formInstance_getInternal = formInstance.getInternal(SECRET), setCallback = _formInstance_getInternal.setCallback, setInitialValues = _formInstance_getInternal.setInitialValues;
    // 设置校验后的回调，给组件的使用者暴露的接口
    setCallback({
        onFinish: onFinish,
        onFinishFailed: onFinishFailed
    });
    // 初始化 initialValues 和 store
    var mountRef = React.useRef(false);
    setInitialValues(initialValues, !mountRef.current);
    if (!mountRef.current) {
        mountRef.current = true;
    }
    return /*#__PURE__*/ React.createElement(TForm, _object_spread_props(_object_spread({}, rest), {
        className: classNames(classPrefix, PositionInfo[labelPosition], className),
        style: style,
        onSubmit: function(e) {
            e.preventDefault();
            e.stopPropagation();
            submit();
            onSubmit === null || onSubmit === void 0 ? void 0 : onSubmit();
        },
        onReset: function(e) {
            e.preventDefault();
            e.stopPropagation();
            resetFields();
            onReset === null || onReset === void 0 ? void 0 : onReset();
        }
    }), /*#__PURE__*/ React.createElement(Cell.Group, {
        divider: divider
    }, /*#__PURE__*/ React.createElement(Context.Provider, {
        value: {
            formInstance: formInstance,
            labelPosition: labelPosition,
            disabled: disabled,
            validateTrigger: validateTrigger
        }
    }, children), footer ? /*#__PURE__*/ React.createElement(Cell, {
        className: "".concat(classPrefix, "-footer")
    }, footer) : null));
});
Form.displayName = 'NutForm';
