"use strict";

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

var _Close = _interopRequireDefault(require("@mui/icons-material/Close"));

var _Alert = _interopRequireDefault(require("@mui/material/Alert"));

var _IconButton = _interopRequireDefault(require("@mui/material/IconButton"));

var _Snackbar = _interopRequireDefault(require("@mui/material/Snackbar"));

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

var _reactPolyglot = require("react-polyglot");

var _hooks = require("../../store/hooks");

var _snackbars = require("../../store/slices/snackbars");

var _react2 = require("@emotion/react");

const _excluded = ["key"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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; }

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

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const Snackbars = _ref => {
  let {
    t
  } = _ref;

  const [open, setOpen] = _react.default.useState(false);

  const [messageInfo, setMessageInfo] = (0, _react.useState)(undefined);
  const snackbars = (0, _hooks.useAppSelector)(_snackbars.selectSnackbars);
  const dispatch = (0, _hooks.useAppDispatch)();
  (0, _react.useEffect)(() => {
    if (snackbars.length && !messageInfo) {
      // Set a new snack when we don't have an active one
      const snackbar = _objectSpread({}, snackbars[0]);

      setMessageInfo(snackbar);
      dispatch((0, _snackbars.removeSnackbarById)(snackbar.id));
      setOpen(true);
    } else if (snackbars.length && messageInfo && open) {
      // Close an active snack when a new one is added
      setOpen(false);
    }
  }, [snackbars, messageInfo, open]);
  const handleClose = (0, _react.useCallback)((_event, reason) => {
    if (reason === 'clickaway') {
      return;
    }

    setOpen(false);
  }, []);

  const handleExited = () => {
    setMessageInfo(undefined);
  };

  const renderAlert = (0, _react.useCallback)(data => {
    const {
      type,
      message: {
        key
      }
    } = data,
          options = _objectWithoutProperties(data.message, _excluded);

    return (0, _react2.jsx)(_Alert.default, {
      key: "message",
      onClose: handleClose,
      severity: type,
      sx: {
        width: '100%'
      }
    }, t(key, options));
  }, [handleClose, t]);
  return (0, _react2.jsx)(_Snackbar.default, {
    key: messageInfo ? messageInfo.id : undefined,
    open: open,
    autoHideDuration: 6000,
    onClose: handleClose,
    TransitionProps: {
      onExited: handleExited
    },
    anchorOrigin: {
      vertical: 'bottom',
      horizontal: 'right'
    },
    action: (0, _react2.jsx)(_IconButton.default, {
      "aria-label": "close",
      color: "inherit",
      sx: {
        p: 0.5
      },
      onClick: handleClose
    }, (0, _react2.jsx)(_Close.default, null))
  }, messageInfo ? renderAlert(messageInfo) : undefined);
};

var _default = (0, _reactPolyglot.translate)()(Snackbars);

exports.default = _default;