"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _Button = _interopRequireDefault(require("../Button"));
var _Dialog = _interopRequireDefault(require("../Dialog"));
var _Paragraph = _interopRequireDefault(require("../Paragraph"));
var _Translate = _interopRequireDefault(require("../Translate"));
var _utils = require("../utils");
var _enUS = _interopRequireDefault(require("./locale/en-US"));
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const useClosingConfirmation = _ref => {
  let {
    onClose,
    closingConfirmation
  } = _ref;
  const [isReady, setIsReady] = (0, _react.useState)(false);
  const [isDialogVisible, setIsDialogVisible] = (0, _react.useState)(false);
  const handleConfirm = () => {
    setIsDialogVisible(false);
    (0, _utils.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__*/(0, _jsxRuntime.jsx)(_Dialog.default, {
      isOpen: isDialogVisible,
      size: "sm",
      onClose: handleCancel,
      buttons: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
        intent: "primary",
        onClick: handleCancel,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
          content: "ClosingConfirmation.cancel",
          fallback: _enUS.default.cancel
        })
      }, "cancel"), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
        onClick: handleConfirm,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
          content: "ClosingConfirmation.confirm",
          fallback: _enUS.default.confirm
        })
      }, "confirm")],
      cancelButton: false,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Paragraph.default, {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
          content: "ClosingConfirmation.content",
          fallback: _enUS.default.content
        })
      })
    });
  }
  return {
    onCloseWithConfirmation,
    closingConfirmationDialog
  };
};
var _default = exports.default = useClosingConfirmation;