import { Controller } from 'react-hook-form'
import TimePicker, { ITimePicker } from '../Input/TimePicker'

interface Props extends Omit<ITimePicker, 'onChange' | 'value'> {
  name: string
  control: any
}

export default function FormTimePicker({
  name,
  control,
  label,
  minTime,
  maxTime,
  required,
  ...rest
}: Props) {
  return (
    <Controller
      name={name}
      control={control}
      render={({ field, fieldState: { error } }) => (
        <TimePicker
          label={label}
          required={required}
          onChange={field.onChange}
          value={field.value}
          minTime={minTime}
          maxTime={maxTime}
          inputProps={{
            ...rest.inputProps,
            error: !!error,
            helperText: error?.message ?? '',
          }}
          {...rest}
        />
      )}
    />
  )
}
