"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.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 react_utils_1 = require("@devesharp/react-utils");
var lodash_1 = require("lodash");
var use_immer_1 = require("use-immer");
var Toast_1 = __importDefault(require("./Toast/Toast"));
var ToastList_style_1 = __importDefault(require("./ToastList.style"));
var ToastObs = new react_utils_1.Event();
var ToastList = function ToastList() {
    var _a = use_immer_1.useImmer([]), items = _a[0], setItems = _a[1];
    var itemsRef = react_1.useRef([]);
    react_1.useLayoutEffect(function () {
        itemsRef.current = items;
    }, [items]);
    function addToast(toast) {
        var _a, _b;
        var currentId = lodash_1.uniqueId();
        var newToast = __assign({}, toast);
        newToast.opened = true;
        newToast.id = (_a = toast.id) !== null && _a !== void 0 ? _a : currentId;
        newToast.timeout = setTimeout(function () {
            setItems(function (v) {
                if (v.findIndex(function (i) { return i.id === newToast.id; }) !== -1) {
                    v[v.findIndex(function (i) { return i.id === newToast.id; })].opened = false;
                }
            });
        }, (_b = newToast.duration) !== null && _b !== void 0 ? _b : 2000);
        setItems(function (v) {
            v.push(newToast);
        });
    }
    react_1.useEffect(function () {
        var obs = ToastObs.observable.subscribe(function (toast) {
            if (toast.id) {
                if (itemsRef.current.find(function (i) { return i.id === toast.id; })) {
                    setItems(function (v) {
                        var _a;
                        var index = itemsRef.current.findIndex(function (i) { return i.id === toast.id; });
                        clearTimeout(v[index].timeout);
                        v[index].timeout = setTimeout(function () {
                            setItems(function (v) {
                                if (v.findIndex(function (i) { return i.id === toast.id; }) !== -1) {
                                    v[v.findIndex(function (i) { return i.id === toast.id; })].opened = false;
                                }
                            });
                        }, (_a = toast.duration) !== null && _a !== void 0 ? _a : 2000);
                    });
                }
                else {
                    addToast(toast);
                }
            }
            else {
                addToast(toast);
            }
        });
        return function () {
            obs.unsubscribe();
            setItems(function (v) {
                return v.map(function (i) {
                    clearTimeout(i.timeout);
                    return i;
                });
            });
        };
    }, []);
    var onDelete = react_1.useCallback(function onDelete(currentId) {
        setItems(function (v) {
            var _a;
            clearTimeout((_a = v.find(function (i) { return i.id === currentId; })) === null || _a === void 0 ? void 0 : _a.timeout);
            v.splice(v.findIndex(function (i) { return i.id === currentId; }), 1);
        });
    }, []);
    return (react_1.default.createElement(ToastList_style_1.default, null, items.map(function (item) { return (react_1.default.createElement(Toast_1.default, { key: "toast-" + item.id, item: item, onDelete: onDelete })); })));
};
exports.ToastList = ToastList;
exports.ToastList.show = function (options) {
    ToastObs.set(options);
};
//# sourceMappingURL=ToastList.js.map