// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

import { useState } from 'react';
import Button from '../Button';
import Dialog from '../Dialog';
import Paragraph from '../Paragraph';
import Translate from '../Translate';
import { safeInvoke } from '../utils';
import locale from './locale/en-US';
import { jsx as _jsx } from "react/jsx-runtime";
const useClosingConfirmation = _ref => {
  let {
    onClose,
    closingConfirmation
  } = _ref;
  const [isReady, setIsReady] = useState(false);
  const [isDialogVisible, setIsDialogVisible] = useState(false);
  const handleConfirm = () => {
    setIsDialogVisible(false);
    safeInvoke(onClose);
  };
  const handleCancel = () => {
    setIsDialogVisible(false);
  };
  let onCloseWithConfirmation;
  if (typeof onClose === 'function') {
    onCloseWithConfirmation = () => {
      if (closingConfirmation) {
        setIsReady(true);
        setIsDialogVisible(true);
      } else {
        onClose();
      }
    };
  }
  let closingConfirmationDialog = null;
  if (isReady) {
    closingConfirmationDialog = /*#__PURE__*/_jsx(Dialog, {
      isOpen: isDialogVisible,
      size: "sm",
      onClose: handleCancel,
      buttons: [/*#__PURE__*/_jsx(Button, {
        intent: "primary",
        onClick: handleCancel,
        children: /*#__PURE__*/_jsx(Translate, {
          content: "ClosingConfirmation.cancel",
          fallback: locale.cancel
        })
      }, "cancel"), /*#__PURE__*/_jsx(Button, {
        onClick: handleConfirm,
        children: /*#__PURE__*/_jsx(Translate, {
          content: "ClosingConfirmation.confirm",
          fallback: locale.confirm
        })
      }, "confirm")],
      cancelButton: false,
      children: /*#__PURE__*/_jsx(Paragraph, {
        children: /*#__PURE__*/_jsx(Translate, {
          content: "ClosingConfirmation.content",
          fallback: locale.content
        })
      })
    });
  }
  return {
    onCloseWithConfirmation,
    closingConfirmationDialog
  };
};
export default useClosingConfirmation;