import { routes } from '@/app';
import { CardTab } from '@/modules/card';
import { WarningModal } from '@/shared';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { useDeleteEntityRentalProductOrder } from '../../../../../../../../../api';

interface Props {
  opened: boolean;
  orderId: number;
  sectionId: number;
  entityId: number;
  entityTypeId: number;
  onClose: () => void;
  onCancel: () => void;
}

const DeleteRentalOrderOrClearItemsWarningModal = (props: Props) => {
  const { opened, orderId, sectionId, entityId, entityTypeId, onClose, onCancel } = props;

  const { t } = useTranslation('module.products', {
    keyPrefix: 'products.components.common.delete_order_or_clear_items_warning_modal',
  });

  const navigate = useNavigate();

  const { mutateAsync, isPending } = useDeleteEntityRentalProductOrder({
    orderId,
    sectionId,
    entityId,
  });

  const handleDeleteOrder = async (): Promise<void> => {
    await mutateAsync();
    onClose();

    navigate(routes.card({ entityTypeId, entityId, tab: CardTab.ORDERS }));
  };

  return (
    <WarningModal
      width="464px"
      icon="trashbin"
      maxHeight="356px"
      isOpened={opened}
      title={t('title')}
      cancelOnClose={false}
      cancelDisabled={isPending}
      approveLoading={isPending}
      approveDisabled={isPending}
      annotation={t('annotation')}
      cancelTitle={t('cancel_title')}
      approveTitle={t('approve_title')}
      onClose={onClose}
      onCancel={onCancel}
      onApprove={handleDeleteOrder}
    />
  );
};

export { DeleteRentalOrderOrClearItemsWarningModal };
