'use strict';

var _tslib = require('../chunks/bundle-CxoFSSXv.js');
var React = require('react');
var index = require('../chunks/bundle-Dn0hi6OJ.js');
var utils = require('../chunks/bundle-DQNu-S6D.js');
var ui_Modal = require('../chunks/bundle-YeW89zsR.js');
require('react-dom');
require('../chunks/bundle-DDNdw3A9.js');
require('../chunks/bundle-EhtiN7UV.js');
require('../chunks/bundle-BV7y6VDn.js');
require('../chunks/bundle-CyQD5TkW.js');
require('../ui/IconButton.js');
require('../ui/Button.js');
require('../chunks/bundle-B0UICprZ.js');
require('../ui/Icon.js');
require('../chunks/bundle-BUGwK1WK.js');
require('../chunks/bundle-LI9JjqG2.js');
require('../chunks/bundle-AAO7jUU2.js');
require('../chunks/bundle-yU9Mr2pS.js');
require('@sendbird/chat');
require('@sendbird/chat/groupChannel');
require('@sendbird/chat/openChannel');

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

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

var GlobalModalContext = React.createContext({
    openModal: utils.noop,
});
var GlobalModalProvider = function (_a) {
    var children = _a.children;
    // Idea from https://dev.to/nurislamov/simple-modals-queue-in-react-4g6c
    var _b = React.useState([]), globalModalQueue = _b[0], setGlobalModalQueue = _b[1];
    var openModal = React.useCallback(function (props) {
        setGlobalModalQueue(function (currentQue) { return _tslib.__spreadArray(_tslib.__spreadArray([], currentQue, true), [props], false); });
    }, []);
    var closeModal = React.useCallback(function () {
        setGlobalModalQueue(function (currentQue) { return currentQue.slice(1); });
    }, []);
    var ModalComponent = React.useMemo(function () { return function () {
        return index.K(globalModalQueue)
            .when(function (q) { return q.length === 0; }, function () {
            return React__default.default.createElement(React__default.default.Fragment, null);
        })
            .otherwise(function () {
            var _a = globalModalQueue[0], modalProps = _a.modalProps, childElement = _a.childElement;
            return (React__default.default.createElement(ui_Modal.Modal, _tslib.__assign({}, modalProps, { className: utils.classnames('sendbird-global-modal', modalProps === null || modalProps === void 0 ? void 0 : modalProps.className), onClose: function () {
                    var _a;
                    (_a = modalProps === null || modalProps === void 0 ? void 0 : modalProps.onClose) === null || _a === void 0 ? void 0 : _a.call(modalProps);
                    closeModal();
                } }), childElement({
                closeModal: closeModal,
            })));
        });
    }; }, [globalModalQueue]);
    return (React__default.default.createElement(GlobalModalContext.Provider, { value: {
            openModal: openModal,
        } },
        React__default.default.createElement(ModalComponent, null),
        children));
};
var useGlobalModalContext = function () { return React.useContext(GlobalModalContext); };

exports.MODAL_ROOT = ui_Modal.MODAL_ROOT;
exports.ModalRoot = ui_Modal.ModalRoot;
exports.GlobalModalProvider = GlobalModalProvider;
exports.useGlobalModalContext = useGlobalModalContext;
//# sourceMappingURL=useModal.js.map
