import {
    FormControl, FormDescription,
    FormField, FormItem, FormLabel, FormMessage,
} from "@repo/shadcn-ui/components";
import { Control, FieldPath, FieldValues } from "react-hook-form";
import { AmountInput, AmountInputProps } from './amount-input';


type AmountInputFieldProps<T extends FieldValues> = {
    inputId?: string;
    control: Control<T>;
    name: FieldPath<T>;
    label?: string;
    description?: string;
    required?: boolean;
} & Omit<AmountInputProps, "value" | "onChange">

export function AmountInputField<T extends FieldValues>({
    inputId,
    control,
    name,
    label,
    description,
    required = false,
    ...inputProps

}: AmountInputFieldProps<T>) {
    return (
        <FormField
            control={control}
            name={name}
            render={({ field }) => (
                <FormItem>
                    {label ? (
                        <FormLabel htmlFor={inputId}>
                            {label} {required ? <span aria-hidden="true">*</span> : null}
                        </FormLabel>
                    ) : null}
                    <FormControl>
                        <AmountInput
                            id={inputId}
                            value={field.value ?? ""}
                            onChange={field.onChange}
                            {...inputProps}
                        />
                    </FormControl>
                    {description ? <FormDescription>{description}</FormDescription> : null}
                    <FormMessage />
                </FormItem>
            )}
        />
    );
}