import React, { useCallback, useState } from 'react';
import i18n from 'ui-i18n';
import { identity } from 'ramda';
import Portal from '@mui/material/Portal';
import Button from '@mui/material/Button';
import Snackbar from '@mui/material/Snackbar';
var FIVE_MINUTES = 5 * 60 * 1000;
export 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 ? i18n.text('Dismiss') : _d, _e = _b.autoHideDuration, autoHideDuration = _e === void 0 ? FIVE_MINUTES : _e, _f = _b.onDismissClick, onDismissClick = _f === void 0 ? identity : _f;
    var _g = useState(false), isOpenSnackbar = _g[0], setIsOpenSnackbar = _g[1];
    var _h = useState(''), snackbarMessage = _h[0], setSnackbarMessage = _h[1];
    var _j = useState(null), snackbarAction = _j[0], setSnackbarAction = _j[1];
    var closeSnackbar = useCallback(function () {
        setIsOpenSnackbar(false);
        setSnackbarMessage('');
        setSnackbarAction(null);
    }, []);
    var handleSnackbarClose = useCallback(function (_, reason) {
        if (reason === 'clickaway') {
            return;
        }
        closeSnackbar();
    }, [closeSnackbar]);
    var showSnackbarMessage = 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 = useCallback(function () {
        closeSnackbar();
        onDismissClick();
    }, [closeSnackbar, onDismissClick]);
    var handleSnackbarActionClick = useCallback(function () {
        closeSnackbar();
        snackbarAction === null || snackbarAction === void 0 ? void 0 : snackbarAction.onClick();
    }, [closeSnackbar, snackbarAction]);
    var SnackbarRenderer = useCallback(function () {
        return (React.createElement(Portal, { container: document.body },
            React.createElement(Snackbar, { open: isOpenSnackbar, anchorOrigin: anchorOrigin, autoHideDuration: autoHideDuration, onClose: handleSnackbarClose, message: snackbarMessage, style: { maxWidth: 500 }, action: showDismiss || snackbarAction ? (React.createElement(React.Fragment, null,
                    showDismiss && (React.createElement(Button, { color: "primary", onClick: handleDismissClick }, dismissTitle)),
                    snackbarAction && (React.createElement(Button, { color: "primary", onClick: handleSnackbarActionClick }, snackbarAction.label)))) : undefined })));
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [isOpenSnackbar, closeSnackbar, snackbarMessage, snackbarAction]);
    return { SnackbarRenderer: SnackbarRenderer, showSnackbarMessage: showSnackbarMessage };
};
