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

import { type FormInputMoneyProps } from './types';
import InputMoney from '.';

const FormInputMoney = ({ name, inputProps, rules }: FormInputMoneyProps) => {
  return (
    <Controller
      render={({
        field: { ref, onBlur, value, onChange, ...field },
        fieldState: { error },
      }) => (
        <InputMoney
          {...field}
          {...inputProps}
          value={value}
          onChange={newValue => onChange(newValue)}
          inputRef={ref}
          error={!!error}
          errorText={error?.message}
        />
      )}
      name={name}
      rules={rules}
    />
  );
};

export type { FormInputMoneyProps };

export default FormInputMoney;
