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

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

import CertificateInUseDialog from './CertificateInUseDialog';

const getInUseDialogId = (certificateId: number) =>
  `certificate-in-use-${certificateId}`;

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

  const openInUseDialog = ({
    certificateId,
    certificateName,
    action,
  }: {
    certificateId: number;
    certificateName: string;
    action: CertificateInUseAction;
  }) => {
    const dialogId = getInUseDialogId(certificateId);
    const handleClose = () => closeDialog(dialogId);

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

  return { openInUseDialog };
};
