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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');
var reactHookForm = require('react-hook-form');
var useValidationResolver = require('../hooks/use-validation-resolver.js');
var debounce = require('lodash/debounce');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var debounce__default = /*#__PURE__*/_interopDefaultLegacy(debounce);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
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 = reactHookForm.useForm({
        defaultValues: defaultValues,
        resolver: useValidationResolver.useValidationResolver(props.translate, props.onValidate)
    });
    var onSubmit = React__default["default"].useRef(methods.handleSubmit(props.onSubmit || amisCore.noop));
    if (autoSubmit) {
        onSubmit = React__default["default"].useRef(debounce__default["default"](methods.handleSubmit(props.onSubmit || amisCore.noop), 250, {
            leading: false,
            trailing: true
        }));
        React__default["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["default"].useEffect(function () {
        if (value && amisCore.isObjectShallowModified(value, methods.getValues(), false)) {
            Object.keys(value).forEach(function (key) {
                methods.setValue(key, value[key]);
            });
        }
    }, [value]);
    if (onChange) {
        React__default["default"].useEffect(function () {
            var subscriber = methods.watch(function (value, info) {
                onChange(tslib.__assign(tslib.__assign({}, defaultValues), value));
            });
            return function () { return subscriber.unsubscribe(); };
        }, [onChange]);
    }
    React__default["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 (_J$X_(reactHookForm.FormProvider, tslib.__assign({}, methods),
        _J$X_("form", { className: cx('Form', className), onSubmit: onSubmit.current, noValidate: true },
            _J$X_("input", { type: "submit", style: { display: 'none' } }), (_b = props.children) === null || _b === void 0 ? void 0 :
            _b.call(props, tslib.__assign(tslib.__assign({}, methods), { onSubmit: onSubmit.current })))));
}
var ThemedForm = amisCore.themeable(amisCore.localeable(Form));
var Form$1 = React__default["default"].forwardRef(function (props, ref) { return (_J$X_(ThemedForm, tslib.__assign({}, props, { forwardRef: ref }))); });

exports.Form = Form;
exports["default"] = Form$1;
