"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _classnames = _interopRequireDefault(require("classnames"));
var _constants = require("../../constants");
var _Overlay = _interopRequireDefault(require("../Overlay"));
var _ClosingConfirmation = require("../ClosingConfirmation");
var _Form = _interopRequireDefault(require("../Form"));
var _Button = _interopRequireDefault(require("../Button"));
var _Heading = _interopRequireDefault(require("../Heading"));
var _Text = _interopRequireDefault(require("../Text"));
var _Translate = _interopRequireWildcard(require("../Translate"));
var _utils = require("../utils");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const isProps = value => value && typeof value === 'object' && ! /*#__PURE__*/(0, _react.isValidElement)(value);

/**
 * `Dialog` is a modal window that asks users to make a decision or enter additional information.
 * @since 0.0.68
 */
const Dialog = _ref => {
  let {
    baseClassName,
    className,
    children,
    actions,
    isOpen,
    banner,
    image,
    title,
    subtitle,
    cancelTitle,
    cancelButton,
    buttons,
    form,
    size,
    closable,
    onClose,
    closingConfirmation,
    canClose,
    ...props
  } = _ref;
  const [isVisible, setIsVisible] = (0, _react.useState)(isOpen);
  const {
    onCloseWithConfirmation,
    closingConfirmationDialog
  } = (0, _ClosingConfirmation.useClosingConfirmation)({
    onClose: () => {
      setIsVisible(false);
      (0, _utils.safeInvoke)(onClose);
    },
    closingConfirmation
  });
  (0, _react.useEffect)(() => {
    setIsVisible(isOpen);
  }, [isOpen]);
  const renderImage = (image, block) => {
    const classPrefix = `${baseClassName}__${block}`;
    if ( /*#__PURE__*/(0, _react.isValidElement)(image)) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: classPrefix,
        children: image
      });
    }
    if (typeof image === 'string') {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: classPrefix,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("img", {
          alt: "",
          className: `${classPrefix}-inner`,
          src: image
        })
      });
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: classPrefix,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("img", {
        alt: "",
        className: `${classPrefix}-inner`,
        ...image
      })
    });
  };
  const getCancelButtonProps = function () {
    let formProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : form;
    if (!closable) {
      return false;
    }
    if (cancelButton === false || formProps && formProps.cancelButton === false) {
      return false;
    }
    let props = {
      onClick: onCloseWithConfirmation,
      children: cancelTitle || /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
        content: "Dialog.cancelButton",
        fallback: "Cancel"
      })
    };
    if (formProps && isProps(formProps.cancelButton)) {
      props = {
        ...props,
        ...formProps.cancelButton
      };
    }
    if (isProps(cancelButton)) {
      props = {
        ...props,
        ...cancelButton
      };
    }
    return props;
  };
  const renderButtons = () => {
    if (form) {
      return null;
    }
    let cancel;
    const cancelButtonProps = getCancelButtonProps();
    if (cancelButtonProps) {
      cancel = /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
        ...cancelButtonProps
      });
    }
    if (buttons || cancel) {
      return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: `${baseClassName}__buttons`,
        children: [buttons, cancel]
      });
    }
    return null;
  };
  let childrenElement;
  if (form) {
    if ( /*#__PURE__*/(0, _react.isValidElement)(form)) {
      childrenElement = /*#__PURE__*/(0, _react.cloneElement)(form, {
        cancelButton: getCancelButtonProps(form.props),
        footerClassName: (0, _classnames.default)(form.props.footerClassName, `${baseClassName}__form-footer`)
      });
    } else if (typeof form === 'object') {
      form.cancelButton = getCancelButtonProps();
      form.footerClassName = (0, _classnames.default)(form.footerClassName, `${baseClassName}__form-footer`);
      form.applyButton = form.applyButton || false;
      childrenElement = /*#__PURE__*/(0, _jsxRuntime.jsx)(_Form.default, {
        ...form,
        children: children
      });
    }
  } else {
    childrenElement = children;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Overlay.default, {
    className: (0, _classnames.default)(baseClassName, className),
    isOpen: isVisible,
    onClose: onCloseWithConfirmation,
    canCloseOnBackdropClick: canClose && closable,
    canCloseOnEscapePress: canClose && closable,
    size: size,
    ...props,
    children: [(title || subtitle || actions) && /*#__PURE__*/(0, _jsxRuntime.jsxs)("header", {
      className: `${baseClassName}__header`,
      children: [banner && renderImage(banner, 'banner'), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: `${baseClassName}__header-inner`,
        children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          className: `${baseClassName}__header-body`,
          children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
            className: `${baseClassName}__header-content`,
            children: [(0, _Translate.isLikeText)(title) ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Heading.default, {
              level: 2,
              className: `${baseClassName}__title`,
              children: title
            }) : /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
              className: `${baseClassName}__title`,
              children: title
            }), subtitle && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
              className: `${baseClassName}__subtitle`,
              children: (0, _Translate.isLikeText)(subtitle) ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Text.default, {
                intent: "muted",
                children: subtitle
              }) : subtitle
            })]
          }), (actions || closable) && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
            className: `${baseClassName}__header-actions`,
            children: [actions, closable && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
              className: `${baseClassName}__header-close`,
              disabled: !canClose,
              ghost: true,
              size: "lg",
              icon: "cross-mark",
              onClick: onCloseWithConfirmation
            })]
          })]
        })
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: `${baseClassName}__content`,
      children: [image && renderImage(image, 'image'), childrenElement, renderButtons()]
    }), closingConfirmationDialog]
  });
};
Dialog.defaultProps = {
  isOpen: false,
  banner: undefined,
  sideBanner: undefined,
  sideBannerContainer: undefined,
  image: undefined,
  title: undefined,
  subtitle: undefined,
  cancelTitle: undefined,
  cancelButton: undefined,
  buttons: undefined,
  actions: undefined,
  form: undefined,
  size: 'md',
  closable: true,
  canClose: true,
  onClose: undefined,
  closingConfirmation: undefined,
  children: undefined,
  className: undefined,
  baseClassName: `${_constants.CLS_PREFIX}dialog`
};
var _default = exports.default = Dialog;