import { useDialogLayer } from '@material-hu/components/layers/Dialogs';

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

import CertificateStatusDialog from './CertificateStatusDialog';

const getStatusDialogId = (certificateId: number) =>
  `certificate-status-${certificateId}`;

export const useCertificateStatusDialog = () => {
  const { openDialog, closeDialog } = useDialogLayer();

  const openStatusDialog = ({
    certificateId,
    certificateName,
    action,
  }: {
    certificateId: number;
    certificateName: string;
    action: CertificateStatusAction;
  }) => {
    const dialogId = getStatusDialogId(certificateId);
    const handleClose = () => closeDialog(dialogId);

    openDialog(
      {
        content: (
          <CertificateStatusDialog
            certificateId={certificateId}
            certificateName={certificateName}
            action={action}
            onClose={handleClose}
          />
        ),
      },
      dialogId,
    );
  };

  return { openStatusDialog };
};
