import { Controller } from 'react-hook-form';

import { type FormInputTimeRangeProps } from './types';
import InputTimeRange from '.';

const FormInputTimeRange = ({
  name,
  inputProps,
  rules,
}: FormInputTimeRangeProps) => {
  return (
    <Controller
      name={name}
      rules={rules}
      render={({ field: { ref, ...field }, fieldState: { error } }) => (
        <InputTimeRange
          value={field.value || [null, null]}
          onChange={field.onChange}
          {...inputProps}
          error={inputProps.error || !!error}
          errorText={error?.message || inputProps.errorText}
        />
      )}
    />
  );
};

export type { FormInputTimeRangeProps };

export default FormInputTimeRange;
