import { logEvent } from 'src/config/logging';
import ArticleDetail from 'src/pages/dashboard/news/components/ArticleDetail';
import { EventName } from 'src/types/amplitude';
import { Attachment } from 'src/types/attachments';
import { CopyTypePath } from 'src/types/deeplinks';

import { librariesRoutes } from '../routes';

import Attachments from './Attachments';
import {
  LibraryArticleLayout,
  LibraryArticleLayoutProps,
} from './LibraryArticleLayout';

export type LibraryDetailProps = Omit<LibraryArticleLayoutProps, 'children'>;

const LibraryDetail = ({ library, ...layoutProps }: LibraryDetailProps) => {
  const handleDownloadAttachment = (attachment: Attachment) => {
    logEvent(EventName.WIDGET_LIBRARY_ATTACHMENT_DOWNLOAD, {
      widgetId: library.id,
      parentId: library.parentId,
      name: library.title,
      fileType: attachment.type,
      fileName: attachment.name,
    });
  };

  const { attachments, ...article } = library;

  return (
    <LibraryArticleLayout
      library={library}
      {...layoutProps}
    >
      <ArticleDetail
        {...article}
        isArticle={false}
        copyType={CopyTypePath.LIBRARY_ARTICLE}
        copyRoute={librariesRoutes.library(library.id)}
      />
      <Attachments
        attachments={attachments}
        onDownload={handleDownloadAttachment}
      />
    </LibraryArticleLayout>
  );
};

export default LibraryDetail;
