
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { h, append, clearNode, $ } from '../../../../base/browser/dom.js';
import { KeybindingLabel } from '../../../../base/browser/ui/keybindingLabel/keybindingLabel.js';
import { coalesce, shuffle } from '../../../../base/common/arrays.js';
import { Disposable, DisposableStore } from '../../../../base/common/lifecycle.js';
import { isMacintosh, isWeb, OS } from '../../../../base/common/platform.js';
import { localize } from '../../../../nls.js';
import { CommandsRegistry } from '../../../../platform/commands/common/commands.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { ContextKeyExpr } from '../../../../platform/contextkey/common/contextkey.js';
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.service.js';
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.service.js';
import { StorageScope, WillSaveStateReason, StorageTarget } from '../../../../platform/storage/common/storage.js';
import { IStorageService } from '../../../../platform/storage/common/storage.service.js';
import { defaultKeybindingLabelStyles } from '../../../../platform/theme/browser/defaultStyles.js';
import { WorkbenchState } from '../../../../platform/workspace/common/workspace.js';
import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.service.js';

var EditorGroupWatermark_1;
const showChatContextKey = ( ContextKeyExpr.and(( ContextKeyExpr.equals("chatSetupHidden", false)), ( ContextKeyExpr.equals("chatSetupDisabled", false))));
const openChat = {
    text: ( localize(3481, "Open Chat")),
    id: "workbench.action.chat.open",
    when: {
        native: showChatContextKey,
        web: showChatContextKey
    }
};
const showCommands = {
    text: ( localize(3482, "Show All Commands")),
    id: "workbench.action.showCommands"
};
const gotoFile = {
    text: ( localize(3483, "Go to File")),
    id: "workbench.action.quickOpen"
};
const openFile = {
    text: ( localize(3484, "Open File")),
    id: "workbench.action.files.openFile"
};
const openFolder = {
    text: ( localize(3485, "Open Folder")),
    id: "workbench.action.files.openFolder"
};
const openFileOrFolder = {
    text: ( localize(3486, "Open File or Folder")),
    id: "workbench.action.files.openFileFolder"
};
const openRecent = {
    text: ( localize(3487, "Open Recent")),
    id: "workbench.action.openRecent"
};
const newUntitledFile = {
    text: ( localize(3488, "New Untitled Text File")),
    id: "workbench.action.files.newUntitledFile"
};
const findInFiles = {
    text: ( localize(3489, "Find in Files")),
    id: "workbench.action.findInFiles"
};
const toggleTerminal = {
    text: ( localize(3490, "Toggle Terminal")),
    id: "workbench.action.terminal.toggleTerminal",
    when: {
        web: ( ContextKeyExpr.equals("terminalProcessSupported", true))
    }
};
const startDebugging = {
    text: ( localize(3491, "Start Debugging")),
    id: "workbench.action.debug.start",
    when: {
        web: ( ContextKeyExpr.equals("terminalProcessSupported", true))
    }
};
const openSettings = {
    text: ( localize(3492, "Open Settings")),
    id: "workbench.action.openSettings"
};
const baseEntries = [openChat, showCommands];
const emptyWindowEntries = coalesce([
    ...baseEntries,
    openRecent,
    ...(isMacintosh && !isWeb ? [openFileOrFolder] : [openFile, openFolder]),
    isMacintosh && !isWeb ? newUntitledFile : undefined
]);
const workspaceEntries = [...baseEntries];
const otherEntries = [gotoFile, findInFiles, startDebugging, toggleTerminal, openSettings];
let EditorGroupWatermark = class EditorGroupWatermark extends Disposable {
    static {
        EditorGroupWatermark_1 = this;
    }
    static {
        this.CACHED_WHEN = "editorGroupWatermark.whenConditions";
    }
    static {
        this.SETTINGS_KEY = "workbench.tips.enabled";
    }
    static {
        this.MINIMUM_ENTRIES = 3;
    }
    constructor(
        container,
        keybindingService,
        contextService,
        contextKeyService,
        configurationService,
        storageService
    ) {
        super();
        this.keybindingService = keybindingService;
        this.contextService = contextService;
        this.contextKeyService = contextKeyService;
        this.configurationService = configurationService;
        this.storageService = storageService;
        this.transientDisposables = this._register(( new DisposableStore()));
        this.keybindingLabels = this._register(( new DisposableStore()));
        this.enabled = false;
        this.cachedWhen = this.storageService.getObject(
            EditorGroupWatermark_1.CACHED_WHEN,
            StorageScope.PROFILE,
            Object.create(null)
        );
        this.workbenchState = this.contextService.getWorkbenchState();
        const elements = h(
            ".editor-group-watermark",
            [h(".watermark-container", [h(".letterpress"), h(".shortcuts@shortcuts")])]
        );
        append(container, elements.root);
        this.shortcuts = elements.shortcuts;
        this.registerListeners();
        this.render();
    }
    registerListeners() {
        this._register(this.configurationService.onDidChangeConfiguration(e => {
            if (e.affectsConfiguration(EditorGroupWatermark_1.SETTINGS_KEY) && this.enabled !== this.configurationService.getValue(EditorGroupWatermark_1.SETTINGS_KEY)) {
                this.render();
            }
        }));
        this._register(this.contextService.onDidChangeWorkbenchState(workbenchState => {
            if (this.workbenchState !== workbenchState) {
                this.workbenchState = workbenchState;
                this.render();
            }
        }));
        this._register(this.storageService.onWillSaveState(e => {
            if (e.reason === WillSaveStateReason.SHUTDOWN) {
                const entries = [...emptyWindowEntries, ...workspaceEntries, ...otherEntries];
                for (const entry of entries) {
                    const when = isWeb ? entry.when?.web : entry.when?.native;
                    if (when) {
                        this.cachedWhen[entry.id] = this.contextKeyService.contextMatchesRules(when);
                    }
                }
                this.storageService.store(
                    EditorGroupWatermark_1.CACHED_WHEN,
                    JSON.stringify(this.cachedWhen),
                    StorageScope.PROFILE,
                    StorageTarget.MACHINE
                );
            }
        }));
    }
    render() {
        this.enabled = this.configurationService.getValue(EditorGroupWatermark_1.SETTINGS_KEY);
        clearNode(this.shortcuts);
        this.transientDisposables.clear();
        if (!this.enabled) {
            return;
        }
        const entries = this.filterEntries(
            this.workbenchState !== WorkbenchState.EMPTY ? workspaceEntries : emptyWindowEntries
        );
        if (entries.length < EditorGroupWatermark_1.MINIMUM_ENTRIES) {
            const additionalEntries = this.filterEntries(otherEntries);
            shuffle(additionalEntries);
            entries.push(
                ...additionalEntries.slice(0, EditorGroupWatermark_1.MINIMUM_ENTRIES - entries.length)
            );
        }
        const box = append(this.shortcuts, $(".watermark-box"));
        const update = () => {
            clearNode(box);
            this.keybindingLabels.clear();
            for (const entry of entries) {
                const keys = this.keybindingService.lookupKeybinding(entry.id);
                if (!keys) {
                    continue;
                }
                const dl = append(box, $("dl"));
                const dt = append(dl, $("dt"));
                dt.textContent = entry.text;
                const dd = append(dl, $("dd"));
                const label = this.keybindingLabels.add(( new KeybindingLabel(dd, OS, {
                    renderUnboundKeybindings: true,
                    ...defaultKeybindingLabelStyles
                })));
                label.set(keys);
            }
        };
        update();
        this.transientDisposables.add(this.keybindingService.onDidUpdateKeybindings(update));
    }
    filterEntries(entries) {
        const filteredEntries = entries.filter(entry => {
            if (this.cachedWhen[entry.id]) {
                return true;
            }
            const contextKey = isWeb ? entry.when?.web : entry.when?.native;
            return !contextKey  || this.contextKeyService.contextMatchesRules(contextKey);
        }).filter(entry => !!CommandsRegistry.getCommand(entry.id)).filter(entry => !!this.keybindingService.lookupKeybinding(entry.id));
        return filteredEntries;
    }
};
EditorGroupWatermark = EditorGroupWatermark_1 = ( __decorate([( __param(1, IKeybindingService)), ( __param(2, IWorkspaceContextService)), ( __param(3, IContextKeyService)), ( __param(4, IConfigurationService)), ( __param(5, IStorageService))], EditorGroupWatermark));

export { EditorGroupWatermark };
