"use strict";

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

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

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

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

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

var _toastedNotes = _interopRequireDefault(require("toasted-notes"));

var _Alert = require("../Alert");

var _Box = require("../Box");

var _CloseButton = require("../CloseButton");

var _ColorModeProvider = require("../ColorModeProvider");

var _ThemeProvider = require("../ThemeProvider");

var _styles = _interopRequireDefault(require("./styles"));

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 __jsx = _react["default"].createElement;

var Toast = function Toast(_ref) {
  var status = _ref.status,
      variant = _ref.variant,
      id = _ref.id,
      title = _ref.title,
      isClosable = _ref.isClosable,
      onClose = _ref.onClose,
      description = _ref.description,
      props = (0, _objectWithoutProperties2["default"])(_ref, ["status", "variant", "id", "title", "isClosable", "onClose", "description"]);
  var toastStyleProps = (0, _styles["default"])({});
  return __jsx(_Alert.Alert, (0, _extends2["default"])({
    status: status,
    variant: variant,
    id: id
  }, toastStyleProps, props), __jsx(_Alert.AlertIcon, null), __jsx(_Box.Box, {
    flex: "1"
  }, title && __jsx(_Alert.AlertTitle, null, title), description && __jsx(_Alert.AlertDescription, {
    color: "titleText"
  }, description)), isClosable && __jsx(_CloseButton.CloseButton, {
    size: "sm",
    onClick: onClose,
    position: "absolute",
    right: "4px",
    top: "4px"
  }));
};

exports.Toast = Toast;

function useToast() {
  var theme = (0, _ThemeProvider.useTheme)();

  var _useColorMode = (0, _ColorModeProvider.useColorMode)(),
      mode = _useColorMode.mode;

  var notify = (0, _react.useCallback)(function (_ref2) {
    var _ref2$position = _ref2.position,
        position = _ref2$position === void 0 ? 'bottom' : _ref2$position,
        _ref2$duration = _ref2.duration,
        duration = _ref2$duration === void 0 ? 5000 : _ref2$duration,
        render = _ref2.render,
        title = _ref2.title,
        description = _ref2.description,
        status = _ref2.status,
        _ref2$variant = _ref2.variant,
        variant = _ref2$variant === void 0 ? 'top-accent' : _ref2$variant,
        isClosable = _ref2.isClosable;
    var options = {
      position: position,
      duration: duration
    };

    if (render) {
      return _toastedNotes["default"].notify(function (_ref3) {
        var onClose = _ref3.onClose,
            id = _ref3.id;
        return __jsx(_ThemeProvider.ThemeProvider, {
          defaultMode: mode,
          theme: theme
        }, render({
          onClose: onClose,
          id: id
        }));
      }, options);
    }

    _toastedNotes["default"].notify(function (_ref4) {
      var onClose = _ref4.onClose,
          id = _ref4.id;
      return __jsx(_ThemeProvider.ThemeProvider, {
        defaultMode: mode,
        theme: theme
      }, __jsx(Toast, {
        onClose: onClose,
        id: id,
        title: title,
        description: description,
        status: status,
        variant: variant,
        isClosable: isClosable
      }));
    }, options);

    return null;
  }, [theme]);
  return notify;
}
//# sourceMappingURL=Toast.js.map