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

import { type FormInputClassicProps } from './types';
import InputClassic from '.';

const FormInputClassic = ({
  name,
  inputProps,
  rules,
}: FormInputClassicProps) => {
  return (
    <Controller
      render={({
        field: { ref, onBlur: fieldOnBlur, ...field },
        fieldState: { error },
      }) => (
        <InputClassic
          {...field}
          {...inputProps}
          inputRef={ref}
          error={!!error || !!inputProps.error}
          errorText={inputProps.errorText || error?.message}
          onBlur={() => {
            fieldOnBlur();
            inputProps.onBlur?.();
          }}
        />
      )}
      name={name}
      rules={rules}
    />
  );
};

export type { FormInputClassicProps };

export default FormInputClassic;
