"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _uiIcons = require("@teamleader/ui-icons");

var _uiUtilities = _interopRequireDefault(require("@teamleader/ui-utilities"));

var _classnames = _interopRequireDefault(require("classnames"));

var _react = _interopRequireWildcard(require("react"));

var _iconButton = _interopRequireDefault(require("../iconButton"));

var _link = _interopRequireDefault(require("../link"));

var _loadingSpinner = _interopRequireDefault(require("../loadingSpinner"));

var _typography = require("../typography");

var _theme = _interopRequireDefault(require("./theme.css"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var Toast = function Toast(_ref) {
  var children = _ref.children,
      className = _ref.className,
      label = _ref.label,
      processing = _ref.processing,
      timeout = _ref.timeout,
      onTimeout = _ref.onTimeout,
      link = _ref.link,
      action = _ref.action,
      actionLabel = _ref.actionLabel,
      onClose = _ref.onClose;

  var _useState = (0, _react.useState)(undefined),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      currentTimeout = _useState2[0],
      setCurrentTimeout = _useState2[1];

  var clearCurrentTimeout = function clearCurrentTimeout() {
    clearTimeout(currentTimeout);
    setCurrentTimeout(undefined);
  };

  var scheduleTimeout = function scheduleTimeout() {
    if (currentTimeout) {
      clearCurrentTimeout();
    }

    setCurrentTimeout(setTimeout(function () {
      if (onTimeout) {
        onTimeout();
      }

      clearCurrentTimeout();
    }, timeout));
  };

  var handleMouseEnter = function handleMouseEnter() {
    if (timeout) {
      clearCurrentTimeout();
    }
  };

  var handleMouseLeave = function handleMouseLeave() {
    if (timeout) {
      scheduleTimeout();
    }
  };

  var renderCustomAction = function renderCustomAction() {
    return action && /*#__PURE__*/_react.default.createElement(_link.default, {
      className: _theme.default['action-link'],
      onClick: action
    }, actionLabel);
  };

  var renderCustomLink = function renderCustomLink() {
    return link && /*#__PURE__*/_react.default.createElement(_typography.TextBody, null, /*#__PURE__*/_react.default.cloneElement(link, {
      className: (0, _classnames.default)(link.props.className, _theme.default['action-link'])
    }));
  };

  var renderCloseButton = function renderCloseButton() {
    return onClose && /*#__PURE__*/_react.default.createElement(_iconButton.default, {
      className: _theme.default['action-button'],
      icon: /*#__PURE__*/_react.default.createElement(_uiIcons.IconCloseMediumOutline, null),
      color: "white",
      onClick: onClose
    });
  };

  (0, _react.useEffect)(function () {
    if (timeout) {
      scheduleTimeout();
    }

    return function () {
      clearCurrentTimeout();
    };
  }, [timeout]);
  var classNames = (0, _classnames.default)(_uiUtilities.default['reset-box-sizing'], _uiUtilities.default['box-shadow-400'], _theme.default['toast'], className);
  return /*#__PURE__*/_react.default.createElement("div", {
    "data-teamleader-ui": "toast",
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave,
    className: classNames
  }, processing && /*#__PURE__*/_react.default.createElement(_loadingSpinner.default, {
    className: _theme.default['spinner'],
    color: "neutral",
    tint: "lightest"
  }), /*#__PURE__*/_react.default.createElement(_typography.TextBody, {
    className: _theme.default['label'],
    color: "neutral",
    tint: "lightest",
    element: "div"
  }, label, children), renderCustomAction() || renderCustomLink() || renderCloseButton());
};

var _default = Toast;
exports.default = _default;