"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToastList = void 0;
var react_1 = __importStar(require("react"));
var event_service_1 = require("../../services/event.service");
var toast_list_style_1 = __importDefault(require("./toast-list.style"));
var toast_1 = __importDefault(require("./toast/toast"));
var ToastObs = new event_service_1.Event();
var id = 0;
exports.ToastList = function () {
    var _a = react_1.useState([]), items = _a[0], setItems = _a[1];
    react_1.useEffect(function () {
        var obs = ToastObs.observable.subscribe(function (value) {
            id += 1;
            var index = id;
            value.forceClose = function () {
                clearTimeout(value.closeTimer);
                setTimeout(function () {
                    setItems(function (newItems) {
                        var list = __spreadArrays(newItems);
                        return list.map(function (i) { return (i.id != index ? i : __assign(__assign({}, i), { opened: false })); });
                    });
                    setTimeout(function () {
                        setItems(function (newItems) {
                            var list = __spreadArrays(newItems);
                            return list.filter(function (i) { return i.id != index; });
                        });
                    }, 25);
                }, 50);
            };
            value.closeTimer = function () {
                setTimeout(function () {
                    setItems(function (newItems) {
                        var list = __spreadArrays(newItems);
                        return list.map(function (i) { return (i.id != index ? i : __assign(__assign({}, i), { opened: false })); });
                    });
                    setTimeout(function () {
                        setItems(function (newItems) {
                            var list = __spreadArrays(newItems);
                            return list.filter(function (i) { return i.id != index; });
                        });
                    }, 300);
                }, 3000);
            };
            setItems(function (newItems) {
                var list = __spreadArrays(newItems);
                list.push(__assign(__assign({}, value), { opened: true, id: index }));
                return list;
            });
            value.closeTimer();
        });
        return function () { return obs.unsubscribe(); };
    }, []);
    return (react_1.default.createElement(toast_list_style_1.default, null, items.map(function (item) { return (react_1.default.createElement(toast_1.default, { key: "toast-" + item.id, item: item })); })));
};
exports.ToastList.show = function (options) {
    ToastObs.set(options);
};
//# sourceMappingURL=toast-list.js.map