"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get NotificationsStore () {
        return NotificationsStore;
    },
    get addNotification () {
        return addNotification;
    },
    get closeNotification () {
        return closeNotification;
    }
});
const _storeon = require("storeon");
const NotificationsStore = (0, _storeon.createStoreon)([
    (store)=>{
        store.on('@init', ()=>({
                notifications: []
            }));
        store.on('add', ({ notifications }, notif)=>{
            return {
                notifications: notifications.concat([
                    notif
                ])
            };
        });
        store.on('hide', ({ notifications }, id)=>{
            return {
                notifications: notifications.map((notif)=>id === notif.id ? {
                        ...notif,
                        isHidden: true
                    } : notif)
            };
        });
        store.on('remove', ({ notifications }, id)=>{
            return {
                notifications: notifications.filter((notif)=>id !== notif.id)
            };
        });
    }
]);
const closeNotification = (id, delay = 380)=>{
    const { dispatch } = NotificationsStore;
    dispatch('hide', id);
    setTimeout(()=>dispatch('remove', id), delay);
};
function addNotification({ id: externalId, onTimeoutClose, ...rest }, timeout = 2000) {
    const id = externalId || `plasma-notification-${Date.now()}`;
    const { dispatch, get } = NotificationsStore;
    dispatch('add', {
        ...rest,
        id,
        isHidden: false
    });
    // TODO: Вынести логику закрытия в отдельный модуль, данный метод должен только создавать новое оповещение
    if (timeout !== 0 && timeout !== null) {
        setTimeout(()=>{
            const { notifications } = get();
            const notification = notifications.find((notification)=>notification.id === id);
            if (!notification) {
                return;
            }
            closeNotification(id);
            onTimeoutClose?.();
        }, timeout);
    }
    return id;
}
