"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.streamNotification = exports.streamNotificationManager = void 0;
class StreamNotificationManager {
    constructor() {
        this.notifications = [];
        this.listeners = new Set();
        this.dismissTimers = new Map();
    }
    show(config) {
        const notification = {
            id: config.id || `notification-${Date.now()}`,
            message: config.message,
            render: config.render,
            duration: config.duration ?? 5,
            actionLabel: config.actionLabel,
            onAction: config.onAction,
            onDismiss: config.onDismiss,
            variant: config.variant ?? "default",
            visible: true,
            startTime: Date.now(),
        };
        // if notification with same ID exists, dismiss it first
        const existingIndex = this.notifications.findIndex((n) => n.id === notification.id);
        if (existingIndex !== -1) {
            const existingTimer = this.dismissTimers.get(notification.id);
            if (existingTimer) {
                clearTimeout(existingTimer);
                this.dismissTimers.delete(notification.id);
            }
            this.notifications = this.notifications.filter((n) => n.id !== notification.id);
        }
        this.notifications = [...this.notifications, notification];
        this.notifyListeners();
        // auto-dismiss if duration > 0
        if (notification.duration && notification.duration > 0) {
            const timer = setTimeout(() => {
                this.requestDismiss(notification.id, "auto");
            }, notification.duration * 1000);
            this.dismissTimers.set(notification.id, timer);
        }
    }
    requestDismiss(id, reason = "user") {
        const notification = this.notifications.find((n) => n.id === id);
        if (!notification) {
            console.log("Notification not found!");
            return;
        }
        // mark the notification for dismissal
        notification.shouldDismiss = true;
        notification.dismissReason = reason;
        this.notifyListeners();
        // after 500ms, just hide it for real
        setTimeout(() => {
            this.hide(id, reason);
        }, 500);
    }
    hide(id, reason = "user") {
        console.log("Hide called with id:", id, "reason:", reason);
        console.log("Current notifications:", this.notifications.map((n) => n.id));
        const notification = this.notifications.find((n) => n.id === id);
        if (!notification) {
            console.log("Notification not found!");
            return;
        }
        const timer = this.dismissTimers.get(id);
        if (timer) {
            clearTimeout(timer);
            this.dismissTimers.delete(id);
        }
        this.notifications = this.notifications.filter((n) => n.id !== id);
        console.log("Remaining notifications:", this.notifications.map((n) => n.id));
        this.notifyListeners();
        notification.onDismiss?.(reason);
    }
    getAll() {
        return this.notifications;
    }
    subscribe(listener) {
        this.listeners.add(listener);
        return () => {
            this.listeners.delete(listener);
        };
    }
    notifyListeners() {
        this.listeners.forEach((listener) => {
            listener(this.notifications);
        });
    }
}
exports.streamNotificationManager = new StreamNotificationManager();
exports.streamNotification = {
    show: (config) => exports.streamNotificationManager.show(config),
    hide: (id) => exports.streamNotificationManager.hide(id),
};
//# sourceMappingURL=stream-notification-manager.js.map