/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

'use strict';

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

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

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

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

var useSubForm = function (defaultValue, translate, onUpdate) {
    var methods = reactHookForm.useForm({
        defaultValues: defaultValue,
        mode: 'onChange',
        shouldUnregister: true,
        resolver: useValidationResolver.useValidationResolver(translate)
    });
    // 数据修改后，自动提交更新到上层
    var lazyUpdate = React__default["default"].useRef(debounce__default["default"](onUpdate, 250, {
        leading: false,
        trailing: true
    }));
    // 销毁的时候要 cancel
    React__default["default"].useEffect(function () {
        return function () { return lazyUpdate.current.cancel(); };
    }, []);
    // 监控数值变化，自动同步到上层
    React__default["default"].useEffect(function () {
        var subscriber = methods.watch(function (data) {
            // 因为 watch 只会触发有表单项的数值，而原始数据中可能包含其他属性，所以要合并一下
            lazyUpdate.current(tslib.__assign(tslib.__assign({}, defaultValue), data));
        });
        return function () { return subscriber.unsubscribe(); };
    }, [methods.watch]);
    return methods;
};

exports["default"] = useSubForm;
