import { __read, __values } from "tslib";
import React, { createContext, useState, useContext } from "react";
import { CSSTransition, TransitionGroup } from "react-transition-group";
import { SnackBarWrapper } from "../../styles";
import SnackBar from "../SnackBar";
var DEFAULT_DURATION = 3000;
var SnackBarContext = createContext({
    openSnackBar: function () { return ""; },
    closeSnackBar: function () { return null; }
});
export var SnackBarProvider = function (_a) {
    var children = _a.children;
    var _b = __read(useState(new Map()), 2), snackBars = _b[0], setSnackBars = _b[1];
    var getKeySnackBar = function (message) {
        var e_1, _a;
        try {
            for (var snackBars_1 = __values(snackBars), snackBars_1_1 = snackBars_1.next(); !snackBars_1_1.done; snackBars_1_1 = snackBars_1.next()) {
                var _b = __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.createElement(SnackBarContext.Provider, { value: {
            openSnackBar: openSnackBar,
            closeSnackBar: closeSnackBar
        } },
        children,
        React.createElement(SnackBarWrapper, null,
            React.createElement(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.createElement(CSSTransition, { key: key, mountOnEnter: true, unmountOnExit: true, classNames: "snack-bar", in: true, timeout: 300 },
                        React.createElement(SnackBar, { id: key, timeoutId: value.timeoutId, message: value.message, action: value.action, closeSnackBar: closeSnackBar, count: value.count })));
                }
                return null;
            })))));
};
export var useSnackBar = function () {
    var _a = useContext(SnackBarContext), openSnackBar = _a.openSnackBar, closeSnackBar = _a.closeSnackBar;
    return [openSnackBar, closeSnackBar];
};
