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

import { Component } from 'react';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import Icon from '../Icon';
import Button from '../Button';
import { intentIconMap } from '../intentIconMap';
import OnDarkContext from '../OnDarkContext';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
const AUTOCLOSE_TIMEOUT = 6000;
export default class Toast extends Component {
  constructor() {
    super(...arguments);
    _defineProperty(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__*/_jsx(Icon, {
        name: intentIconMap[intent]
      }),
      onClose,
      closable = true,
      autoClosable,
      autoCloseTimeout,
      innerRef,
      ...props
    } = this.props;
    const result = /*#__PURE__*/_jsxs("div", {
      tabIndex: 0,
      className: classNames(baseClassName, intent && `${baseClassName}--${intent}`, accent && `${baseClassName}--accent`, className),
      ...props,
      ref: innerRef,
      children: [icon ? /*#__PURE__*/_jsx("div", {
        className: `${baseClassName}__icon`,
        children: Icon.create(icon, {
          intent
        })
      }) : null, /*#__PURE__*/_jsx("div", {
        className: `${baseClassName}__content`,
        children: message
      }), closable ? /*#__PURE__*/_jsx("div", {
        className: `${baseClassName}__action`,
        children: /*#__PURE__*/_jsx(Button, {
          ghost: true,
          icon: {
            name: 'cross-mark',
            size: '12'
          },
          className: `${baseClassName}__close`,
          onClick: onClose
        })
      }) : null]
    });
    if (accent) {
      return result;
    }
    return /*#__PURE__*/_jsx(OnDarkContext.Provider, {
      value: true,
      children: result
    });
  }
}
_defineProperty(Toast, "defaultProps", {
  baseClassName: `${CLS_PREFIX}toast`
});