'use client';
import { jsx as _jsx } from "react/jsx-runtime";
import { useEffect, useMemo } from 'react';
import { useFormContext } from 'react-hook-form';
import { nanoid } from 'nanoid';
export const IdField = ({ name = 'id', value, generator = 'nanoid', ...rest }) => {
    const { register, setValue, formState: { submitCount }, } = useFormContext();
    const actualValue = useMemo(() => {
        if (value !== undefined)
            return value;
        if (typeof generator === 'function')
            return generator();
        switch (generator) {
            case 'nanoid':
                return nanoid();
            case 'uuid':
                return crypto.randomUUID();
        }
        // We want to re-generate the ID on every submit
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [value, generator, submitCount]);
    // Sync the generated ID with react-hook-form when actualValue changes (submitCount)
    useEffect(() => {
        setValue(name, actualValue);
    }, [setValue, name, actualValue]);
    return _jsx("input", { type: "hidden", value: actualValue, ...register(name), ...rest });
};
//# sourceMappingURL=id-field.js.map