(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-dom", "react-transition-group", "../SnackBar", "../../styles", "./store"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.openSnackBar = exports.SnackBarInstance = void 0;
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var react_dom_1 = require("react-dom");
    var react_transition_group_1 = require("react-transition-group");
    var SnackBar_1 = (0, tslib_1.__importDefault)(require("../SnackBar"));
    var styles_1 = require("../../styles");
    var store_1 = (0, tslib_1.__importDefault)(require("./store"));
    var CONTAINER_SNACK_BAR = "yggdrassil-snack-bar";
    var SnackBarInstance = function () {
        var _a = (0, tslib_1.__read)((0, react_1.useState)([]), 2), snackBarList = _a[0], setSnackBarList = _a[1];
        var refInstance = (0, react_1.useRef)(null);
        var element = document.getElementById(CONTAINER_SNACK_BAR);
        if (!element) {
            element = document.createElement("div");
            element.setAttribute("id", CONTAINER_SNACK_BAR);
            document.body.appendChild(element);
        }
        (0, react_1.useEffect)(function () {
            if (process.env.NODE_ENV !== "production" &&
                element &&
                element.children.length > 1) {
                window.console.error("Warning: You are using two instances SnackBarInstance. Use only one instance per application.");
            }
        }, []);
        (0, react_1.useEffect)(function () {
            store_1.default.subscribe(updateSnackBarList);
            return function () {
                store_1.default.unsubscribe(updateSnackBarList);
                element === null || element === void 0 ? void 0 : element.remove();
            };
        }, []);
        var updateSnackBarList = function () {
            setSnackBarList((0, tslib_1.__spreadArray)([], (0, tslib_1.__read)(store_1.default.data), false));
        };
        return (0, react_dom_1.createPortal)(react_1.default.createElement(styles_1.SnackBarWrapper, { ref: refInstance },
            react_1.default.createElement(react_transition_group_1.TransitionGroup, { component: null, appear: true, enter: true, exit: true }, snackBarList.map(function (params) { return (react_1.default.createElement(react_transition_group_1.CSSTransition, { key: params.id, mountOnEnter: true, unmountOnExit: true, classNames: "snack-bar", in: true, timeout: 300 },
                react_1.default.createElement(SnackBar_1.default, { id: params.id, message: params.message, action: params.action, count: params.count }))); }))), element);
    };
    exports.SnackBarInstance = SnackBarInstance;
    function openSnackBar(params) {
        var id = store_1.default.data.length + 1;
        store_1.default.add((0, tslib_1.__assign)((0, tslib_1.__assign)({}, params), { id: params.id || id }));
    }
    exports.openSnackBar = openSnackBar;
});
