"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 _Button = _interopRequireDefault(require("../Button"));
var _ButtonContext = require("../Button/ButtonContext");
var _Translate = _interopRequireDefault(require("../Translate"));
var _Progress = _interopRequireDefault(require("../Progress"));
var _ProgressStep = _interopRequireWildcard(require("../ProgressStep"));
var _constants = require("../../constants");
var _utils = require("../utils");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const additionalButtonsConfig = (0, _ButtonContext.getButtonContextValue)({
  defaultProps: {
    size: 'lg'
  }
});
class DrawerProgress extends _react.Component {
  constructor() {
    super(...arguments);
    (0, _defineProperty2.default)(this, "state", {
      status: null
    });
    (0, _defineProperty2.default)(this, "closeTimeout", void 0);
    (0, _defineProperty2.default)(this, "handleStatusChange", status => {
      const {
        onClosableChange,
        onFinish,
        onFail,
        cancelable,
        onStatusChange
      } = this.props;
      const isFinished = status === _ProgressStep.STATUS_DONE;
      const isFailed = status === _ProgressStep.STATUS_ERROR;
      (0, _utils.safeInvoke)(onStatusChange, status);
      this.setState({
        status
      });
      (0, _utils.safeInvoke)(onClosableChange, cancelable || isFinished || isFailed);
      if (isFinished) {
        (0, _utils.safeInvoke)(onFinish);
      } else if (isFailed) {
        (0, _utils.safeInvoke)(onFail);
      }
    });
    (0, _defineProperty2.default)(this, "handleClose", () => {
      const {
        onClose,
        onCancel,
        cancelable
      } = this.props;
      window.clearTimeout(this.closeTimeout);
      if (cancelable) {
        (0, _utils.safeInvoke)(onCancel);
      }
      (0, _utils.safeInvoke)(onClose);
    });
    (0, _defineProperty2.default)(this, "handleCancel", () => {
      const {
        onCancel,
        onClose,
        cancelable
      } = this.props;
      window.clearTimeout(this.closeTimeout);
      if (cancelable) {
        (0, _utils.safeInvoke)(onCancel);
      }
      (0, _utils.safeInvoke)(onClose);
    });
  }
  componentDidUpdate(_prevProps, prevState) {
    const {
      autoCloseTimeout,
      onStart
    } = this.props;
    const {
      status
    } = this.state;
    if (prevState.status !== _ProgressStep.STATUS_DONE && status === _ProgressStep.STATUS_DONE) {
      window.clearTimeout(this.closeTimeout);
      if (autoCloseTimeout !== undefined) {
        this.closeTimeout = window.setTimeout(this.handleClose, autoCloseTimeout);
      }
    }
    if (prevState.status === _ProgressStep.STATUS_NOT_STARTED && status !== _ProgressStep.STATUS_NOT_STARTED) {
      (0, _utils.safeInvoke)(onStart);
    }
  }
  componentWillUnmount() {
    window.clearTimeout(this.closeTimeout);
  }
  render() {
    const {
      baseClassName,
      title,
      steps,
      buttons,
      cancelable,
      render
    } = this.props;
    const {
      status
    } = this.state;
    const isFinished = status === _ProgressStep.STATUS_DONE;
    const isFailed = status === _ProgressStep.STATUS_ERROR;
    const canCancel = cancelable && !(isFinished || isFailed);
    const hasFooter = !!(buttons || canCancel);
    const content = /*#__PURE__*/(0, _jsxRuntime.jsxs)(_react.Fragment, {
      children: [title && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: `${baseClassName}__title`,
        children: title
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Progress.default, {
        onStatusChange: this.handleStatusChange,
        className: `${baseClassName}__progress`,
        children: steps.map((step, index) =>
        /*#__PURE__*/
        // eslint-disable-next-line react/no-array-index-key
        (0, _jsxRuntime.jsx)(_ProgressStep.default, {
          ...step
        }, index.toString()))
      })]
    });
    const footer = hasFooter ? /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: `${baseClassName}__footer`,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_ButtonContext.ButtonContext.Provider, {
        value: additionalButtonsConfig,
        children: buttons
      }), canCancel && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
        onClick: this.handleCancel,
        size: "lg",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Translate.default, {
          content: "DrawerProgress.cancelButton",
          fallback: "Cancel"
        })
      })]
    }) : null;
    return typeof render === 'function' ? render({
      content,
      footer
    }) : /*#__PURE__*/(0, _jsxRuntime.jsxs)(_react.Fragment, {
      children: [content, footer]
    });
  }
}
(0, _defineProperty2.default)(DrawerProgress, "defaultProps", {
  baseClassName: `${_constants.CLS_PREFIX}drawer-progress`,
  autoCloseTimeout: 0,
  cancelable: false,
  steps: []
});
var _default = exports.default = DrawerProgress;