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

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

import { logEvent } from 'src/config/logging';
import { EventName } from 'src/types/amplitude';

import { type Article } from '../../types';

export type ArticleAttachmentsProps = { article: Article };

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

  return (
    <Attachments
      attachments={(article.attachments || []) as SortableAttachment[]}
      onDownload={handleDownloadAttachment}
    />
  );
};
