"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 _constants = require("../../constants");

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

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

// Copyright 1999-2020. Plesk International GmbH. All rights reserved.

/**
 * `Progress` is used for showing progress of performing long operations.
 * @since 1.10.0
 */
class Progress extends _react.Component {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2.default)(this, "state", {
      status: null
    });
  }

  static getDerivedStateFromProps({
    children
  }) {
    let isNotStarted = true;
    let isFinished = true;
    let isFailed = false;

    if (children) {
      _react.Children.forEach(children, child => {
        if ( /*#__PURE__*/(0, _react.isValidElement)(child) && child.props.status) {
          isNotStarted = isNotStarted && child.props.status === _ProgressStep.STATUS_NOT_STARTED;
          isFinished = isFinished && child.props.status === _ProgressStep.STATUS_DONE;
          isFailed = isFailed || child.props.status === _ProgressStep.STATUS_ERROR;
        }
      });
    }

    if (isFinished) {
      return {
        status: _ProgressStep.STATUS_DONE
      };
    }

    if (isNotStarted) {
      return {
        status: _ProgressStep.STATUS_NOT_STARTED
      };
    }

    if (isFailed) {
      return {
        status: _ProgressStep.STATUS_ERROR
      };
    }

    return {
      status: _ProgressStep.STATUS_RUNNING
    };
  }

  componentDidMount() {
    const {
      status
    } = this.state;
    const {
      onStatusChange
    } = this.props;
    (0, _utils.safeInvoke)(onStatusChange, status);
  }

  componentDidUpdate(prevProps, prevState) {
    const {
      status
    } = this.state;
    const {
      onStatusChange
    } = this.props;

    if (prevState.status !== status) {
      (0, _utils.safeInvoke)(onStatusChange, status);
    }
  }

  render() {
    const {
      baseClassName,
      className,
      children,
      onStatusChange,
      ...props
    } = this.props;
    return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
      className: (0, _classnames.default)(baseClassName, className)
    }, props), children);
  }

}

Progress.propTypes = {
  /**
   * On overall status event handler.
   * @since 1.10.0
   */
  onStatusChange: _propTypes.default.func,

  /**
   * Content of the `Progress`.
   * @since 1.10.0
   */
  children: _propTypes.default.node,

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

  /**
   * @ignore
   */
  baseClassName: _propTypes.default.string
};
Progress.defaultProps = {
  onStatusChange: null,
  children: null,
  className: null,
  baseClassName: "".concat(_constants.CLS_PREFIX, "progress")
};
var _default = Progress;
exports.default = _default;