"use strict";

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

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

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _primitives = require("../../../../primitives");

var _behaviors = require("../../../behaviors");

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

var _icon = require("../../../icon");

var _branding = require("../../../../mixins/branding");

var _toast = require("./toast.proptypes");

var _toast2 = require("./toast.css-rules");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var StyledToastContainer = _styledComponents["default"].div.withConfig({
  displayName: "StyledToastContainer",
  componentId: "sc-2kat61-0"
})(["margin-bottom:10px;transition:all 100ms linear;z-index:1;&:last-child{margin-bottom:0;}"]);

var StyledToast = _styledComponents["default"].div.withConfig({
  displayName: "StyledToast",
  componentId: "sc-2kat61-1"
})(["pointer-events:auto;display:inline-block;background-color:", ";color:", ";padding:20px;border-radius:6px;box-shadow:0 5px 15px 0 rgba(191,189,187,0.5);"], _toast2.getBackgroundColor, (0, _branding.getPaletteColor)('white'));

exports.StyledToast = StyledToast;

var StyledToastInnerElement = _styledComponents["default"].div.withConfig({
  displayName: "StyledToastInnerElement",
  componentId: "sc-2kat61-2"
})(["display:inline-block;min-height:16px;line-height:1.4;"]);

var CloseButton = (0, _styledComponents["default"])(_behaviors.UnstyledButton).withConfig({
  displayName: "CloseButton",
  componentId: "sc-2kat61-3"
})(["height:16;display:flex;"]);
exports.CloseButton = CloseButton;

var Toast = function Toast(_ref) {
  var id = _ref.id,
      children = _ref.children,
      _ref$timerDelay = _ref.timerDelay,
      timerDelay = _ref$timerDelay === void 0 ? 5000 : _ref$timerDelay,
      onClose = _ref.onClose,
      level = _ref.level;

  var _useState = (0, _react.useState)(true),
      _useState2 = _slicedToArray(_useState, 2),
      show = _useState2[0],
      setShow = _useState2[1];

  var _useState3 = (0, _react.useState)(null),
      _useState4 = _slicedToArray(_useState3, 2),
      animationTimerId = _useState4[0],
      setAnimationTimerId = _useState4[1];

  var handleClose = (0, _react.useCallback)(function () {
    setShow(false);

    if (onClose) {
      onClose(id);
    }
  }, [id, onClose]);
  var startTimer = (0, _react.useCallback)(function () {
    if (timerDelay > 0) {
      var timerId = window.setTimeout(handleClose, timerDelay);
      setAnimationTimerId(timerId);
    }
  }, [handleClose, timerDelay]);
  var stopTimer = (0, _react.useCallback)(function () {
    if (animationTimerId === null) return;
    window.clearTimeout(animationTimerId);
    setAnimationTimerId(null);
  }, [animationTimerId]);
  var handleMouseIn = (0, _react.useCallback)(function () {
    stopTimer();
  }, [stopTimer]);
  var handleMouseOut = (0, _react.useCallback)(function () {
    startTimer();
  }, [startTimer]);
  (0, _react.useEffect)(function () {
    startTimer();
  }, [startTimer]);
  if (!show) return null;
  return _react["default"].createElement(StyledToastContainer, null, _react["default"].createElement(StyledToast, {
    onMouseOver: handleMouseIn,
    onFocus: handleMouseIn,
    onMouseOut: handleMouseOut,
    onBlur: handleMouseOut,
    level: level
  }, _react["default"].createElement(_primitives.Box, {
    stacked: "row",
    justify: "center",
    align: "center"
  }, _react["default"].createElement(StyledToastInnerElement, null, _react["default"].createElement(_typography.Span, {
    fontSize: "f1",
    colorName: "white",
    bold: true,
    face: "sans"
  }, children)), _react["default"].createElement(StyledToastInnerElement, {
    style: {
      marginLeft: 40
    }
  }, _react["default"].createElement(CloseButton, {
    insetFocus: true,
    onFocus: handleMouseIn,
    onBlur: handleMouseOut,
    onClick: handleClose
  }, _react["default"].createElement(_icon.Close, null))))));
};

exports.Toast = Toast;
Toast.displayName = 'Toast';
Toast.propTypes = _toast.toastPropTypes;