"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = require("react");
var _classnames = _interopRequireDefault(require("classnames"));
var _constants = require("../../constants");
var _utils = require("../utils");
var _ProgressStep = require("../ProgressStep");
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. 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() {
    super(...arguments);
    (0, _defineProperty2.default)(this, "state", {
      status: null
    });
  }
  static getDerivedStateFromProps(_ref) {
    let {
      children
    } = _ref;
    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;
    if (!status) {
      return;
    }
    (0, _utils.safeInvoke)(onStatusChange, status);
  }
  componentDidUpdate(_, prevState) {
    const {
      status
    } = this.state;
    const {
      onStatusChange
    } = this.props;
    if (prevState.status !== status && status) {
      (0, _utils.safeInvoke)(onStatusChange, status);
    }
  }
  render() {
    const {
      baseClassName,
      className,
      children,
      onStatusChange,
      ...props
    } = this.props;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames.default)(baseClassName, className),
      ...props,
      children: children
    });
  }
}
(0, _defineProperty2.default)(Progress, "defaultProps", {
  onStatusChange: null,
  children: null,
  className: null,
  baseClassName: `${_constants.CLS_PREFIX}progress`
});
var _default = exports.default = Progress;