"use strict";

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

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

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

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

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

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

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"));

// Copyright 1999-2020. 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(...args) {
    super(...args);
    (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;
      clearTimeout(this.closeTimeout);

      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({
    isOpen
  }) {
    return {
      isOpen
    };
  }

  componentDidUpdate(prevProps, prevState) {
    const {
      isFinished
    } = this.state;
    const {
      autoCloseTimeout
    } = this.props;

    if (prevState.isFinished === false && isFinished === true) {
      clearTimeout(this.closeTimeout);

      if (autoCloseTimeout) {
        this.closeTimeout = setTimeout(this.handleClose, autoCloseTimeout);
      }
    }
  }

  componentWillUnmount() {
    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__*/_react.default.createElement(_Translate.default, {
      content: "ProgressDialog.closeButton",
      fallback: "Close"
    }) : /*#__PURE__*/_react.default.createElement(_Translate.default, {
      content: "ProgressDialog.cancelButton",
      fallback: "Cancel"
    });
    return /*#__PURE__*/_react.default.createElement(_Dialog.default, (0, _extends2.default)({
      className: (0, _classnames.default)(baseClassName, className),
      size: "xs",
      cancelTitle: cancelTitle
    }, props, {
      isOpen: isOpen,
      onClose: this.handleClose,
      closable: canClose || canCancel
    }), description && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(baseClassName, "__description")
    }, description), /*#__PURE__*/_react.default.createElement(_Progress.default, {
      className: "".concat(baseClassName, "__content"),
      onStatusChange: this.handleStatusChange
    }, children));
  }

}

ProgressDialog.propTypes = {
  /**
   * Toggles visibility.
   * @since 1.2.0
   */
  isOpen: _propTypes.default.bool,

  /**
   * Banner. Renders in front of content as wide as dialog.
   * @since 1.2.0
   */
  banner: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.object, _propTypes.default.element]),

  /**
   * Image. Renders into content.
   * @since 1.2.0
   */
  image: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.object, _propTypes.default.element]),

  /**
   * Title
   * @since 1.2.0
   */
  title: _propTypes.default.node,

  /**
   * Description of the `ProgressDialog`.
   * @since 1.2.0
   */
  description: _propTypes.default.node,

  /**
   * Actions which will be shown in Dialog header.
   * @since 1.2.0
   */
  actions: _propTypes.default.node,

  /**
   * Whether the dialog should be canceled with the cancel button.
   * @since 1.2.0
   */
  cancelable: _propTypes.default.bool,

  /**
   * The interval of time to auto close dialog when it successful finished.
   * Set `null` for disable auto close.
   * @since 1.2.0
   */
  autoCloseTimeout: _propTypes.default.number,

  /**
   * On cancel event handler.
   * @since 1.2.0
   */
  onCancel: _propTypes.default.func,

  /**
   * On close event handler.
   * @since 1.2.0
   */
  onClose: _propTypes.default.func,

  /**
   * On finish event handler.
   * @since 1.2.0
   */
  onFinish: _propTypes.default.func,

  /**
   * On fail event handler.
   * @since 1.2.0
   */
  onFail: _propTypes.default.func,

  /**
   * Content of the `ProgressDialog`.
   * @since 1.2.0
   */
  children: _propTypes.default.node,

  /**
   * @ignore
   */
  className: _propTypes.default.string,

  /**
   * @ignore
   */
  baseClassName: _propTypes.default.string
};
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: "".concat(_constants.CLS_PREFIX, "progress-dialog")
};
var _default = ProgressDialog;
exports.default = _default;