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

import InputSign from '@material-hu/components/composed-components/dynamic-forms/components/FormInputs/InputSign';

import { uploadFiles } from 'src/services/attachments';
import { useLokaliseTranslation } from 'src/utils/i18n';

type Props = {
  name: string;
  formFilled?: boolean;
  required?: boolean;
};

const DigitalSignature = ({ name, formFilled, required }: Props) => {
  const { t } = useLokaliseTranslation(['service_management', 'validations']);
  const { control } = useFormContext();

  return (
    <Controller
      name={name}
      control={control}
      rules={{
        required: required && !formFilled ? t('validations:required') : false,
      }}
      render={() => (
        <InputSign
          name={name}
          disabled={formFilled}
          signProps={{
            uploadFn: uploadFiles,
          }}
          buttonProps={{
            children: t('service_management:add_signature'),
          }}
          deleteButtonProps={{
            children: t('general:delete'),
          }}
          editButtonProps={{
            children: t('general:edit'),
          }}
        />
      )}
    />
  );
};

export default DigitalSignature;
