import CheckCircle from '../../../../../../node_modules/@mui/icons-material/esm/CheckCircle.mjs';
import CloseIcon from '../../../../../../node_modules/@mui/icons-material/esm/Close.mjs';
import ErrorIcon from '../../../../../../node_modules/@mui/icons-material/esm/Error.mjs';
import React__default, { useState, useRef, useCallback, useContext, createContext } from 'react';
import { mergeSx } from '../utils/mergeSx.mjs';
import { noop } from '../utils/noop.mjs';
import styled from '../../../../../../node_modules/@mui/material/styles/styled.mjs';
import Snackbar from '../../../../../../node_modules/@mui/material/Snackbar/Snackbar.mjs';
import Alert from '../../../../../../node_modules/@mui/material/Alert/Alert.mjs';
import alertClasses from '../../../../../../node_modules/@mui/material/Alert/alertClasses.mjs';
import IconButton from '../../../../../../node_modules/@mui/material/IconButton/IconButton.mjs';

const StyledSnackbar = styled(Snackbar)(({ theme })=>({
        bottom: theme.spacing(5)
    }));
const CloseButton$1 = styled(IconButton)(({ theme })=>({
        padding: theme.spacing(0.5)
    }));
const StyledAlert$1 = styled(Alert)(({ theme })=>({
        [`& .${alertClasses.icon}`]: {
            marginRight: theme.spacing(1)
        },
        [`& .${alertClasses.message}`]: {
            padding: 0
        },
        [`& .${alertClasses.action}`]: {
            marginRight: theme.spacing(-0.5)
        },
        [theme.breakpoints.down('sm')]: {
            width: '100%'
        },
        width: 512,
        padding: theme.spacing(1, 1.5),
        display: 'flex',
        alignItems: 'center',
        '& p': {}
    }));
const DEFAULT_STATE$1 = {
    openToast: noop,
    closeToast: noop
};
const ToastContext = /*#__PURE__*/ createContext(DEFAULT_STATE$1);
const useToast = ()=>useContext(ToastContext);
const ToastContextProviderCore = ({ children, alertSx, closeIconSx, errorIconSx, successIconSx, errorSx, successSx, TypographyComponent: Typography })=>{
    const [open, setOpen] = useState(false);
    const [toastText, setToastText] = useState('');
    const [toastType, setToastType] = useState('success');
    const timeoutRef = useRef(null);
    const closeToast = ()=>setOpen(false);
    const triggerOpen = useCallback((openDurationMS)=>{
        setOpen(true);
        if (timeoutRef.current) {
            clearTimeout(timeoutRef.current);
        }
        timeoutRef.current = setTimeout(()=>{
            setOpen(false);
        }, openDurationMS);
    }, []);
    // Without this useCallback, the value of openToast changes on every render
    // causing downstream code shaped like
    // `useEffect(() => ..., [openToast])`
    // to run again, which we don't want to do
    const openToast = useCallback(({ type, text, openDurationMS = 5000 })=>{
        setToastType(type);
        setToastText(text);
        triggerOpen(openDurationMS);
    }, [
        triggerOpen
    ]);
    return /*#__PURE__*/ React__default.createElement(ToastContext.Provider, {
        value: {
            openToast,
            closeToast
        }
    }, /*#__PURE__*/ React__default.createElement(StyledSnackbar, {
        anchorOrigin: {
            horizontal: 'center',
            vertical: 'bottom'
        },
        open: open
    }, /*#__PURE__*/ React__default.createElement(StyledAlert$1, {
        sx: mergeSx(alertSx, toastType === 'error' ? errorSx : successSx),
        action: /*#__PURE__*/ React__default.createElement(CloseButton$1, {
            onClick: closeToast,
            size: "large"
        }, /*#__PURE__*/ React__default.createElement(CloseIcon, {
            fontSize: "small",
            sx: mergeSx(closeIconSx, toastType === 'error' ? errorIconSx : successIconSx)
        })),
        elevation: 6,
        icon: toastType === 'error' ? /*#__PURE__*/ React__default.createElement(ErrorIcon, {
            fontSize: "small",
            sx: errorIconSx
        }) : /*#__PURE__*/ React__default.createElement(CheckCircle, {
            fontSize: "small",
            sx: successIconSx
        }),
        type: toastType,
        variant: "filled"
    }, /*#__PURE__*/ React__default.createElement(Typography, {
        variant: "body2"
    }, toastText))), children);
};

export { ToastContext, ToastContextProviderCore, useToast };
//# sourceMappingURL=Toast.mjs.map
