import HuFormUploader from '@material-hu/components/design-system/Uploader/form';

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

const AddAttachmentFormContent = () => {
  const { t } = useLokaliseTranslation('general');

  const handleUpload = async (file: File) => {
    try {
      const attachment = await uploadGenericFile(file);
      return {
        status: 'success' as const,
        attachment: {
          url: attachment.url || '',
          name: attachment.name || '',
          size: attachment.size || '',
          bytes: attachment.bytes || 0,
          type: attachment.type,
        },
        file,
      };
    } catch (err) {
      return {
        status: 'error' as const,
        file,
      };
    }
  };

  return (
    <HuFormUploader
      name="fileCards"
      uploaderProps={{
        triggerOnChangeWhenUploading: true,
        acceptedTypes: ['image', 'pdf', 'msword', 'video'],
        uploadFunction: handleUpload,
        helperText: t('attachment.empty'),
        description: t('attachment.allowed_with_max_size', {
          maxSize: '100MB',
        }),
        fileSizeLimit: Infinity,
        slotProps: {
          fileCard: {
            showRemoveUploadingButton: false,
          },
        },
      }}
    />
  );
};

export default AddAttachmentFormContent;
