"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 _Icon = _interopRequireDefault(require("../Icon"));
var _Button = _interopRequireDefault(require("../Button"));
var _intentIconMap = require("../intentIconMap");
var _OnDarkContext = _interopRequireDefault(require("../OnDarkContext"));
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const AUTOCLOSE_TIMEOUT = 6000;
class Toast extends _react.Component {
  constructor() {
    super(...arguments);
    (0, _defineProperty2.default)(this, "timer", void 0);
  }
  componentDidMount() {
    const {
      autoClosable,
      onClose
    } = this.props;
    let {
      autoCloseTimeout
    } = this.props;
    switch (autoClosable) {
      case true:
        autoCloseTimeout = AUTOCLOSE_TIMEOUT;
        break;
      case false:
        autoCloseTimeout = undefined;
        break;
    }
    if (autoCloseTimeout && onClose) {
      this.timer = window.setTimeout(onClose, autoCloseTimeout);
    }
  }
  componentWillUnmount() {
    if (this.timer) {
      clearTimeout(this.timer);
    }
  }
  render() {
    const {
      baseClassName,
      className,
      message,
      intent,
      accent,
      icon = intent && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.default, {
        name: _intentIconMap.intentIconMap[intent]
      }),
      onClose,
      closable = true,
      autoClosable,
      autoCloseTimeout,
      innerRef,
      ...props
    } = this.props;
    const result = /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      tabIndex: 0,
      className: (0, _classnames.default)(baseClassName, intent && `${baseClassName}--${intent}`, accent && `${baseClassName}--accent`, className),
      ...props,
      ref: innerRef,
      children: [icon ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: `${baseClassName}__icon`,
        children: _Icon.default.create(icon, {
          intent
        })
      }) : null, /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: `${baseClassName}__content`,
        children: message
      }), closable ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: `${baseClassName}__action`,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
          ghost: true,
          icon: {
            name: 'cross-mark',
            size: '12'
          },
          className: `${baseClassName}__close`,
          onClick: onClose
        })
      }) : null]
    });
    if (accent) {
      return result;
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_OnDarkContext.default.Provider, {
      value: true,
      children: result
    });
  }
}
exports.default = Toast;
(0, _defineProperty2.default)(Toast, "defaultProps", {
  baseClassName: `${_constants.CLS_PREFIX}toast`
});