
import { __decorate, __param } from '../../../../../external/tslib/tslib.es6.js';
import { Disposable } from '../../base/common/lifecycle.js';
import { getIEditor } from '../../editor/browser/editorBrowser.js';
import { localize } from '../../nls.js';
import { RawContextKey, ContextKeyExpr } from '../../platform/contextkey/common/contextkey.js';
import { IKeybindingService } from '../../platform/keybinding/common/keybinding.service.js';
import { IQuickInputService } from '../../platform/quickinput/common/quickInput.service.js';
import { IEditorGroupsService } from '../services/editor/common/editorGroupsService.service.js';
import { IEditorService } from '../services/editor/common/editorService.service.js';

const inQuickPickContextKeyValue = "inQuickOpen";
const InQuickPickContextKey = ( new RawContextKey(inQuickPickContextKeyValue, false, ( localize(3918, "Whether keyboard focus is inside the quick open control"))));
const inQuickPickContext = ( ContextKeyExpr.has(inQuickPickContextKeyValue));
const defaultQuickAccessContextKeyValue = "inFilesPicker";
const defaultQuickAccessContext = ( ContextKeyExpr.and(inQuickPickContext, ( ContextKeyExpr.has(defaultQuickAccessContextKeyValue))));
function getQuickNavigateHandler(id, next) {
    return accessor => {
        const keybindingService = accessor.get(IKeybindingService);
        const quickInputService = accessor.get(IQuickInputService);
        const keys = keybindingService.lookupKeybindings(id);
        const quickNavigate = {
            keybindings: keys
        };
        quickInputService.navigate(!!next, quickNavigate);
    };
}
let PickerEditorState = class PickerEditorState extends Disposable {
    constructor(editorService, editorGroupsService) {
        super();
        this.editorService = editorService;
        this.editorGroupsService = editorGroupsService;
        this.editorViewState = undefined;
        this.openedTransientEditors = ( new Set());
    }
    set() {
        if (this.editorViewState) {
            return;
        }
        const activeEditorPane = this.editorService.activeEditorPane;
        if (activeEditorPane) {
            this.editorViewState = {
                group: activeEditorPane.group,
                editor: activeEditorPane.input,
                state: getIEditor(activeEditorPane.getControl())?.saveViewState() ?? undefined
            };
        }
    }
    async openTransientEditor(editor, group) {
        editor.options = {
            ...editor.options,
            transient: true
        };
        const editorPane = await this.editorService.openEditor(editor, group);
        if (editorPane?.input && editorPane.input !== this.editorViewState?.editor && editorPane.group.isTransient(editorPane.input)) {
            this.openedTransientEditors.add(editorPane.input);
        }
        return editorPane;
    }
    async restore() {
        if (this.editorViewState) {
            for (const editor of this.openedTransientEditors) {
                if (editor.isDirty()) {
                    continue;
                }
                for (const group of this.editorGroupsService.groups) {
                    if (group.isTransient(editor)) {
                        await group.closeEditor(editor, {
                            preserveFocus: true
                        });
                    }
                }
            }
            await this.editorViewState.group.openEditor(this.editorViewState.editor, {
                viewState: this.editorViewState.state,
                preserveFocus: true
            });
            this.reset();
        }
    }
    reset() {
        this.editorViewState = undefined;
        this.openedTransientEditors.clear();
    }
    dispose() {
        super.dispose();
        this.reset();
    }
};
PickerEditorState = ( __decorate([( __param(0, IEditorService)), ( __param(1, IEditorGroupsService))], PickerEditorState));

export { InQuickPickContextKey, PickerEditorState, defaultQuickAccessContext, defaultQuickAccessContextKeyValue, getQuickNavigateHandler, inQuickPickContext, inQuickPickContextKeyValue };
