import { type SortableAttachment } from '@material-hu/types/attachments';

import FormAttachments from '@material-hu/components/composed-components/files/Attachments/form';

import { logEvent } from 'src/config/logging';
import { type Article } from 'src/pages/dashboard/HuLibraries/types';
import { uploadAttachment } from 'src/services/attachments';
import { EventName } from 'src/types/amplitude';
import { useLokaliseTranslation } from 'src/utils/i18n';

type ArticleEditAttachmentsProps = {
  article: Article;
};

export const ArticleEditAttachments = ({
  article,
}: ArticleEditAttachmentsProps) => {
  const { t } = useLokaliseTranslation('libraries');

  const handleDownload = (attachment: SortableAttachment) => {
    logEvent(EventName.WIDGET_LIBRARY_ATTACHMENT_DOWNLOAD, {
      widgetId: article.id,
      parentId: article.parentId,
      name: article.title,
      fileType: attachment.type,
      fileName: attachment.name,
    });
  };

  return (
    <FormAttachments
      name="attachments"
      inputProps={{
        upload: uploadAttachment,
        onDownload: handleDownload,
        emptyDescription: t('library.attachment.upload.description'),
      }}
    />
  );
};
