"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 _Button = _interopRequireDefault(require("../Button"));

var _Translate = _interopRequireDefault(require("../Translate"));

var _Progress = _interopRequireDefault(require("../Progress"));

var _ProgressStep = _interopRequireWildcard(require("../ProgressStep"));

var _constants = require("../../constants");

var _utils = require("../utils");

// Copyright 1999-2019. Plesk International GmbH. All rights reserved.
class DrawerProgress extends _react.Component {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2.default)(this, "state", {
      status: null
    });
    (0, _defineProperty2.default)(this, "closeTimeout", void 0);
    (0, _defineProperty2.default)(this, "handleStatusChange", status => {
      const {
        onClosableChange,
        onFinish,
        onFail,
        cancelable,
        onStatusChange
      } = this.props;
      const isFinished = status === _ProgressStep.STATUS_DONE;
      const isFailed = status === _ProgressStep.STATUS_ERROR;
      (0, _utils.safeInvoke)(onStatusChange, status);
      this.setState({
        status
      });
      (0, _utils.safeInvoke)(onClosableChange, cancelable || isFinished || isFailed);

      if (isFinished) {
        (0, _utils.safeInvoke)(onFinish);
      } else if (isFailed) {
        (0, _utils.safeInvoke)(onFail);
      }
    });
    (0, _defineProperty2.default)(this, "handleClose", () => {
      const {
        onClose,
        onCancel,
        cancelable
      } = this.props;
      window.clearTimeout(this.closeTimeout);

      if (cancelable) {
        (0, _utils.safeInvoke)(onCancel);
      }

      (0, _utils.safeInvoke)(onClose);
    });
    (0, _defineProperty2.default)(this, "handleCancel", () => {
      const {
        onCancel,
        onClose,
        cancelable
      } = this.props;
      window.clearTimeout(this.closeTimeout);

      if (cancelable) {
        (0, _utils.safeInvoke)(onCancel);
      }

      (0, _utils.safeInvoke)(onClose);
    });
  }

  componentDidUpdate(_prevProps, prevState) {
    const {
      autoCloseTimeout,
      onStart
    } = this.props;
    const {
      status
    } = this.state;

    if (prevState.status !== _ProgressStep.STATUS_DONE && status === _ProgressStep.STATUS_DONE) {
      window.clearTimeout(this.closeTimeout);

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

    if (prevState.status === _ProgressStep.STATUS_NOT_STARTED && status !== _ProgressStep.STATUS_NOT_STARTED) {
      (0, _utils.safeInvoke)(onStart);
    }
  }

  componentWillUnmount() {
    window.clearTimeout(this.closeTimeout);
  }

  render() {
    const {
      baseClassName,
      title,
      steps,
      buttons,
      cancelable
    } = this.props;
    const {
      status
    } = this.state;
    const isFinished = status === _ProgressStep.STATUS_DONE;
    const isFailed = status === _ProgressStep.STATUS_ERROR;
    const canCancel = cancelable && !(isFinished || isFailed);
    /* eslint-disable react/jsx-indent */

    return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, title && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(baseClassName, "__title")
    }, title), /*#__PURE__*/_react.default.createElement(_Progress.default, {
      onStatusChange: this.handleStatusChange,
      className: "".concat(baseClassName, "__progress")
    }, steps.map((step, index) => /*#__PURE__*/_react.default.createElement(_ProgressStep.default, (0, _extends2.default)({
      key: index.toString()
    }, step)))), /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(baseClassName, "__footer")
    }, buttons, (isFinished || isFailed) && /*#__PURE__*/_react.default.createElement(_Button.default, {
      onClick: this.handleClose
    }, /*#__PURE__*/_react.default.createElement(_Translate.default, {
      content: "DrawerProgress.closeButton",
      fallback: "Close"
    })), canCancel && /*#__PURE__*/_react.default.createElement(_Button.default, {
      onClick: this.handleCancel
    }, /*#__PURE__*/_react.default.createElement(_Translate.default, {
      content: "DrawerProgress.cancelButton",
      fallback: "Cancel"
    }))));
  }

}

(0, _defineProperty2.default)(DrawerProgress, "defaultProps", {
  baseClassName: "".concat(_constants.CLS_PREFIX, "drawer-progress"),
  autoCloseTimeout: 3000,
  cancelable: false,
  steps: []
});
var _default = DrawerProgress;
exports.default = _default;