import _defineProperty from "@babel/runtime/helpers/defineProperty";
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

import { Component, Children, isValidElement } from 'react';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import { safeInvoke } from '../utils';
import { STATUS_NOT_STARTED, STATUS_RUNNING, STATUS_DONE, STATUS_ERROR } from '../ProgressStep';
import { jsx as _jsx } from "react/jsx-runtime";
/**
 * `Progress` is used for showing progress of performing long operations.
 * @since 1.10.0
 */
class Progress extends Component {
  constructor() {
    super(...arguments);
    _defineProperty(this, "state", {
      status: null
    });
  }
  static getDerivedStateFromProps(_ref) {
    let {
      children
    } = _ref;
    let isNotStarted = true;
    let isFinished = true;
    let isFailed = false;
    if (children) {
      Children.forEach(children, child => {
        if ( /*#__PURE__*/isValidElement(child) && child.props.status) {
          isNotStarted = isNotStarted && child.props.status === STATUS_NOT_STARTED;
          isFinished = isFinished && child.props.status === STATUS_DONE;
          isFailed = isFailed || child.props.status === STATUS_ERROR;
        }
      });
    }
    if (isFinished) {
      return {
        status: STATUS_DONE
      };
    }
    if (isNotStarted) {
      return {
        status: STATUS_NOT_STARTED
      };
    }
    if (isFailed) {
      return {
        status: STATUS_ERROR
      };
    }
    return {
      status: STATUS_RUNNING
    };
  }
  componentDidMount() {
    const {
      status
    } = this.state;
    const {
      onStatusChange
    } = this.props;
    if (!status) {
      return;
    }
    safeInvoke(onStatusChange, status);
  }
  componentDidUpdate(_, prevState) {
    const {
      status
    } = this.state;
    const {
      onStatusChange
    } = this.props;
    if (prevState.status !== status && status) {
      safeInvoke(onStatusChange, status);
    }
  }
  render() {
    const {
      baseClassName,
      className,
      children,
      onStatusChange,
      ...props
    } = this.props;
    return /*#__PURE__*/_jsx("div", {
      className: classNames(baseClassName, className),
      ...props,
      children: children
    });
  }
}
_defineProperty(Progress, "defaultProps", {
  onStatusChange: null,
  children: null,
  className: null,
  baseClassName: `${CLS_PREFIX}progress`
});
export default Progress;