import { type MouseEvent } from 'react';

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

import { useLokaliseTranslation } from 'src/utils/i18n';

import useDeleteCourse from '../../../hooks/useDeleteCourse';
import { type CourseListItem } from '../../../types';

type DeleteCourseModalProps = {
  onClose: () => void;
  course: CourseListItem;
};

const DeleteCourseModal = ({ onClose, course }: DeleteCourseModalProps) => {
  const { t } = useLokaliseTranslation('learning');

  const { mutate: deleteCourse, isLoading } = useDeleteCourse();

  const handleDelete = () => deleteCourse(course, { onSettled: onClose });

  const handleAction =
    (callback: Function) => (event: MouseEvent<HTMLButtonElement>) => {
      event.stopPropagation();
      callback();
    };

  return (
    <Dialog
      onClose={onClose}
      title={t('courses.delete_modal.title', { name: course.title })}
      textBody={t('courses.delete_modal.description')}
      primaryButtonProps={{
        id: 'confirm-delete-course-button',
        loading: isLoading,
        disabled: isLoading,
        children: t('courses.delete_modal.action'),
        onClick: handleAction(handleDelete),
      }}
      secondaryButtonProps={{
        id: 'cancel-delete-course-button',
        variant: 'text',
        disabled: isLoading,
        children: t('general:cancel'),
        onClick: handleAction(onClose),
      }}
    />
  );
};

export default DeleteCourseModal;
