(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "tslib", "react", "react-transition-group", "../../styles", "../SnackBar"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.useSnackBar = exports.SnackBarProvider = void 0;
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var react_transition_group_1 = require("react-transition-group");
    var styles_1 = require("../../styles");
    var SnackBar_1 = (0, tslib_1.__importDefault)(require("../SnackBar"));
    var DEFAULT_DURATION = 3000;
    var SnackBarContext = (0, react_1.createContext)({
        openSnackBar: function () { return ""; },
        closeSnackBar: function () { return null; }
    });
    var SnackBarProvider = function (_a) {
        var children = _a.children;
        var _b = (0, tslib_1.__read)((0, react_1.useState)(new Map()), 2), snackBars = _b[0], setSnackBars = _b[1];
        var getKeySnackBar = function (message) {
            var e_1, _a;
            try {
                for (var snackBars_1 = (0, tslib_1.__values)(snackBars), snackBars_1_1 = snackBars_1.next(); !snackBars_1_1.done; snackBars_1_1 = snackBars_1.next()) {
                    var _b = (0, tslib_1.__read)(snackBars_1_1.value, 2), key = _b[0], value = _b[1];
                    if (value.message === message) {
                        return key;
                    }
                }
            }
            catch (e_1_1) { e_1 = { error: e_1_1 }; }
            finally {
                try {
                    if (snackBars_1_1 && !snackBars_1_1.done && (_a = snackBars_1.return)) _a.call(snackBars_1);
                }
                finally { if (e_1) throw e_1.error; }
            }
            return null;
        };
        var openSnackBar = function (_a) {
            var message = _a.message, duration = _a.duration, action = _a.action, id = _a.id;
            snackBars.entries();
            var keySnackBar = getKeySnackBar(message);
            if (!keySnackBar) {
                keySnackBar = id || "snackBar-" + Math.random();
            }
            var timeoutId = setTimeout(function () {
                closeSnackBar(keySnackBar);
            }, duration || DEFAULT_DURATION);
            var snackBar = snackBars.get(keySnackBar);
            if (snackBar) {
                setSnackBars(function (_snackBars) {
                    return new Map(_snackBars.set(keySnackBar, {
                        message: message,
                        action: action,
                        timeoutId: timeoutId,
                        count: snackBar.count ? snackBar.count + 1 : 2
                    }));
                });
            }
            else {
                setSnackBars(function (_snackBars) {
                    return new Map(_snackBars.set(keySnackBar, {
                        message: message,
                        action: action,
                        timeoutId: timeoutId
                    }));
                });
            }
            return keySnackBar;
        };
        var closeSnackBar = function (key) {
            setSnackBars(function (_snackBars) {
                var copySnackBars = new Map(_snackBars);
                copySnackBars.delete(key);
                return new Map(copySnackBars);
            });
        };
        return (react_1.default.createElement(SnackBarContext.Provider, { value: {
                openSnackBar: openSnackBar,
                closeSnackBar: closeSnackBar
            } },
            children,
            react_1.default.createElement(styles_1.SnackBarWrapper, null,
                react_1.default.createElement(react_transition_group_1.TransitionGroup, { component: null, appear: true, enter: true, exit: true }, Array.from(snackBars.keys())
                    .reverse()
                    .map(function (key) {
                    var value = snackBars.get(key);
                    if (value) {
                        return (react_1.default.createElement(react_transition_group_1.CSSTransition, { key: key, mountOnEnter: true, unmountOnExit: true, classNames: "snack-bar", in: true, timeout: 300 },
                            react_1.default.createElement(SnackBar_1.default, { id: key, timeoutId: value.timeoutId, message: value.message, action: value.action, closeSnackBar: closeSnackBar, count: value.count })));
                    }
                    return null;
                })))));
    };
    exports.SnackBarProvider = SnackBarProvider;
    var useSnackBar = function () {
        var _a = (0, react_1.useContext)(SnackBarContext), openSnackBar = _a.openSnackBar, closeSnackBar = _a.closeSnackBar;
        return [openSnackBar, closeSnackBar];
    };
    exports.useSnackBar = useSnackBar;
});
