import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import IconButton from '../iconButton';
import Link from '../link';
import { TextBody } from '../typography';
import LoadingSpinner from '../loadingSpinner';
import { IconCloseMediumOutline } from '@teamleader/ui-icons';
import theme from './theme.css';
import uiUtilities from '@teamleader/ui-utilities';
/** @type {React.ComponentType<any>} */

var Toast = /*#__PURE__*/function (_PureComponent) {
  _inherits(Toast, _PureComponent);

  var _super = _createSuper(Toast);

  function Toast() {
    var _this;

    _classCallCheck(this, Toast);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));

    _defineProperty(_assertThisInitialized(_this), "scheduleTimeout", function (props) {
      var onTimeout = props.onTimeout,
          timeout = props.timeout;

      if (_this.currentTimeout) {
        clearTimeout(_this.currentTimeout);
      }

      _this.currentTimeout = setTimeout(function () {
        if (onTimeout) {
          onTimeout();
        }

        _this.currentTimeout = null;
      }, timeout);
    });

    _defineProperty(_assertThisInitialized(_this), "renderCustomAction", function () {
      var _this$props = _this.props,
          action = _this$props.action,
          actionLabel = _this$props.actionLabel;
      return action && /*#__PURE__*/React.createElement(Link, {
        className: theme['action-link'],
        onClick: action
      }, actionLabel);
    });

    _defineProperty(_assertThisInitialized(_this), "renderCustomLink", function () {
      var link = _this.props.link;
      return link && /*#__PURE__*/React.createElement(TextBody, null, /*#__PURE__*/React.cloneElement(link, {
        className: cx(link.props.className, theme['action-link'])
      }));
    });

    _defineProperty(_assertThisInitialized(_this), "renderCloseButton", function () {
      var onClose = _this.props.onClose;
      return onClose && /*#__PURE__*/React.createElement(IconButton, {
        className: theme['action-button'],
        icon: /*#__PURE__*/React.createElement(IconCloseMediumOutline, null),
        color: "white",
        onClick: onClose
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleMouseEnter", function () {
      if (_this.props.timeout) {
        clearTimeout(_this.currentTimeout);
        _this.currentTimeout = null;
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleMouseLeave", function () {
      if (_this.props.timeout) {
        _this.scheduleTimeout(_this.props);
      }
    });

    return _this;
  }

  _createClass(Toast, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.props.timeout) {
        this.scheduleTimeout(this.props);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      clearTimeout(this.currentTimeout);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          children = _this$props2.children,
          className = _this$props2.className,
          label = _this$props2.label,
          processing = _this$props2.processing;
      var classNames = cx(uiUtilities['reset-box-sizing'], uiUtilities['box-shadow-400'], theme['toast'], className);
      return /*#__PURE__*/React.createElement("div", {
        "data-teamleader-ui": "toast",
        onMouseEnter: this.handleMouseEnter,
        onMouseLeave: this.handleMouseLeave,
        className: classNames
      }, processing && /*#__PURE__*/React.createElement(LoadingSpinner, {
        className: theme['spinner'],
        color: "neutral",
        tint: "lightest"
      }), /*#__PURE__*/React.createElement(TextBody, {
        className: theme['label'],
        color: "neutral",
        tint: "lightest",
        element: "div"
      }, label, children), this.renderCustomAction() || this.renderCustomLink() || this.renderCloseButton());
    }
  }]);

  return Toast;
}(PureComponent);

export default Toast;