"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 _reactDom = require("react-dom");
var _react = require("react");
var _reactTransitionGroup = require("react-transition-group");
var _classnames = _interopRequireDefault(require("classnames"));
var _constants = require("../../constants");
var _Toast = _interopRequireDefault(require("../Toast"));
var _Layer = _interopRequireDefault(require("../Layer"));
var _jsxRuntime = require("react/jsx-runtime");
var _class; // Copyright 1999-2023. Plesk International GmbH. All rights reserved.
let toastId = 0;
/**
 * `Toaster` is used for notifying about events happened in the system. For example, about results of performed operations.
 * @since 0.0.42
 */
class Toaster extends _react.Component {
  constructor() {
    super(...arguments);
    (0, _defineProperty2.default)(this, "state", {
      toasts: []
    });
  }
  static getDerivedStateFromProps(_ref) {
    let {
      toasts,
      maxToastsNumber
    } = _ref;
    if (toasts) {
      return {
        toasts: toasts.slice(0, maxToastsNumber)
      };
    }
    return null;
  }
  isControlled() {
    return Boolean(this.props.toasts);
  }

  /**
   * Add a new toast
   *
   * `intent` - Visual intent color to apply to component. (Type: `oneOf` 'info', 'success', 'warning', 'danger'. *Required*.)<br>
   * `accent` - Contrast (light) version of the component. (Type: `boolean`. Default: 'undefined'.)<br>
   * `icon` - Name of icon or [Icon](#!/Icon) component. (Type: `oneOfType` 'string', 'object', 'element'. Default: `undefined`.)<br>
   * `message` - Message to display in the body of the toast. (Type: `any`. Default: `undefined`.)<br>
   * `onClose` - onClose handler. (Type: `func`. Default: `undefined`.)<br>
   * `closable` - Show close control (cross mark). (Type: `boolean`. Default: `true`.)<br>
   * `autoClosable` - Whether call `onClose` after 6 seconds or not.
   * Enabled by default when the position is "top-end" and a toast has "success" intent. (Type: `boolean`. Default: 'undefined'.)<br>
   * ---
   * @param {Object} props **Toast props**:
   * @returns {string} New toast key
   * @public
   */
  add(props) {
    const key = `toast-${toastId++}`;
    this.setState(_ref2 => {
      let {
        toasts
      } = _ref2;
      return {
        toasts: [{
          key,
          ...props
        }, ...toasts].slice(0, this.props.maxToastsNumber)
      };
    });
    return key;
  }

  /**
   * Update toast by key
   *
   * @param {string} key
   * @param {Object} props New props
   * @returns {string} Updated toast key
   * @public
   */
  update(key, props) {
    this.setState(_ref3 => {
      let {
        toasts
      } = _ref3;
      return {
        toasts: toasts.map(toast => toast.key === key ? {
          ...props,
          key
        } : toast)
      };
    });
    return key;
  }

  /**
   * Remove toast by key
   *
   * @param {string} key
   * @public
   */
  remove(key) {
    this.setState(_ref4 => {
      let {
        toasts
      } = _ref4;
      return {
        toasts: toasts.filter(toast => toast.key !== key)
      };
    });
  }

  /**
   * Clear all toasts
   *
   * @public
   */
  clear() {
    this.setState({
      toasts: []
    });
  }
  render() {
    const {
      baseClassName,
      className,
      toasts,
      maxToastsNumber,
      position,
      onToastClose,
      ...props
    } = this.props;
    let stack = this.state.toasts;
    if (position === 'bottom-end') {
      stack = [...stack];
      stack.reverse();
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Layer.default, {
      level: _constants.Z_INDEX_TOASTER,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: (0, _classnames.default)(baseClassName, {
          [`${baseClassName}--${position}`]: position
        }, className),
        ...props,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactTransitionGroup.TransitionGroup, {
          component: "span",
          children: stack.map(toastProps => {
            const {
              intent
            } = toastProps;
            let {
              autoClosable
            } = toastProps;
            if (autoClosable === undefined && intent === 'success' && position === 'top-end') {
              autoClosable = true;
            }
            const toastRef = /*#__PURE__*/(0, _react.createRef)();
            return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactTransitionGroup.CSSTransition, {
              classNames: `${_Toast.default.defaultProps.baseClassName}-`,
              timeout: {
                enter: 300,
                exit: 300
              },
              nodeRef: toastRef,
              children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Toast.default, {
                ...toastProps,
                autoClosable: autoClosable,
                onClose: () => {
                  if (!this.isControlled()) {
                    this.remove(toastProps.key);
                  }
                  if (toastProps.onClose) {
                    toastProps.onClose();
                  }
                  if (onToastClose) {
                    onToastClose(toastProps.key);
                  }
                },
                innerRef: toastRef
              })
            }, toastProps.key);
          })
        })
      })
    });
  }
}
exports.default = Toaster;
_class = Toaster;
// eslint-disable-next-line react/sort-comp
(0, _defineProperty2.default)(Toaster, "defaultProps", {
  position: 'top-end',
  baseClassName: `${_constants.CLS_PREFIX}toaster`
});
(0, _defineProperty2.default)(Toaster, "create", function () {
  let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  let container = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document.body;
  const containerElement = document.createElement('div');
  container.appendChild(containerElement);
  return (0, _reactDom.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_class, {
    ...props
  }), containerElement);
});