"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSnackbar = void 0;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var ramda_1 = require("ramda");
var Portal_1 = __importDefault(require("@mui/material/Portal"));
var Button_1 = __importDefault(require("@mui/material/Button"));
var Snackbar_1 = __importDefault(require("@mui/material/Snackbar"));
var FIVE_MINUTES = 5 * 60 * 1000;
var useSnackbar = function (_a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.anchorOrigin, anchorOrigin = _c === void 0 ? { vertical: 'bottom', horizontal: 'center' } : _c, showDismiss = _b.showDismiss, _d = _b.dismissTitle, dismissTitle = _d === void 0 ? ui_i18n_1.default.text('Dismiss') : _d, _e = _b.autoHideDuration, autoHideDuration = _e === void 0 ? FIVE_MINUTES : _e, _f = _b.onDismissClick, onDismissClick = _f === void 0 ? ramda_1.identity : _f;
    var _g = (0, react_1.useState)(false), isOpenSnackbar = _g[0], setIsOpenSnackbar = _g[1];
    var _h = (0, react_1.useState)(''), snackbarMessage = _h[0], setSnackbarMessage = _h[1];
    var _j = (0, react_1.useState)(null), snackbarAction = _j[0], setSnackbarAction = _j[1];
    var closeSnackbar = (0, react_1.useCallback)(function () {
        setIsOpenSnackbar(false);
        setSnackbarMessage('');
        setSnackbarAction(null);
    }, []);
    var handleSnackbarClose = (0, react_1.useCallback)(function (_, reason) {
        if (reason === 'clickaway') {
            return;
        }
        closeSnackbar();
    }, [closeSnackbar]);
    var showSnackbarMessage = (0, react_1.useCallback)(function (payload) {
        if (typeof payload === 'object' && 'message' in payload && 'action' in payload) {
            setSnackbarMessage(payload.message);
            setSnackbarAction(payload.action);
        }
        else {
            setSnackbarMessage(payload);
            setSnackbarAction(null);
        }
        setIsOpenSnackbar(true);
    }, []);
    var handleDismissClick = (0, react_1.useCallback)(function () {
        closeSnackbar();
        onDismissClick();
    }, [closeSnackbar, onDismissClick]);
    var handleSnackbarActionClick = (0, react_1.useCallback)(function () {
        closeSnackbar();
        snackbarAction === null || snackbarAction === void 0 ? void 0 : snackbarAction.onClick();
    }, [closeSnackbar, snackbarAction]);
    var SnackbarRenderer = (0, react_1.useCallback)(function () {
        return (react_1.default.createElement(Portal_1.default, { container: document.body },
            react_1.default.createElement(Snackbar_1.default, { open: isOpenSnackbar, anchorOrigin: anchorOrigin, autoHideDuration: autoHideDuration, onClose: handleSnackbarClose, message: snackbarMessage, style: { maxWidth: 500 }, action: showDismiss || snackbarAction ? (react_1.default.createElement(react_1.default.Fragment, null,
                    showDismiss && (react_1.default.createElement(Button_1.default, { color: "primary", onClick: handleDismissClick }, dismissTitle)),
                    snackbarAction && (react_1.default.createElement(Button_1.default, { color: "primary", onClick: handleSnackbarActionClick }, snackbarAction.label)))) : undefined })));
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [isOpenSnackbar, closeSnackbar, snackbarMessage, snackbarAction]);
    return { SnackbarRenderer: SnackbarRenderer, showSnackbarMessage: showSnackbarMessage };
};
exports.useSnackbar = useSnackbar;
