import HuDialog from '@material-hu/components/design-system/Dialog';

import { Article } from 'src/types/libraries';
import { useLokaliseTranslation } from 'src/utils/i18n';

import { useDeleteLibrary } from '../hooks/useDeleteLibrary';

export type DeleteLibraryModalProps = {
  library: Article;
  onClose: () => void;
  onConfirm?: () => void;
  onCancel?: () => void;
};

export const DeleteLibraryModal = ({
  library,
  onClose,
  onConfirm = () => null,
  onCancel = () => null,
}: DeleteLibraryModalProps) => {
  const { t } = useLokaliseTranslation('libraries');

  const deleteMutation = useDeleteLibrary(library);

  const handleConfirm = () => {
    onConfirm();
    deleteMutation.mutate(undefined, { onSettled: onClose });
  };

  const handleCancel = () => {
    onCancel();
    onClose();
  };

  return (
    <HuDialog
      onClose={onClose}
      title={t('article.delete.confirm.title')}
      textBody={t('article.delete.confirm.description')}
      secondaryButtonProps={{
        children: t('general:cancel'),
        onClick: handleCancel,
      }}
      primaryButtonProps={{
        loading: deleteMutation.isLoading,
        children: t('general:delete'),
        onClick: handleConfirm,
      }}
    />
  );
};

export default DeleteLibraryModal;
