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

import { __assign } from 'tslib';
import { useForm } from 'react-hook-form';
import debounce from 'lodash/debounce';
import React__default from 'react';
import { useValidationResolver } from './use-validation-resolver.js';

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

export { useSubForm as default };
