"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = require("react");
var _classnames = _interopRequireDefault(require("classnames"));
var _Dialog = _interopRequireDefault(require("../Dialog"));
var _constants = require("../../constants");
var _Translate = _interopRequireDefault(require("../Translate"));
var _utils = require("../utils");
var _ProgressStep = require("../ProgressStep");
var _Progress = _interopRequireDefault(require("../Progress"));
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

/**
 * `ProgressDialog` is a [Dialog](#!/Dialog) combined with of [Progress](#!/Progress) component.
 * @since 1.2.0
 */
class ProgressDialog extends _react.Component {
  constructor() {
    super(...arguments);
    (0, _defineProperty2.default)(this, "state", {
      isOpen: false,
      isFinished: false,
      isFailed: false
    });
    (0, _defineProperty2.default)(this, "closeTimeout", null);
    (0, _defineProperty2.default)(this, "handleStatusChange", status => {
      this.setState({
        isFinished: status === _ProgressStep.STATUS_DONE,
        isFailed: status === _ProgressStep.STATUS_ERROR
      });
    });
    (0, _defineProperty2.default)(this, "handleClose", () => {
      const {
        onClose,
        onCancel,
        onFinish,
        onFail,
        cancelable
      } = this.props;
      const {
        isOpen,
        isFinished,
        isFailed
      } = this.state;
      this.clearCloseTimeout();
      if (!isOpen) {
        return;
      }
      if (isFinished) {
        (0, _utils.safeInvoke)(onFinish);
      } else if (isFailed) {
        (0, _utils.safeInvoke)(onFail);
      } else if (cancelable) {
        (0, _utils.safeInvoke)(onCancel);
      }
      this.setState({
        isOpen: false
      }, () => (0, _utils.safeInvoke)(onClose));
    });
  }
  static getDerivedStateFromProps(_ref) {
    let {
      isOpen
    } = _ref;
    return {
      isOpen
    };
  }
  componentDidUpdate(_, prevState) {
    const {
      isFinished
    } = this.state;
    const {
      autoCloseTimeout
    } = this.props;
    if (prevState.isFinished === false && isFinished === true) {
      this.clearCloseTimeout();
      if (autoCloseTimeout) {
        this.closeTimeout = setTimeout(this.handleClose, autoCloseTimeout);
      }
    }
  }
  componentWillUnmount() {
    this.clearCloseTimeout();
  }
  clearCloseTimeout() {
    if (!this.closeTimeout) {
      return;
    }
    clearTimeout(this.closeTimeout);
  }
  render() {
    const {
      baseClassName,
      className,
      children,
      description,
      cancelable,
      autoCloseTimeout,
      onCancel,
      onClose,
      onFinish,
      onFail,
      ...props
    } = this.props;
    const {
      isOpen,
      isFinished,
      isFailed
    } = this.state;
    const canClose = isFinished || isFailed;
    const canCancel = cancelable && !canClose;
    const cancelTitle = canClose ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
      content: "ProgressDialog.closeButton",
      fallback: "Close"
    }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
      content: "ProgressDialog.cancelButton",
      fallback: "Cancel"
    });
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Dialog.default, {
      className: (0, _classnames.default)(baseClassName, className),
      size: "xs",
      cancelTitle: cancelTitle,
      ...props,
      isOpen: isOpen,
      onClose: this.handleClose,
      closable: canClose || canCancel,
      canClose: canClose,
      children: [description && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: `${baseClassName}__description`,
        children: description
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Progress.default, {
        className: `${baseClassName}__content`,
        onStatusChange: this.handleStatusChange,
        children: children
      })]
    });
  }
}
(0, _defineProperty2.default)(ProgressDialog, "defaultProps", {
  isOpen: false,
  banner: null,
  image: null,
  title: null,
  description: null,
  actions: null,
  cancelable: false,
  autoCloseTimeout: 3000,
  onCancel: null,
  onClose: null,
  onFinish: null,
  onFail: null,
  children: null,
  className: null,
  baseClassName: `${_constants.CLS_PREFIX}progress-dialog`
});
var _default = exports.default = ProgressDialog;