
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { CommandsRegistry } from '../../../../platform/commands/common/commands.js';
import { ContextKeyExpr } from '../../../../platform/contextkey/common/contextkey.js';
import { KeybindingsRegistry, KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';
import { KeyChord, KeyMod as KeyMod$1, KeyCode } from '../../../../base/common/keyCodes.js';
import { isNotificationViewItem, NotificationsSettings, NotificationsPosition } from '../../../common/notifications.js';
import { MenuRegistry, MenuId, registerAction2, Action2 } from '../../../../platform/actions/common/actions.js';
import { localize, localize2 } from '../../../../nls.js';
import { WorkbenchList } from '../../../../platform/list/browser/listService.js';
import { IListService } from '../../../../platform/list/browser/listService.service.js';
import { ITelemetryService } from '../../../../platform/telemetry/common/telemetry.service.js';
import { NotificationsCenterVisibleContext, NotificationFocusedContext, NotificationsToastsVisibleContext } from '../../../common/contextkeys.js';
import { NotificationsFilter } from '../../../../platform/notification/common/notification.js';
import { INotificationService } from '../../../../platform/notification/common/notification.service.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { ActionRunner } from '../../../../base/common/actions.js';
import { IQuickInputService } from '../../../../platform/quickinput/common/quickInput.service.js';
import { DisposableStore } from '../../../../base/common/lifecycle.js';
import { AccessibilitySignal } from '../../../../platform/accessibilitySignal/browser/accessibilitySignalService.js';
import { IAccessibilitySignalService } from '../../../../platform/accessibilitySignal/browser/accessibilitySignalService.service.js';
import { Codicon } from '../../../../base/common/codicons.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { CONTEXT_ACCESSIBILITY_MODE_ENABLED } from '../../../../platform/accessibility/common/accessibility.js';

const SHOW_NOTIFICATIONS_CENTER = "notifications.showList";
const HIDE_NOTIFICATIONS_CENTER = "notifications.hideList";
const TOGGLE_NOTIFICATIONS_CENTER = "notifications.toggleList";
const HIDE_NOTIFICATION_TOAST = "notifications.hideToasts";
const FOCUS_NOTIFICATION_TOAST = "notifications.focusToasts";
const FOCUS_NEXT_NOTIFICATION_TOAST = "notifications.focusNextToast";
const FOCUS_PREVIOUS_NOTIFICATION_TOAST = "notifications.focusPreviousToast";
const FOCUS_FIRST_NOTIFICATION_TOAST = "notifications.focusFirstToast";
const FOCUS_LAST_NOTIFICATION_TOAST = "notifications.focusLastToast";
const COLLAPSE_NOTIFICATION = "notification.collapse";
const EXPAND_NOTIFICATION = "notification.expand";
const ACCEPT_PRIMARY_ACTION_NOTIFICATION = "notification.acceptPrimaryAction";
const TOGGLE_NOTIFICATION = "notification.toggle";
const CLEAR_NOTIFICATION = "notification.clear";
const CLEAR_ALL_NOTIFICATIONS = "notifications.clearAll";
const TOGGLE_DO_NOT_DISTURB_MODE = "notifications.toggleDoNotDisturbMode";
const TOGGLE_DO_NOT_DISTURB_MODE_BY_SOURCE = "notifications.toggleDoNotDisturbModeBySource";
function getNotificationFromContext(listService, context) {
    if (isNotificationViewItem(context)) {
        return context;
    }
    const list = listService.lastFocusedList;
    if (list instanceof WorkbenchList) {
        let element = list.getFocusedElements()[0];
        if (!isNotificationViewItem(element)) {
            if (list.isDOMFocused()) {
                element = list.element(0);
            }
        }
        if (isNotificationViewItem(element)) {
            return element;
        }
    }
    return undefined;
}
function registerNotificationCommands(center, toasts, model) {
    KeybindingsRegistry.registerCommandAndKeybindingRule({
        id: SHOW_NOTIFICATIONS_CENTER,
        weight: KeybindingWeight.WorkbenchContrib,
        primary: KeyChord(
            KeyMod$1.CtrlCmd | KeyCode.KeyK,
            KeyMod$1.CtrlCmd | KeyMod$1.Shift | KeyCode.KeyN
        ),
        handler: () => {
            toasts.hide();
            center.show();
        }
    });
    KeybindingsRegistry.registerCommandAndKeybindingRule({
        id: HIDE_NOTIFICATIONS_CENTER,
        weight: KeybindingWeight.WorkbenchContrib + 50,
        when: NotificationsCenterVisibleContext,
        primary: KeyCode.Escape,
        handler: () => center.hide()
    });
    CommandsRegistry.registerCommand(TOGGLE_NOTIFICATIONS_CENTER, () => {
        if (center.isVisible) {
            center.hide();
        } else {
            toasts.hide();
            center.show();
        }
    });
    KeybindingsRegistry.registerCommandAndKeybindingRule({
        id: CLEAR_NOTIFICATION,
        weight: KeybindingWeight.WorkbenchContrib,
        when: NotificationFocusedContext,
        primary: KeyCode.Delete,
        mac: {
            primary: KeyMod$1.CtrlCmd | KeyCode.Backspace
        },
        handler: (accessor, args) => {
            const accessibilitySignalService = accessor.get(IAccessibilitySignalService);
            const notification = getNotificationFromContext(accessor.get(IListService), args);
            if (notification && !notification.hasProgress) {
                notification.close();
                accessibilitySignalService.playSignal(AccessibilitySignal.clear);
            }
        }
    });
    KeybindingsRegistry.registerCommandAndKeybindingRule({
        id: EXPAND_NOTIFICATION,
        weight: KeybindingWeight.WorkbenchContrib,
        when: NotificationFocusedContext,
        primary: KeyCode.RightArrow,
        handler: (accessor, args) => {
            const notification = getNotificationFromContext(accessor.get(IListService), args);
            notification?.expand();
        }
    });
    KeybindingsRegistry.registerCommandAndKeybindingRule({
        id: ACCEPT_PRIMARY_ACTION_NOTIFICATION,
        weight: KeybindingWeight.WorkbenchContrib + 1,
        when: ( ContextKeyExpr.and(CONTEXT_ACCESSIBILITY_MODE_ENABLED, ( ContextKeyExpr.or(NotificationFocusedContext, NotificationsToastsVisibleContext)))),
        primary: KeyMod$1.CtrlCmd | KeyMod$1.Shift | KeyCode.KeyA,
        handler: accessor => {
            const actionRunner = accessor.get(IInstantiationService).createInstance(NotificationActionRunner);
            const notification = getNotificationFromContext(accessor.get(IListService)) || model.notifications.at(0);
            if (!notification) {
                return;
            }
            const primaryAction = notification.actions?.primary ? notification.actions.primary.at(0) : undefined;
            if (!primaryAction) {
                return;
            }
            actionRunner.run(primaryAction, notification);
            notification.close();
            actionRunner.dispose();
        }
    });
    KeybindingsRegistry.registerCommandAndKeybindingRule({
        id: COLLAPSE_NOTIFICATION,
        weight: KeybindingWeight.WorkbenchContrib,
        when: NotificationFocusedContext,
        primary: KeyCode.LeftArrow,
        handler: (accessor, args) => {
            const notification = getNotificationFromContext(accessor.get(IListService), args);
            notification?.collapse();
        }
    });
    KeybindingsRegistry.registerCommandAndKeybindingRule({
        id: TOGGLE_NOTIFICATION,
        weight: KeybindingWeight.WorkbenchContrib,
        when: NotificationFocusedContext,
        primary: KeyCode.Space,
        secondary: [KeyCode.Enter],
        handler: accessor => {
            const notification = getNotificationFromContext(accessor.get(IListService));
            notification?.toggle();
        }
    });
    CommandsRegistry.registerCommand(HIDE_NOTIFICATION_TOAST, accessor => {
        toasts.hide();
    });
    KeybindingsRegistry.registerKeybindingRule({
        id: HIDE_NOTIFICATION_TOAST,
        weight: KeybindingWeight.WorkbenchContrib - 50,
        when: NotificationsToastsVisibleContext,
        primary: KeyCode.Escape
    });
    KeybindingsRegistry.registerKeybindingRule({
        id: HIDE_NOTIFICATION_TOAST,
        weight: KeybindingWeight.WorkbenchContrib + 100,
        when: ( ContextKeyExpr.and(NotificationsToastsVisibleContext, NotificationFocusedContext)),
        primary: KeyCode.Escape
    });
    CommandsRegistry.registerCommand(FOCUS_NOTIFICATION_TOAST, () => toasts.focus());
    KeybindingsRegistry.registerCommandAndKeybindingRule({
        id: FOCUS_NEXT_NOTIFICATION_TOAST,
        weight: KeybindingWeight.WorkbenchContrib,
        when: ( ContextKeyExpr.and(NotificationFocusedContext, NotificationsToastsVisibleContext)),
        primary: KeyCode.DownArrow,
        handler: () => {
            toasts.focusNext();
        }
    });
    KeybindingsRegistry.registerCommandAndKeybindingRule({
        id: FOCUS_PREVIOUS_NOTIFICATION_TOAST,
        weight: KeybindingWeight.WorkbenchContrib,
        when: ( ContextKeyExpr.and(NotificationFocusedContext, NotificationsToastsVisibleContext)),
        primary: KeyCode.UpArrow,
        handler: () => {
            toasts.focusPrevious();
        }
    });
    KeybindingsRegistry.registerCommandAndKeybindingRule({
        id: FOCUS_FIRST_NOTIFICATION_TOAST,
        weight: KeybindingWeight.WorkbenchContrib,
        when: ( ContextKeyExpr.and(NotificationFocusedContext, NotificationsToastsVisibleContext)),
        primary: KeyCode.PageUp,
        secondary: [KeyCode.Home],
        handler: () => {
            toasts.focusFirst();
        }
    });
    KeybindingsRegistry.registerCommandAndKeybindingRule({
        id: FOCUS_LAST_NOTIFICATION_TOAST,
        weight: KeybindingWeight.WorkbenchContrib,
        when: ( ContextKeyExpr.and(NotificationFocusedContext, NotificationsToastsVisibleContext)),
        primary: KeyCode.PageDown,
        secondary: [KeyCode.End],
        handler: () => {
            toasts.focusLast();
        }
    });
    CommandsRegistry.registerCommand(CLEAR_ALL_NOTIFICATIONS, () => center.clearAll());
    CommandsRegistry.registerCommand(TOGGLE_DO_NOT_DISTURB_MODE, accessor => {
        const notificationService = accessor.get(INotificationService);
        notificationService.setFilter(
            notificationService.getFilter() === NotificationsFilter.ERROR ? NotificationsFilter.OFF : NotificationsFilter.ERROR
        );
    });
    CommandsRegistry.registerCommand(TOGGLE_DO_NOT_DISTURB_MODE_BY_SOURCE, accessor => {
        const notificationService = accessor.get(INotificationService);
        const quickInputService = accessor.get(IQuickInputService);
        const sortedFilters = notificationService.getFilters().sort((a, b) => a.label.localeCompare(b.label));
        const disposables = ( new DisposableStore());
        const picker = disposables.add(quickInputService.createQuickPick());
        picker.items = ( sortedFilters.map(source => ({
            id: source.id,
            label: source.label,
            tooltip: `${source.label} (${source.id})`,
            filter: source.filter
        })));
        picker.canSelectMany = true;
        picker.placeholder = ( localize(3754, "Select sources to enable all notifications from"));
        picker.selectedItems = picker.items.filter(item => item.filter === NotificationsFilter.OFF);
        picker.show();
        disposables.add(picker.onDidAccept(async () => {
            for (const item of picker.items) {
                notificationService.setFilter({
                    id: item.id,
                    label: item.label,
                    filter: picker.selectedItems.includes(item) ? NotificationsFilter.OFF : NotificationsFilter.ERROR
                });
            }
            picker.hide();
        }));
        disposables.add(picker.onDidHide(() => disposables.dispose()));
    });
    const category = ( localize2(3755, "Notifications"));
    MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
        command: {
            id: SHOW_NOTIFICATIONS_CENTER,
            title: ( localize2(3756, "Show Notifications")),
            category
        }
    });
    MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
        command: {
            id: HIDE_NOTIFICATIONS_CENTER,
            title: ( localize2(3757, "Hide Notifications")),
            category
        },
        when: NotificationsCenterVisibleContext
    });
    MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
        command: {
            id: CLEAR_ALL_NOTIFICATIONS,
            title: ( localize2(3758, "Clear All Notifications")),
            category
        }
    });
    MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
        command: {
            id: ACCEPT_PRIMARY_ACTION_NOTIFICATION,
            title: ( localize2(3759, "Accept Notification Primary Action")),
            category
        }
    });
    MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
        command: {
            id: TOGGLE_DO_NOT_DISTURB_MODE,
            title: ( localize2(3760, "Toggle Do Not Disturb Mode")),
            category
        }
    });
    MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
        command: {
            id: TOGGLE_DO_NOT_DISTURB_MODE_BY_SOURCE,
            title: ( localize2(3761, "Toggle Do Not Disturb Mode By Source...")),
            category
        }
    });
    MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
        command: {
            id: FOCUS_NOTIFICATION_TOAST,
            title: ( localize2(3762, "Focus Notification Toast")),
            category
        },
        when: NotificationsToastsVisibleContext
    });
    MenuRegistry.appendMenuItem(MenuId.TitleBar, {
        command: {
            id: TOGGLE_NOTIFICATIONS_CENTER,
            title: ( localize(3763, "Toggle Notifications")),
            icon: Codicon.bell
        },
        group: "navigation",
        order: 10000,
        when: ( ContextKeyExpr.and(( ContextKeyExpr.equals(
            `config.${NotificationsSettings.NOTIFICATIONS_POSITION}`,
            NotificationsPosition.TOP_RIGHT
        )), ( ContextKeyExpr.equals(`config.${NotificationsSettings.NOTIFICATIONS_BUTTON}`, true))))
    });
}
registerAction2(class SetNotificationsPositionBottomRight extends Action2 {
    constructor() {
        super({
            id: "workbench.action.setNotificationsPosition.bottomRight",
            title: ( localize2(3764, "Bottom Right")),
            toggled: ( ContextKeyExpr.equals(
                `config.${NotificationsSettings.NOTIFICATIONS_POSITION}`,
                NotificationsPosition.BOTTOM_RIGHT
            )),
            menu: {
                id: MenuId.NotificationsCenterPositionMenu,
                order: 1
            }
        });
    }
    run(accessor) {
        accessor.get(IConfigurationService).updateValue(
            NotificationsSettings.NOTIFICATIONS_POSITION,
            NotificationsPosition.BOTTOM_RIGHT
        );
    }
});
registerAction2(class SetNotificationsPositionBottomLeft extends Action2 {
    constructor() {
        super({
            id: "workbench.action.setNotificationsPosition.bottomLeft",
            title: ( localize2(3765, "Bottom Left")),
            toggled: ( ContextKeyExpr.equals(
                `config.${NotificationsSettings.NOTIFICATIONS_POSITION}`,
                NotificationsPosition.BOTTOM_LEFT
            )),
            menu: {
                id: MenuId.NotificationsCenterPositionMenu,
                order: 2
            }
        });
    }
    run(accessor) {
        accessor.get(IConfigurationService).updateValue(
            NotificationsSettings.NOTIFICATIONS_POSITION,
            NotificationsPosition.BOTTOM_LEFT
        );
    }
});
registerAction2(class SetNotificationsPositionTopRight extends Action2 {
    constructor() {
        super({
            id: "workbench.action.setNotificationsPosition.topRight",
            title: ( localize2(3766, "Top Right")),
            toggled: ( ContextKeyExpr.equals(
                `config.${NotificationsSettings.NOTIFICATIONS_POSITION}`,
                NotificationsPosition.TOP_RIGHT
            )),
            menu: {
                id: MenuId.NotificationsCenterPositionMenu,
                order: 3
            }
        });
    }
    run(accessor) {
        accessor.get(IConfigurationService).updateValue(
            NotificationsSettings.NOTIFICATIONS_POSITION,
            NotificationsPosition.TOP_RIGHT
        );
    }
});
let NotificationActionRunner = class NotificationActionRunner extends ActionRunner {
    constructor(telemetryService, notificationService) {
        super();
        this.telemetryService = telemetryService;
        this.notificationService = notificationService;
    }
    async runAction(action, context) {
        this.telemetryService.publicLog2("workbenchActionExecuted", {
            id: action.id,
            from: "message"
        });
        try {
            await super.runAction(action, context);
        } catch (error) {
            this.notificationService.error(error);
        }
    }
};
NotificationActionRunner = ( __decorate([( __param(0, ITelemetryService)), ( __param(1, INotificationService))], NotificationActionRunner));

export { ACCEPT_PRIMARY_ACTION_NOTIFICATION, CLEAR_ALL_NOTIFICATIONS, CLEAR_NOTIFICATION, COLLAPSE_NOTIFICATION, EXPAND_NOTIFICATION, HIDE_NOTIFICATIONS_CENTER, HIDE_NOTIFICATION_TOAST, NotificationActionRunner, SHOW_NOTIFICATIONS_CENTER, TOGGLE_DO_NOT_DISTURB_MODE, TOGGLE_DO_NOT_DISTURB_MODE_BY_SOURCE, getNotificationFromContext, registerNotificationCommands };
