import 'core-js/modules/es.array.iterator.js';
import 'core-js/modules/web.dom-collections.iterator.js';
import 'core-js/modules/es.object.assign.js';
import { jsxs, jsx } from 'react/jsx-runtime';
import { Snackbar, Alert } from '@mui/material';
import { useContext, createContext, useState, useCallback } from 'react';

const GlobalSnackAlertContext = /*#__PURE__*/createContext(null);
const GlobalSnackAlertProvider = ({
  children
}) => {
  const [type, setType] = useState('success');
  const [openAlert, setOpenAlert] = useState(false);
  const [snackBarConfig, setSnackBarConfig] = useState({
    anchorOrigin: {
      horizontal: 'right',
      vertical: 'top'
    }
  });
  const [alertMessage, setAlertMessage] = useState();
  const handleClose = useCallback(() => {
    setOpenAlert(false);
  }, []);
  const handleOpen = useCallback((message, type = 'success', config) => {
    setOpenAlert(true);
    setAlertMessage(message);
    setType(type);
    if (config) {
      setSnackBarConfig(prevConfig => Object.assign(Object.assign({}, prevConfig), config));
    }
  }, []);
  return jsxs(GlobalSnackAlertContext.Provider, Object.assign({
    value: {
      open: openAlert,
      openAlert: handleOpen
    }
  }, {
    children: [children, jsx(Snackbar, Object.assign({
      open: openAlert,
      ClickAwayListenerProps: {
        onClickAway: () => null
      },
      onClose: handleClose
    }, snackBarConfig, {
      children: jsx(Alert, Object.assign({
        onClose: handleClose,
        severity: type,
        variant: "outlined"
      }, {
        children: alertMessage
      }))
    }))]
  }));
};
const useSnackAlert = () => {
  const value = useContext(GlobalSnackAlertContext);
  if (value == null) {
    throw new Error(`Must be used inside GlobalSnackAlertProvider`);
  }
  return value;
};

export { GlobalSnackAlertProvider as default, useSnackAlert };
