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

import { Component, Fragment } from 'react';
import Button from '../Button';
import { ButtonContext, getButtonContextValue } from '../Button/ButtonContext';
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';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
const additionalButtonsConfig = getButtonContextValue({
  defaultProps: {
    size: 'lg'
  }
});
class DrawerProgress extends Component {
  constructor() {
    super(...arguments);
    _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 !== undefined) {
        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,
      render
    } = this.props;
    const {
      status
    } = this.state;
    const isFinished = status === STATUS_DONE;
    const isFailed = status === STATUS_ERROR;
    const canCancel = cancelable && !(isFinished || isFailed);
    const hasFooter = !!(buttons || canCancel);
    const content = /*#__PURE__*/_jsxs(Fragment, {
      children: [title && /*#__PURE__*/_jsx("div", {
        className: `${baseClassName}__title`,
        children: title
      }), /*#__PURE__*/_jsx(Progress, {
        onStatusChange: this.handleStatusChange,
        className: `${baseClassName}__progress`,
        children: steps.map((step, index) =>
        /*#__PURE__*/
        // eslint-disable-next-line react/no-array-index-key
        _jsx(ProgressStep, {
          ...step
        }, index.toString()))
      })]
    });
    const footer = hasFooter ? /*#__PURE__*/_jsxs("div", {
      className: `${baseClassName}__footer`,
      children: [/*#__PURE__*/_jsx(ButtonContext.Provider, {
        value: additionalButtonsConfig,
        children: buttons
      }), canCancel && /*#__PURE__*/_jsx(Button, {
        onClick: this.handleCancel,
        size: "lg",
        children: /*#__PURE__*/_jsx(Translate, {
          content: "DrawerProgress.cancelButton",
          fallback: "Cancel"
        })
      })]
    }) : null;
    return typeof render === 'function' ? render({
      content,
      footer
    }) : /*#__PURE__*/_jsxs(Fragment, {
      children: [content, footer]
    });
  }
}
_defineProperty(DrawerProgress, "defaultProps", {
  baseClassName: `${CLS_PREFIX}drawer-progress`,
  autoCloseTimeout: 0,
  cancelable: false,
  steps: []
});
export default DrawerProgress;