/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __assign } from 'tslib';
import React__default from 'react';
import { themeable, localeable, noop, isObjectShallowModified } from 'amis-core';
import { useForm, FormProvider } from 'react-hook-form';
import { useValidationResolver } from '../hooks/use-validation-resolver.js';
import debounce from 'lodash/debounce';

function Form(props) {
    var _a, _b;
    var cx = props.classnames, className = props.className, autoSubmit = props.autoSubmit, value = props.value, onChange = props.onChange;
    var defaultValues = (_a = props.value) !== null && _a !== void 0 ? _a : props.defaultValue;
    var methods = useForm({
        defaultValues: defaultValues,
        resolver: useValidationResolver(props.translate, props.onValidate)
    });
    var onSubmit = React__default.useRef(methods.handleSubmit(props.onSubmit || noop));
    if (autoSubmit) {
        onSubmit = React__default.useRef(debounce(methods.handleSubmit(props.onSubmit || noop), 250, {
            leading: false,
            trailing: true
        }));
        React__default.useEffect(function () {
            var subscriber = methods.watch(onSubmit.current);
            return function () {
                var _a, _b;
                subscriber.unsubscribe();
                // debounce 后需要销毁
                (_b = (_a = onSubmit.current) === null || _a === void 0 ? void 0 : _a.cancel) === null || _b === void 0 ? void 0 : _b.call(_a);
            };
        }, []);
    }
    React__default.useEffect(function () {
        if (value && isObjectShallowModified(value, methods.getValues(), false)) {
            Object.keys(value).forEach(function (key) {
                methods.setValue(key, value[key]);
            });
        }
    }, [value]);
    if (onChange) {
        React__default.useEffect(function () {
            var subscriber = methods.watch(function (value, info) {
                onChange(__assign(__assign({}, defaultValues), value));
            });
            return function () { return subscriber.unsubscribe(); };
        }, [onChange]);
    }
    React__default.useImperativeHandle(props.forwardRef, function () {
        return {
            submit: function () {
                return new Promise(function (resolve) {
                    methods.handleSubmit(function (values) {
                        var _a;
                        (_a = props.onSubmit) === null || _a === void 0 ? void 0 : _a.call(props, values);
                        resolve(values);
                    }, function (e) { var _a; return resolve(((_a = e.customValidate) === null || _a === void 0 ? void 0 : _a.message) || false); })();
                });
            },
            validate: function () {
                return new Promise(function (resolve) {
                    methods.handleSubmit(function () {
                        resolve('');
                    }, function (e) {
                        var _a;
                        return resolve(((_a = e.customValidate) === null || _a === void 0 ? void 0 : _a.message) ||
                            props.translate('Form.validateFailed'));
                    })();
                });
            }
        };
    }, []);
    return (React__default.createElement(FormProvider, __assign({}, methods),
        React__default.createElement("form", { className: cx('Form', className), onSubmit: onSubmit.current, noValidate: true },
            React__default.createElement("input", { type: "submit", style: { display: 'none' } }), (_b = props.children) === null || _b === void 0 ? void 0 :
            _b.call(props, __assign(__assign({}, methods), { onSubmit: onSubmit.current })))));
}
var ThemedForm = themeable(localeable(Form));
var Form$1 = React__default.forwardRef(function (props, ref) { return (React__default.createElement(ThemedForm, __assign({}, props, { forwardRef: ref }))); });

export { Form, Form$1 as default };
