(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", "./helpers", "./Banner"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.useBannerService = exports.BannerService = void 0;
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var react_dom_1 = require("react-dom");
    var helpers_1 = require("./helpers");
    var Banner_1 = (0, tslib_1.__importDefault)(require("./Banner"));
    var BannerContext = (0, react_1.createContext)({
        bannerShow: function () { return function () {
            /* */
        }; }
    });
    BannerContext.displayName = "BannerContext";
    var BannerService = function (_a) {
        var children = _a.children, idContainer = _a.idContainer;
        var _b = (0, tslib_1.__read)((0, react_1.useState)(new Map()), 2), banners = _b[0], setBanners = _b[1];
        var bannersRef = (0, react_1.useRef)(Array.from(banners.values()).map(function (_a) {
            var container = _a.container;
            return container;
        }));
        (0, react_1.useEffect)(function () {
            bannersRef.current = Array.from(banners.values()).map(function (_a) {
                var container = _a.container;
                return container;
            });
        }, [banners]);
        (0, react_1.useEffect)(function () { return function () {
            bannersRef.current.forEach(function (container) {
                container.remove();
            });
        }; }, []);
        var bannerShow = function (props) {
            var key = "banner-" + Math.random();
            var container = (0, helpers_1.getContainerBanner)(props.type, idContainer);
            setBanners(new Map(banners.set(key, { params: props, container: container })));
            return function () { return bannerClose(key); };
        };
        var bannerClose = function (key) {
            var _a;
            (_a = banners.get(key)) === null || _a === void 0 ? void 0 : _a.container.remove();
            banners.delete(key);
        };
        return (react_1.default.createElement(BannerContext.Provider, { value: { bannerShow: bannerShow } },
            children,
            Array.from(banners.keys()).map(function (key) {
                var banner = banners.get(key);
                if (banner) {
                    var handlePrimaryClick = function () {
                        var _a, _b;
                        (_b = (_a = banner.params).onPrimaryClick) === null || _b === void 0 ? void 0 : _b.call(_a);
                        bannerClose(key);
                    };
                    return (0, react_dom_1.createPortal)(react_1.default.createElement(Banner_1.default, (0, tslib_1.__assign)({}, banner.params, { onPrimaryClick: handlePrimaryClick })), banner.container);
                }
                return null;
            })));
    };
    exports.BannerService = BannerService;
    var useBannerService = function () {
        var bannerShow = (0, react_1.useContext)(BannerContext).bannerShow;
        return [bannerShow];
    };
    exports.useBannerService = useBannerService;
});
