import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
// Copyright 1999-2019. Plesk International GmbH. All rights reserved.
import React, { Component, Fragment } from 'react';
import Button from '../Button';
import Translate from '../Translate';
import Progress from '../Progress';
import ProgressStep, { STATUS_DONE, STATUS_ERROR, STATUS_NOT_STARTED } from '../ProgressStep';
import { CLS_PREFIX } from '../../constants';
import { safeInvoke } from '../utils';

class DrawerProgress extends Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "state", {
      status: null
    });

    _defineProperty(this, "closeTimeout", void 0);

    _defineProperty(this, "handleStatusChange", status => {
      const {
        onClosableChange,
        onFinish,
        onFail,
        cancelable,
        onStatusChange
      } = this.props;
      const isFinished = status === STATUS_DONE;
      const isFailed = status === STATUS_ERROR;
      safeInvoke(onStatusChange, status);
      this.setState({
        status
      });
      safeInvoke(onClosableChange, cancelable || isFinished || isFailed);

      if (isFinished) {
        safeInvoke(onFinish);
      } else if (isFailed) {
        safeInvoke(onFail);
      }
    });

    _defineProperty(this, "handleClose", () => {
      const {
        onClose,
        onCancel,
        cancelable
      } = this.props;
      window.clearTimeout(this.closeTimeout);

      if (cancelable) {
        safeInvoke(onCancel);
      }

      safeInvoke(onClose);
    });

    _defineProperty(this, "handleCancel", () => {
      const {
        onCancel,
        onClose,
        cancelable
      } = this.props;
      window.clearTimeout(this.closeTimeout);

      if (cancelable) {
        safeInvoke(onCancel);
      }

      safeInvoke(onClose);
    });
  }

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

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

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

    if (prevState.status === STATUS_NOT_STARTED && status !== STATUS_NOT_STARTED) {
      safeInvoke(onStart);
    }
  }

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

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

    return /*#__PURE__*/React.createElement(Fragment, null, title && /*#__PURE__*/React.createElement("div", {
      className: "".concat(baseClassName, "__title")
    }, title), /*#__PURE__*/React.createElement(Progress, {
      onStatusChange: this.handleStatusChange,
      className: "".concat(baseClassName, "__progress")
    }, steps.map((step, index) => /*#__PURE__*/React.createElement(ProgressStep, _extends({
      key: index.toString()
    }, step)))), /*#__PURE__*/React.createElement("div", {
      className: "".concat(baseClassName, "__footer")
    }, buttons, (isFinished || isFailed) && /*#__PURE__*/React.createElement(Button, {
      onClick: this.handleClose
    }, /*#__PURE__*/React.createElement(Translate, {
      content: "DrawerProgress.closeButton",
      fallback: "Close"
    })), canCancel && /*#__PURE__*/React.createElement(Button, {
      onClick: this.handleCancel
    }, /*#__PURE__*/React.createElement(Translate, {
      content: "DrawerProgress.cancelButton",
      fallback: "Cancel"
    }))));
  }

}

_defineProperty(DrawerProgress, "defaultProps", {
  baseClassName: "".concat(CLS_PREFIX, "drawer-progress"),
  autoCloseTimeout: 3000,
  cancelable: false,
  steps: []
});

export default DrawerProgress;