'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var ReactDOM = require('react-dom');
var index = require('../../../utils/index.js');
var MessageProviderToast = require('./components/MessageProviderToast.js');
var constants = require('./constants.js');
var reducer = require('./reducer.js');
var styles = require('./styles.js');
var createMessageMethods = require('./utils/createMessageMethods.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

const MessageContext = React.createContext(constants.INITIAL_MESSAGE_CONTEXT_VALUE);
const useMessageContext = () => React.useContext(MessageContext);
const defaultProviderOption = {
    action: 'acknowledge',
    autoHideDuration: constants.AUTO_HIDE_DURATION_DEFAULT,
    position: constants.MESSAGE_POSITION_DEFAULT,
    toastType: constants.TOAST_TYPE_COMPACT,
    zIndex: 6000,
};
function MessageProvider({ children, messageRoot, options, }) {
    const [state, dispatch] = React.useReducer(reducer.messagesReducer, reducer.initialMessagesState);
    const providerOptions = Object.assign(Object.assign({}, defaultProviderOption), options);
    const methods = React.useMemo(() => (Object.assign({ compact: createMessageMethods.createMessageMethods(dispatch, Object.assign(Object.assign({}, providerOptions), { toastType: constants.TOAST_TYPE_COMPACT })), default: createMessageMethods.createMessageMethods(dispatch, Object.assign(Object.assign({}, providerOptions), { toastType: constants.TOAST_TYPE_DEFAULT })) }, createMessageMethods.createMessageMethods(dispatch, providerOptions))), []);
    // set message provider root
    const mainElement = React.useRef(index.getComponentRoot(messageRoot));
    return (React__default['default'].createElement(MessageContext.Provider, { value: methods },
        children,
        ReactDOM.createPortal(React__default['default'].createElement(styles.ToastWrapper, { position: providerOptions.position, zIndex: providerOptions.zIndex }, state.messages.map((message) => (React__default['default'].createElement(MessageProviderToast.MessageProviderToast, Object.assign({}, message, { key: message.id, position: providerOptions.position }))))), mainElement.current)));
}

exports.MessageProvider = MessageProvider;
exports.useMessageContext = useMessageContext;
