import _defineProperty from "@babel/runtime/helpers/defineProperty";
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

import { Component } from 'react';
import classNames from 'classnames';
import Dialog from '../Dialog';
import { CLS_PREFIX } from '../../constants';
import Translate from '../Translate';
import { safeInvoke } from '../utils';
import { STATUS_DONE, STATUS_ERROR } from '../ProgressStep';
import Progress from '../Progress';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
/**
 * `ProgressDialog` is a [Dialog](#!/Dialog) combined with of [Progress](#!/Progress) component.
 * @since 1.2.0
 */
class ProgressDialog extends Component {
  constructor() {
    super(...arguments);
    _defineProperty(this, "state", {
      isOpen: false,
      isFinished: false,
      isFailed: false
    });
    _defineProperty(this, "closeTimeout", null);
    _defineProperty(this, "handleStatusChange", status => {
      this.setState({
        isFinished: status === STATUS_DONE,
        isFailed: status === STATUS_ERROR
      });
    });
    _defineProperty(this, "handleClose", () => {
      const {
        onClose,
        onCancel,
        onFinish,
        onFail,
        cancelable
      } = this.props;
      const {
        isOpen,
        isFinished,
        isFailed
      } = this.state;
      this.clearCloseTimeout();
      if (!isOpen) {
        return;
      }
      if (isFinished) {
        safeInvoke(onFinish);
      } else if (isFailed) {
        safeInvoke(onFail);
      } else if (cancelable) {
        safeInvoke(onCancel);
      }
      this.setState({
        isOpen: false
      }, () => 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__*/_jsx(Translate, {
      content: "ProgressDialog.closeButton",
      fallback: "Close"
    }) : /*#__PURE__*/_jsx(Translate, {
      content: "ProgressDialog.cancelButton",
      fallback: "Cancel"
    });
    return /*#__PURE__*/_jsxs(Dialog, {
      className: classNames(baseClassName, className),
      size: "xs",
      cancelTitle: cancelTitle,
      ...props,
      isOpen: isOpen,
      onClose: this.handleClose,
      closable: canClose || canCancel,
      canClose: canClose,
      children: [description && /*#__PURE__*/_jsx("div", {
        className: `${baseClassName}__description`,
        children: description
      }), /*#__PURE__*/_jsx(Progress, {
        className: `${baseClassName}__content`,
        onStatusChange: this.handleStatusChange,
        children: children
      })]
    });
  }
}
_defineProperty(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: `${CLS_PREFIX}progress-dialog`
});
export default ProgressDialog;