import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
import React, { Component } from 'react';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import Icon from '../Icon';
import Button from '../Button';
import { intentIconMap } from '../intentIconMap';
const AUTOCLOSE_TIMEOUT = 6000;
export default class Toast extends Component {
  constructor(...args) {
    super(...args);

    _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,
      onClose,
      closable = true,
      autoClosable,
      autoCloseTimeout,
      innerRef,
      ...props
    } = this.props;
    return /*#__PURE__*/React.createElement("div", _extends({
      tabIndex: 0,
      className: classNames(baseClassName, intent && `${baseClassName}--${intent}`, className)
    }, props, {
      ref: innerRef
    }), intent ? /*#__PURE__*/React.createElement("span", {
      className: `${baseClassName}__icon`
    }, /*#__PURE__*/React.createElement(Icon, {
      name: intentIconMap[intent]
    })) : null, /*#__PURE__*/React.createElement("span", {
      className: `${baseClassName}__content`
    }, message), closable ? /*#__PURE__*/React.createElement("span", {
      className: `${baseClassName}__action`
    }, /*#__PURE__*/React.createElement(Button, {
      ghost: true,
      icon: {
        name: 'cross-mark',
        size: '12'
      },
      className: `${baseClassName}__close`,
      onClick: onClose
    })) : null);
  }

}

_defineProperty(Toast, "defaultProps", {
  baseClassName: `${CLS_PREFIX}toast`
});