"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(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"));

// Copyright 1999-2021. Plesk International GmbH. All rights reserved.
const useClosingConfirmation = ({
  onClose,
  closingConfirmation
}) => {
  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__*/_react.default.createElement(_Dialog.default, {
      isOpen: isDialogVisible,
      size: "sm",
      onClose: handleCancel,
      buttons: [/*#__PURE__*/_react.default.createElement(_Button.default, {
        key: "cancel",
        intent: "primary",
        onClick: handleCancel
      }, /*#__PURE__*/_react.default.createElement(_Translate.default, {
        content: "ClosingConfirmation.cancel",
        fallback: _enUS.default.cancel
      })), /*#__PURE__*/_react.default.createElement(_Button.default, {
        key: "confirm",
        onClick: handleConfirm
      }, /*#__PURE__*/_react.default.createElement(_Translate.default, {
        content: "ClosingConfirmation.confirm",
        fallback: _enUS.default.confirm
      }))],
      cancelButton: false
    }, /*#__PURE__*/_react.default.createElement(_Paragraph.default, null, /*#__PURE__*/_react.default.createElement(_Translate.default, {
      content: "ClosingConfirmation.content",
      fallback: _enUS.default.content
    })));
  }

  return {
    onCloseWithConfirmation,
    closingConfirmationDialog
  };
};

var _default = useClosingConfirmation;
exports.default = _default;