import { useEffect } from 'react';
import { useForm } from 'react-final-form';
/**
 * Form helper hook that runs a callback when the value of a field has changed
 * Usefull for creating effects where changes to one field affect the value of another field imperatively
 */
export function useFieldChangeListener(name, cb) {
    const form = useForm();
    useEffect(() => {
        var _a;
        let lastValue = (_a = form.getFieldState(name)) === null || _a === void 0 ? void 0 : _a.value;
        const unsubscribe = form.subscribe(({ values }) => {
            const value = values[name];
            if (value !== lastValue) {
                cb(form, value, lastValue);
            }
            lastValue = values[name];
        }, { values: true });
        return unsubscribe;
    }, [form, name, cb]);
}
//# sourceMappingURL=useFieldChangeListener.js.map