
import { localize, localize2 } from '../../../../nls.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
import { IStorageService } from '../../../../platform/storage/common/storage.service.js';
import { LayoutSettings } from '../../../services/layout/browser/layoutService.js';
import { Action2, MenuId, registerAction2 } from '../../../../platform/actions/common/actions.js';
import { ContextKeyExpr } from '../../../../platform/contextkey/common/contextkey.js';
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.service.js';
import { ACCOUNTS_ACTIVITY_ID, GLOBAL_ACTIVITY_ID } from '../../../common/activity.js';
import { IsCompactTitleBarContext, TitleBarStyleContext, IsMainWindowFullscreenContext, TitleBarVisibleContext } from '../../../common/contextkeys.js';
import { TitlebarStyle, TitleBarSetting, CustomTitleBarVisibility } from '../../../../platform/window/common/window.js';
import { NotificationsSettings, NotificationsPosition } from '../../../common/notifications.js';

const TitleBarLeadingActionsGroup = "0_leading";
class ToggleTitleBarConfigAction extends Action2 {
    constructor(section, title, description, order, when) {
        super({
            id: `toggle.${section}`,
            title,
            metadata: description ? {
                description
            } : undefined,
            toggled: ( ContextKeyExpr.equals(`config.${section}`, true)),
            menu: [{
                id: MenuId.TitleBarContext,
                when,
                order,
                group: "2_config"
            }, {
                id: MenuId.TitleBarTitleContext,
                when,
                order,
                group: "2_config"
            }]
        });
        this.section = section;
    }
    run(accessor, ...args) {
        const configService = accessor.get(IConfigurationService);
        const value = configService.getValue(this.section);
        configService.updateValue(this.section, !value);
    }
}
registerAction2(class ToggleCommandCenter extends ToggleTitleBarConfigAction {
    constructor() {
        super(LayoutSettings.COMMAND_CENTER, ( localize(3873, "Command Center")), ( localize(3874, "Toggle visibility of the Command Center in title bar")), 1, ( IsCompactTitleBarContext.toNegated()));
    }
});
registerAction2(class ToggleNavigationControl extends ToggleTitleBarConfigAction {
    constructor() {
        super("workbench.navigationControl.enabled", ( localize(3875, "Navigation Controls")), ( localize(3876, "Toggle visibility of the Navigation Controls in title bar")), 2, ( ContextKeyExpr.and(( IsCompactTitleBarContext.toNegated()), ( ContextKeyExpr.has(`config.${LayoutSettings.COMMAND_CENTER}`)))));
    }
});
registerAction2(class ToggleLayoutControl extends ToggleTitleBarConfigAction {
    constructor() {
        super(LayoutSettings.LAYOUT_ACTIONS, ( localize(3877, "Layout Controls")), ( localize(3878, "Toggle visibility of the Layout Controls in title bar")), 4);
    }
});
registerAction2(class ToggleNotificationsButton extends ToggleTitleBarConfigAction {
    constructor() {
        super(NotificationsSettings.NOTIFICATIONS_BUTTON, ( localize(3879, "Notifications")), ( localize(3880, "Toggle visibility of the Notifications button in title bar")), 5, ( ContextKeyExpr.equals(
            `config.${NotificationsSettings.NOTIFICATIONS_POSITION}`,
            NotificationsPosition.TOP_RIGHT
        )));
    }
});
registerAction2(class ToggleCustomTitleBar extends Action2 {
    constructor() {
        super({
            id: `toggle.${TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY}`,
            title: ( localize(3881, "Hide Custom Title Bar")),
            menu: [{
                id: MenuId.TitleBarContext,
                order: 0,
                when: ( ContextKeyExpr.equals(TitleBarStyleContext.key, TitlebarStyle.NATIVE)),
                group: "3_toggle"
            }, {
                id: MenuId.TitleBarTitleContext,
                order: 0,
                when: ( ContextKeyExpr.equals(TitleBarStyleContext.key, TitlebarStyle.NATIVE)),
                group: "3_toggle"
            }]
        });
    }
    run(accessor, ...args) {
        const configService = accessor.get(IConfigurationService);
        configService.updateValue(
            TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY,
            CustomTitleBarVisibility.NEVER
        );
    }
});
registerAction2(class ToggleCustomTitleBarWindowed extends Action2 {
    constructor() {
        super({
            id: `toggle.${TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY}.windowed`,
            title: ( localize(3882, "Hide Custom Title Bar In Full Screen")),
            menu: [{
                id: MenuId.TitleBarContext,
                order: 1,
                when: IsMainWindowFullscreenContext,
                group: "3_toggle"
            }, {
                id: MenuId.TitleBarTitleContext,
                order: 1,
                when: IsMainWindowFullscreenContext,
                group: "3_toggle"
            }]
        });
    }
    run(accessor, ...args) {
        const configService = accessor.get(IConfigurationService);
        configService.updateValue(
            TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY,
            CustomTitleBarVisibility.WINDOWED
        );
    }
});
class ToggleCustomTitleBar extends Action2 {
    constructor() {
        super({
            id: `toggle.toggleCustomTitleBar`,
            title: ( localize(3883, "Custom Title Bar")),
            toggled: TitleBarVisibleContext,
            menu: [{
                id: MenuId.MenubarAppearanceMenu,
                order: 6,
                when: ( ContextKeyExpr.or(( ContextKeyExpr.and(( ContextKeyExpr.equals(TitleBarStyleContext.key, TitlebarStyle.NATIVE)), ( ContextKeyExpr.and(( ContextKeyExpr.equals("config.workbench.layoutControl.enabled", false)), ( ContextKeyExpr.equals("config.window.commandCenter", false)), ( ContextKeyExpr.notEquals("config.workbench.editor.editorActionsLocation", "titleBar")), ( ContextKeyExpr.notEquals("config.workbench.activityBar.location", "top")), ( ContextKeyExpr.notEquals("config.workbench.activityBar.location", "bottom"))))?.negate())), IsMainWindowFullscreenContext)),
                group: "2_workbench_layout"
            }]
        });
    }
    run(accessor, ...args) {
        const configService = accessor.get(IConfigurationService);
        const contextKeyService = accessor.get(IContextKeyService);
        const titleBarVisibility = configService.getValue(TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY);
        switch (titleBarVisibility) {
        case CustomTitleBarVisibility.NEVER:
            configService.updateValue(TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY, CustomTitleBarVisibility.AUTO);
            break;
        case CustomTitleBarVisibility.WINDOWED:
            {
                const isFullScreen = IsMainWindowFullscreenContext.evaluate(contextKeyService.getContext(null));
                if (isFullScreen) {
                    configService.updateValue(TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY, CustomTitleBarVisibility.AUTO);
                } else {
                    configService.updateValue(
                        TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY,
                        CustomTitleBarVisibility.NEVER
                    );
                }
                break;
            }
        case CustomTitleBarVisibility.AUTO:
        default:
            configService.updateValue(
                TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY,
                CustomTitleBarVisibility.NEVER
            );
            break;
        }
    }
}
registerAction2(ToggleCustomTitleBar);
registerAction2(class ShowCustomTitleBar extends Action2 {
    constructor() {
        super({
            id: `showCustomTitleBar`,
            title: ( localize2(3884, "Show Custom Title Bar")),
            precondition: ( TitleBarVisibleContext.negate()),
            f1: true
        });
    }
    run(accessor, ...args) {
        const configService = accessor.get(IConfigurationService);
        configService.updateValue(TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY, CustomTitleBarVisibility.AUTO);
    }
});
registerAction2(class HideCustomTitleBar extends Action2 {
    constructor() {
        super({
            id: `hideCustomTitleBar`,
            title: ( localize2(3885, "Hide Custom Title Bar")),
            precondition: TitleBarVisibleContext,
            f1: true
        });
    }
    run(accessor, ...args) {
        const configService = accessor.get(IConfigurationService);
        configService.updateValue(
            TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY,
            CustomTitleBarVisibility.NEVER
        );
    }
});
registerAction2(class HideCustomTitleBar extends Action2 {
    constructor() {
        super({
            id: `hideCustomTitleBarInFullScreen`,
            title: ( localize2(3886, "Hide Custom Title Bar In Full Screen")),
            precondition: ( ContextKeyExpr.and(TitleBarVisibleContext, IsMainWindowFullscreenContext)),
            f1: true
        });
    }
    run(accessor, ...args) {
        const configService = accessor.get(IConfigurationService);
        configService.updateValue(
            TitleBarSetting.CUSTOM_TITLE_BAR_VISIBILITY,
            CustomTitleBarVisibility.WINDOWED
        );
    }
});
registerAction2(class ToggleEditorActions extends Action2 {
    static {
        this.settingsID = `workbench.editor.editorActionsLocation`;
    }
    constructor() {
        const titleBarContextCondition = ( ContextKeyExpr.and(( ( ContextKeyExpr.equals(`config.workbench.editor.showTabs`, "none")).negate()), ( ContextKeyExpr.equals(`config.${ToggleEditorActions.settingsID}`, "default"))))?.negate();
        super({
            id: `toggle.${ToggleEditorActions.settingsID}`,
            title: ( localize(3887, "Editor Actions")),
            toggled: ( ( ContextKeyExpr.equals(`config.${ToggleEditorActions.settingsID}`, "hidden")).negate()),
            menu: [{
                id: MenuId.TitleBarContext,
                order: 3,
                when: titleBarContextCondition,
                group: "2_config"
            }, {
                id: MenuId.TitleBarTitleContext,
                order: 3,
                when: titleBarContextCondition,
                group: "2_config"
            }]
        });
    }
    run(accessor, ...args) {
        const configService = accessor.get(IConfigurationService);
        const storageService = accessor.get(IStorageService);
        const location = configService.getValue(ToggleEditorActions.settingsID);
        if (location === "hidden") {
            const showTabs = configService.getValue(LayoutSettings.EDITOR_TABS_MODE);
            if (showTabs !== "none") {
                configService.updateValue(ToggleEditorActions.settingsID, "titleBar");
            }
            else {
                const storedValue = storageService.get(ToggleEditorActions.settingsID, StorageScope.PROFILE);
                configService.updateValue(ToggleEditorActions.settingsID, storedValue ?? "default");
            }
            storageService.remove(ToggleEditorActions.settingsID, StorageScope.PROFILE);
        }
        else {
            configService.updateValue(ToggleEditorActions.settingsID, "hidden");
            storageService.store(
                ToggleEditorActions.settingsID,
                location,
                StorageScope.PROFILE,
                StorageTarget.USER
            );
        }
    }
});
const ACCOUNTS_ACTIVITY_TILE_ACTION = {
    id: ACCOUNTS_ACTIVITY_ID,
    label: ( localize(3888, "Accounts")),
    tooltip: ( localize(3888, "Accounts")),
    class: undefined,
    enabled: true,
    run: function() {}
};
const GLOBAL_ACTIVITY_TITLE_ACTION = {
    id: GLOBAL_ACTIVITY_ID,
    label: ( localize(3889, "Manage")),
    tooltip: ( localize(3889, "Manage")),
    class: undefined,
    enabled: true,
    run: function() {}
};

export { ACCOUNTS_ACTIVITY_TILE_ACTION, GLOBAL_ACTIVITY_TITLE_ACTION, TitleBarLeadingActionsGroup, ToggleTitleBarConfigAction };
