import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { NotificationChangeType, StatusMessageChangeType } from '../../../common/notifications.js';
import { StatusbarAlignment } from '../../../services/statusbar/browser/statusbar.js';
import { IStatusbarService } from '../../../services/statusbar/browser/statusbar.service.js';
import { Disposable, dispose } from '../../../../base/common/lifecycle.js';
import { HIDE_NOTIFICATIONS_CENTER, SHOW_NOTIFICATIONS_CENTER } from './notificationsCommands.js';
import { localizeWithPath } from '../../../../nls.js';
import { NotificationsFilter } from '../../../../platform/notification/common/notification.js';
import { INotificationService } from '../../../../platform/notification/common/notification.service.js';
const _moduleId = "vs/workbench/browser/parts/notifications/notificationsStatus";
let NotificationsStatus = class NotificationsStatus extends Disposable {
    constructor(model, statusbarService, notificationService) {
        super();
        this.model = model;
        this.statusbarService = statusbarService;
        this.notificationService = notificationService;
        this.newNotificationsCount = 0;
        this.isNotificationsCenterVisible = false;
        this.isNotificationsToastsVisible = false;
        this.updateNotificationsCenterStatusItem();
        if (model.statusMessage) {
            this.doSetStatusMessage(model.statusMessage);
        }
        this.registerListeners();
    }
    registerListeners() {
        this._register(this.model.onDidChangeNotification(e => this.onDidChangeNotification(e)));
        this._register(this.model.onDidChangeStatusMessage(e => this.onDidChangeStatusMessage(e)));
        this._register(this.notificationService.onDidChangeFilter(() => this.updateNotificationsCenterStatusItem()));
    }
    onDidChangeNotification(e) {
        if (!this.isNotificationsCenterVisible) {
            if (e.kind === NotificationChangeType.ADD) {
                this.newNotificationsCount++;
            }
            else if (e.kind === NotificationChangeType.REMOVE && this.newNotificationsCount > 0) {
                this.newNotificationsCount--;
            }
        }
        this.updateNotificationsCenterStatusItem();
    }
    updateNotificationsCenterStatusItem() {
        let notificationsInProgress = 0;
        if (!this.isNotificationsCenterVisible && !this.isNotificationsToastsVisible) {
            for (const notification of this.model.notifications) {
                if (notification.hasProgress) {
                    notificationsInProgress++;
                }
            }
        }
        let statusProperties = {
            name: ( localizeWithPath(_moduleId, 0, "Notifications")),
            text: `${notificationsInProgress > 0 || this.newNotificationsCount > 0 ? '$(bell-dot)' : '$(bell)'}`,
            ariaLabel: ( localizeWithPath(_moduleId, 0, "Notifications")),
            command: this.isNotificationsCenterVisible ? HIDE_NOTIFICATIONS_CENTER : SHOW_NOTIFICATIONS_CENTER,
            tooltip: this.getTooltip(notificationsInProgress),
            showBeak: this.isNotificationsCenterVisible
        };
        if (this.notificationService.getFilter() === NotificationsFilter.ERROR) {
            statusProperties = {
                ...statusProperties,
                text: `${notificationsInProgress > 0 || this.newNotificationsCount > 0 ? '$(bell-slash-dot)' : '$(bell-slash)'}`,
                ariaLabel: ( localizeWithPath(_moduleId, 1, "Do Not Disturb")),
                tooltip: ( localizeWithPath(_moduleId, 2, "Do Not Disturb Mode is Enabled"))
            };
        }
        if (!this.notificationsCenterStatusItem) {
            this.notificationsCenterStatusItem = this.statusbarService.addEntry(statusProperties, 'status.notifications', StatusbarAlignment.RIGHT, -Number.MAX_VALUE );
        }
        else {
            this.notificationsCenterStatusItem.update(statusProperties);
        }
    }
    getTooltip(notificationsInProgress) {
        if (this.isNotificationsCenterVisible) {
            return ( localizeWithPath(_moduleId, 3, "Hide Notifications"));
        }
        if (this.model.notifications.length === 0) {
            return ( localizeWithPath(_moduleId, 4, "No Notifications"));
        }
        if (notificationsInProgress === 0) {
            if (this.newNotificationsCount === 0) {
                return ( localizeWithPath(_moduleId, 5, "No New Notifications"));
            }
            if (this.newNotificationsCount === 1) {
                return ( localizeWithPath(_moduleId, 6, "1 New Notification"));
            }
            return ( localizeWithPath(_moduleId, 7, "{0} New Notifications", this.newNotificationsCount));
        }
        if (this.newNotificationsCount === 0) {
            return ( localizeWithPath(
                _moduleId,
                8,
                "No New Notifications ({0} in progress)",
                notificationsInProgress
            ));
        }
        if (this.newNotificationsCount === 1) {
            return ( localizeWithPath(
                _moduleId,
                9,
                "1 New Notification ({0} in progress)",
                notificationsInProgress
            ));
        }
        return ( localizeWithPath(
            _moduleId,
            10,
            "{0} New Notifications ({1} in progress)",
            this.newNotificationsCount,
            notificationsInProgress
        ));
    }
    update(isCenterVisible, isToastsVisible) {
        let updateNotificationsCenterStatusItem = false;
        if (this.isNotificationsCenterVisible !== isCenterVisible) {
            this.isNotificationsCenterVisible = isCenterVisible;
            this.newNotificationsCount = 0;
            updateNotificationsCenterStatusItem = true;
        }
        if (this.isNotificationsToastsVisible !== isToastsVisible) {
            this.isNotificationsToastsVisible = isToastsVisible;
            updateNotificationsCenterStatusItem = true;
        }
        if (updateNotificationsCenterStatusItem) {
            this.updateNotificationsCenterStatusItem();
        }
    }
    onDidChangeStatusMessage(e) {
        const statusItem = e.item;
        switch (e.kind) {
            case StatusMessageChangeType.ADD:
                this.doSetStatusMessage(statusItem);
                break;
            case StatusMessageChangeType.REMOVE:
                if (this.currentStatusMessage && this.currentStatusMessage[0] === statusItem) {
                    dispose(this.currentStatusMessage[1]);
                    this.currentStatusMessage = undefined;
                }
                break;
        }
    }
    doSetStatusMessage(item) {
        const message = item.message;
        const showAfter = item.options && typeof item.options.showAfter === 'number' ? item.options.showAfter : 0;
        const hideAfter = item.options && typeof item.options.hideAfter === 'number' ? item.options.hideAfter : -1;
        if (this.currentStatusMessage) {
            dispose(this.currentStatusMessage[1]);
        }
        let statusMessageEntry;
        let showHandle = setTimeout(() => {
            statusMessageEntry = this.statusbarService.addEntry({
                name: ( localizeWithPath(_moduleId, 11, "Status Message")),
                text: message,
                ariaLabel: message
            }, 'status.message', StatusbarAlignment.LEFT, -Number.MAX_VALUE );
            showHandle = null;
        }, showAfter);
        let hideHandle;
        const statusMessageDispose = {
            dispose: () => {
                if (showHandle) {
                    clearTimeout(showHandle);
                }
                if (hideHandle) {
                    clearTimeout(hideHandle);
                }
                statusMessageEntry?.dispose();
            }
        };
        if (hideAfter > 0) {
            hideHandle = setTimeout(() => statusMessageDispose.dispose(), hideAfter);
        }
        this.currentStatusMessage = [item, statusMessageDispose];
    }
};
NotificationsStatus = ( (__decorate([
    ( (__param(1, IStatusbarService))),
    ( (__param(2, INotificationService)))
], NotificationsStatus)));
export { NotificationsStatus };
