import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import './media/notificationsCenter.css.js';
import './media/notificationsActions.css.js';
import { NOTIFICATIONS_CENTER_BORDER, NOTIFICATIONS_CENTER_HEADER_FOREGROUND, NOTIFICATIONS_CENTER_HEADER_BACKGROUND } from '../../../common/theme.js';
import { Themable } from '../../../../platform/theme/common/themeService.js';
import { IThemeService } from '../../../../platform/theme/common/themeService.service.js';
import { NotificationChangeType, NotificationViewItemContentChangeKind } from '../../../common/notifications.js';
import { Parts } from '../../../services/layout/browser/layoutService.js';
import { IWorkbenchLayoutService } from '../../../services/layout/browser/layoutService.service.js';
import { Emitter } from '../../../../base/common/event.js';
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.service.js';
import { NotificationActionRunner } from './notificationsCommands.js';
import { NotificationsList } from './notificationsList.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { Dimension, isAncestorOfActiveElement } from '../../../../base/browser/dom.js';
import '../../../../platform/theme/common/colorUtils.js';
import '../../../../platform/theme/common/colors/baseColors.js';
import '../../../../platform/theme/common/colors/chartsColors.js';
import { widgetShadow } from '../../../../platform/theme/common/colors/editorColors.js';
import '../../../../platform/theme/common/colors/inputColors.js';
import '../../../../platform/theme/common/colors/listColors.js';
import '../../../../platform/theme/common/colors/menuColors.js';
import '../../../../platform/theme/common/colors/minimapColors.js';
import '../../../../platform/theme/common/colors/miscColors.js';
import '../../../../platform/theme/common/colors/quickpickColors.js';
import '../../../../platform/theme/common/colors/searchColors.js';
import { IEditorGroupsService } from '../../../services/editor/common/editorGroupsService.service.js';
import { localizeWithPath } from '../../../../nls.js';
import { ActionBar } from '../../../../base/browser/ui/actionbar/actionbar.js';
import { ConfigureDoNotDisturbAction, ToggleDoNotDisturbAction, ToggleDoNotDisturbBySourceAction, ClearAllNotificationsAction, HideNotificationsCenterAction } from './notificationsActions.js';
import { toAction, Separator } from '../../../../base/common/actions.js';
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.service.js';
import { assertIsDefined, assertAllDefined } from '../../../../base/common/types.js';
import { NotificationsCenterVisibleContext } from '../../../common/contextkeys.js';
import { NotificationsFilter } from '../../../../platform/notification/common/notification.js';
import { INotificationService } from '../../../../platform/notification/common/notification.service.js';
import { mainWindow } from '../../../../base/browser/window.js';
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.service.js';
import { DropdownMenuActionViewItem } from '../../../../base/browser/ui/dropdown/dropdownActionViewItem.js';
import { AccessibilitySignal } from '../../../../platform/accessibilitySignal/browser/accessibilitySignalService.js';
import { IAccessibilitySignalService } from '../../../../platform/accessibilitySignal/browser/accessibilitySignalService.service.js';
var NotificationsCenter_1;
const _moduleId = "vs/workbench/browser/parts/notifications/notificationsCenter";
let NotificationsCenter = class NotificationsCenter extends Themable {
    static { NotificationsCenter_1 = this; }
    static { this.MAX_DIMENSIONS = ( (new Dimension(450, 400))); }
    static { this.MAX_NOTIFICATION_SOURCES = 10; }
    constructor(container, model, themeService, instantiationService, layoutService, contextKeyService, editorGroupService, keybindingService, notificationService, accessibilitySignalService, contextMenuService) {
        super(themeService);
        this.container = container;
        this.model = model;
        this.instantiationService = instantiationService;
        this.layoutService = layoutService;
        this.contextKeyService = contextKeyService;
        this.editorGroupService = editorGroupService;
        this.keybindingService = keybindingService;
        this.notificationService = notificationService;
        this.accessibilitySignalService = accessibilitySignalService;
        this.contextMenuService = contextMenuService;
        this._onDidChangeVisibility = this._register(( (new Emitter())));
        this.onDidChangeVisibility = this._onDidChangeVisibility.event;
        this.notificationsCenterVisibleContextKey = NotificationsCenterVisibleContext.bindTo(this.contextKeyService);
        this.notificationsCenterVisibleContextKey = NotificationsCenterVisibleContext.bindTo(contextKeyService);
        this.registerListeners();
    }
    registerListeners() {
        this._register(this.model.onDidChangeNotification(e => this.onDidChangeNotification(e)));
        this._register(this.layoutService.onDidLayoutMainContainer(dimension => this.layout(Dimension.lift(dimension))));
        this._register(this.notificationService.onDidChangeFilter(() => this.onDidChangeFilter()));
    }
    onDidChangeFilter() {
        if (this.notificationService.getFilter() === NotificationsFilter.ERROR) {
            this.hide();
        }
    }
    get isVisible() {
        return !!this._isVisible;
    }
    show() {
        if (this._isVisible) {
            const notificationsList = assertIsDefined(this.notificationsList);
            notificationsList.show();
            notificationsList.focusFirst();
            return;
        }
        if (!this.notificationsCenterContainer) {
            this.create();
        }
        this.updateTitle();
        const [notificationsList, notificationsCenterContainer] = assertAllDefined(this.notificationsList, this.notificationsCenterContainer);
        this._isVisible = true;
        notificationsCenterContainer.classList.add('visible');
        notificationsList.show();
        this.layout(this.workbenchDimensions);
        notificationsList.updateNotificationsList(0, 0, this.model.notifications);
        notificationsList.focusFirst();
        this.updateStyles();
        this.model.notifications.forEach(notification => notification.updateVisibility(true));
        this.notificationsCenterVisibleContextKey.set(true);
        this._onDidChangeVisibility.fire();
    }
    updateTitle() {
        const [notificationsCenterTitle, clearAllAction] = assertAllDefined(this.notificationsCenterTitle, this.clearAllAction);
        if (this.model.notifications.length === 0) {
            notificationsCenterTitle.textContent = ( localizeWithPath(_moduleId, 0, "No new notifications"));
            clearAllAction.enabled = false;
        }
        else {
            notificationsCenterTitle.textContent = ( localizeWithPath(_moduleId, 1, "Notifications"));
            clearAllAction.enabled = ( (this.model.notifications.some(notification => !notification.hasProgress)));
        }
    }
    create() {
        this.notificationsCenterContainer = document.createElement('div');
        this.notificationsCenterContainer.classList.add('notifications-center');
        this.notificationsCenterHeader = document.createElement('div');
        this.notificationsCenterHeader.classList.add('notifications-center-header');
        this.notificationsCenterContainer.appendChild(this.notificationsCenterHeader);
        this.notificationsCenterTitle = document.createElement('span');
        this.notificationsCenterTitle.classList.add('notifications-center-header-title');
        this.notificationsCenterHeader.appendChild(this.notificationsCenterTitle);
        const toolbarContainer = document.createElement('div');
        toolbarContainer.classList.add('notifications-center-header-toolbar');
        this.notificationsCenterHeader.appendChild(toolbarContainer);
        const actionRunner = this._register(this.instantiationService.createInstance(NotificationActionRunner));
        const that = this;
        const notificationsToolBar = this._register(( (new ActionBar(toolbarContainer, {
            ariaLabel: ( localizeWithPath(_moduleId, 2, "Notification Center Actions")),
            actionRunner,
            actionViewItemProvider: (action, options) => {
                if (action.id === ConfigureDoNotDisturbAction.ID) {
                    return this._register(this.instantiationService.createInstance(DropdownMenuActionViewItem, action, {
                        getActions() {
                            const actions = [toAction({
                                    id: ToggleDoNotDisturbAction.ID,
                                    label: that.notificationService.getFilter() === NotificationsFilter.OFF ? ( localizeWithPath(_moduleId, 3, "Enable Do Not Disturb Mode")) : ( localizeWithPath(_moduleId, 4, "Disable Do Not Disturb Mode")),
                                    run: () => that.notificationService.setFilter(that.notificationService.getFilter() === NotificationsFilter.OFF ? NotificationsFilter.ERROR : NotificationsFilter.OFF)
                                })];
                            const sortedFilters = that.notificationService.getFilters().sort((a, b) => a.label.localeCompare(b.label));
                            for (const source of sortedFilters.slice(0, NotificationsCenter_1.MAX_NOTIFICATION_SOURCES)) {
                                if (actions.length === 1) {
                                    actions.push(( (new Separator())));
                                }
                                actions.push(toAction({
                                    id: `${ToggleDoNotDisturbAction.ID}.${source.id}`,
                                    label: source.label,
                                    checked: source.filter !== NotificationsFilter.ERROR,
                                    run: () => that.notificationService.setFilter({
                                        ...source,
                                        filter: source.filter === NotificationsFilter.ERROR ? NotificationsFilter.OFF : NotificationsFilter.ERROR
                                    })
                                }));
                            }
                            if (sortedFilters.length > NotificationsCenter_1.MAX_NOTIFICATION_SOURCES) {
                                actions.push(( (new Separator())));
                                actions.push(that._register(that.instantiationService.createInstance(ToggleDoNotDisturbBySourceAction, ToggleDoNotDisturbBySourceAction.ID, ( localizeWithPath(_moduleId, 5, "More…")))));
                            }
                            return actions;
                        },
                    }, this.contextMenuService, {
                        ...options,
                        actionRunner,
                        classNames: action.class,
                        keybindingProvider: action => this.keybindingService.lookupKeybinding(action.id)
                    }));
                }
                return undefined;
            }
        }))));
        this.clearAllAction = this._register(this.instantiationService.createInstance(ClearAllNotificationsAction, ClearAllNotificationsAction.ID, ClearAllNotificationsAction.LABEL));
        notificationsToolBar.push(this.clearAllAction, { icon: true, label: false, keybinding: this.getKeybindingLabel(this.clearAllAction) });
        this.configureDoNotDisturbAction = this._register(this.instantiationService.createInstance(ConfigureDoNotDisturbAction, ConfigureDoNotDisturbAction.ID, ConfigureDoNotDisturbAction.LABEL));
        notificationsToolBar.push(this.configureDoNotDisturbAction, { icon: true, label: false });
        const hideAllAction = this._register(this.instantiationService.createInstance(HideNotificationsCenterAction, HideNotificationsCenterAction.ID, HideNotificationsCenterAction.LABEL));
        notificationsToolBar.push(hideAllAction, { icon: true, label: false, keybinding: this.getKeybindingLabel(hideAllAction) });
        this.notificationsList = this.instantiationService.createInstance(NotificationsList, this.notificationsCenterContainer, {
            widgetAriaLabel: ( localizeWithPath(_moduleId, 6, "Notifications Center"))
        });
        this.container.appendChild(this.notificationsCenterContainer);
    }
    getKeybindingLabel(action) {
        const keybinding = this.keybindingService.lookupKeybinding(action.id);
        return keybinding ? keybinding.getLabel() : null;
    }
    onDidChangeNotification(e) {
        if (!this._isVisible) {
            return;
        }
        let focusEditor = false;
        const [notificationsList, notificationsCenterContainer] = assertAllDefined(this.notificationsList, this.notificationsCenterContainer);
        switch (e.kind) {
            case NotificationChangeType.ADD:
                notificationsList.updateNotificationsList(e.index, 0, [e.item]);
                e.item.updateVisibility(true);
                break;
            case NotificationChangeType.CHANGE:
                switch (e.detail) {
                    case NotificationViewItemContentChangeKind.ACTIONS:
                        notificationsList.updateNotificationsList(e.index, 1, [e.item]);
                        break;
                    case NotificationViewItemContentChangeKind.MESSAGE:
                        if (e.item.expanded) {
                            notificationsList.updateNotificationHeight(e.item);
                        }
                        break;
                }
                break;
            case NotificationChangeType.EXPAND_COLLAPSE:
                notificationsList.updateNotificationsList(e.index, 1, [e.item]);
                break;
            case NotificationChangeType.REMOVE:
                focusEditor = isAncestorOfActiveElement(notificationsCenterContainer);
                notificationsList.updateNotificationsList(e.index, 1);
                e.item.updateVisibility(false);
                break;
        }
        this.updateTitle();
        if (this.model.notifications.length === 0) {
            this.hide();
            if (focusEditor) {
                this.editorGroupService.activeGroup.focus();
            }
        }
    }
    hide() {
        if (!this._isVisible || !this.notificationsCenterContainer || !this.notificationsList) {
            return;
        }
        const focusEditor = isAncestorOfActiveElement(this.notificationsCenterContainer);
        this._isVisible = false;
        this.notificationsCenterContainer.classList.remove('visible');
        this.notificationsList.hide();
        this.model.notifications.forEach(notification => notification.updateVisibility(false));
        this.notificationsCenterVisibleContextKey.set(false);
        this._onDidChangeVisibility.fire();
        if (focusEditor) {
            this.editorGroupService.activeGroup.focus();
        }
    }
    updateStyles() {
        if (this.notificationsCenterContainer && this.notificationsCenterHeader) {
            const widgetShadowColor = this.getColor(widgetShadow);
            this.notificationsCenterContainer.style.boxShadow = widgetShadowColor ? `0 0 8px 2px ${widgetShadowColor}` : '';
            const borderColor = this.getColor(NOTIFICATIONS_CENTER_BORDER);
            this.notificationsCenterContainer.style.border = borderColor ? `1px solid ${borderColor}` : '';
            const headerForeground = this.getColor(NOTIFICATIONS_CENTER_HEADER_FOREGROUND);
            this.notificationsCenterHeader.style.color = headerForeground ?? '';
            const headerBackground = this.getColor(NOTIFICATIONS_CENTER_HEADER_BACKGROUND);
            this.notificationsCenterHeader.style.background = headerBackground ?? '';
        }
    }
    layout(dimension) {
        this.workbenchDimensions = dimension;
        if (this._isVisible && this.notificationsCenterContainer) {
            const maxWidth = NotificationsCenter_1.MAX_DIMENSIONS.width;
            const maxHeight = NotificationsCenter_1.MAX_DIMENSIONS.height;
            let availableWidth = maxWidth;
            let availableHeight = maxHeight;
            if (this.workbenchDimensions) {
                availableWidth = this.workbenchDimensions.width;
                availableWidth -= (2 * 8);
                availableHeight = this.workbenchDimensions.height - 35 ;
                if (this.layoutService.isVisible(Parts.STATUSBAR_PART, mainWindow)) {
                    availableHeight -= 22;
                }
                if (this.layoutService.isVisible(Parts.TITLEBAR_PART, mainWindow)) {
                    availableHeight -= 22;
                }
                availableHeight -= (2 * 12);
            }
            const notificationsList = assertIsDefined(this.notificationsList);
            notificationsList.layout(Math.min(maxWidth, availableWidth), Math.min(maxHeight, availableHeight));
        }
    }
    clearAll() {
        this.hide();
        for (const notification of [...this.model.notifications] ) {
            if (!notification.hasProgress) {
                notification.close();
            }
            this.accessibilitySignalService.playSignal(AccessibilitySignal.clear);
        }
    }
};
NotificationsCenter = NotificationsCenter_1 = ( (__decorate([
    ( (__param(2, IThemeService))),
    ( (__param(3, IInstantiationService))),
    ( (__param(4, IWorkbenchLayoutService))),
    ( (__param(5, IContextKeyService))),
    ( (__param(6, IEditorGroupsService))),
    ( (__param(7, IKeybindingService))),
    ( (__param(8, INotificationService))),
    ( (__param(9, IAccessibilitySignalService))),
    ( (__param(10, IContextMenuService)))
], NotificationsCenter)));
export { NotificationsCenter };
