
import { registerCss } from '../../../../../../../../css.js';
import { __decorate, __param } from '../../../../../../../../external/tslib/tslib.es6.js';
import * as openeditors from './media/openeditors.css';
import { localize2, localize } from '../../../../../nls.js';
import { RunOnceScheduler } from '../../../../../base/common/async.js';
import { ActionRunner } from '../../../../../base/common/actions.js';
import { $ as $$1, append, isMouseEvent, getLargestChildWidth } from '../../../../../base/browser/dom.js';
import { IContextMenuService } from '../../../../../platform/contextview/browser/contextView.service.js';
import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';
import { GroupsOrder, GroupOrientation } from '../../../../services/editor/common/editorGroupsService.js';
import { IEditorGroupsService } from '../../../../services/editor/common/editorGroupsService.service.js';
import { IConfigurationService } from '../../../../../platform/configuration/common/configuration.service.js';
import { IKeybindingService } from '../../../../../platform/keybinding/common/keybinding.service.js';
import { GroupModelChangeKind, preventEditorClose, EditorCloseMethod, EditorResourceAccessor, Verbosity, SideBySideEditor } from '../../../../common/editor.js';
import { SaveAllInGroupAction, CloseGroupAction } from '../fileActions.js';
import { OpenEditor, OpenEditorsFocusedContext, ExplorerFocusedContext } from '../../common/files.js';
import { CloseEditorAction, UnpinEditorAction, CloseAllEditorsAction } from '../../../../browser/parts/editor/editorActions.js';
import { ContextKeyExpr } from '../../../../../platform/contextkey/common/contextkey.js';
import { IContextKeyService } from '../../../../../platform/contextkey/common/contextkey.service.js';
import { IThemeService } from '../../../../../platform/theme/common/themeService.service.js';
import { asCssVariable } from '../../../../../platform/theme/common/colorUtils.js';
import { contrastBorder } from '../../../../../platform/theme/common/colors/baseColors.js';
import '../../../../../platform/theme/common/colors/chartsColors.js';
import '../../../../../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 { badgeBackground, badgeForeground } from '../../../../../platform/theme/common/colors/miscColors.js';
import '../../../../../platform/theme/common/colors/quickpickColors.js';
import '../../../../../platform/theme/common/colors/searchColors.js';
import { WorkbenchList } from '../../../../../platform/list/browser/listService.js';
import { ListDragOverEffectType, ListDragOverEffectPosition } from '../../../../../base/browser/ui/list/list.js';
import { ResourceLabels } from '../../../../browser/labels.js';
import { ActionBar } from '../../../../../base/browser/ui/actionbar/actionbar.js';
import { ITelemetryService } from '../../../../../platform/telemetry/common/telemetry.service.js';
import { DisposableMap, dispose } from '../../../../../base/common/lifecycle.js';
import { MenuId, registerAction2, Action2, MenuRegistry } from '../../../../../platform/actions/common/actions.js';
import { OpenEditorsGroupContext, OpenEditorsDirtyEditorContext, OpenEditorsReadonlyEditorContext, OpenEditorsSelectedFileOrUntitledContext, SAVE_ALL_LABEL, SAVE_ALL_COMMAND_ID, NEW_UNTITLED_FILE_COMMAND_ID } from '../fileConstants.js';
import { ResourceContextKey, MultipleEditorGroupsContext } from '../../../../common/contextkeys.js';
import { containsDragType, CodeDataTransfers } from '../../../../../platform/dnd/browser/dnd.js';
import { ResourcesDropHandler, fillEditorsDragData } from '../../../../browser/dnd.js';
import { ViewPane } from '../../../../browser/parts/views/viewPane.js';
import { DataTransfers } from '../../../../../base/browser/dnd.js';
import { memoize } from '../../../../../base/common/decorators.js';
import { NativeDragAndDropData, ElementsDragAndDropData, ListViewTargetSector } from '../../../../../base/browser/ui/list/listView.js';
import { IWorkingCopyService } from '../../../../services/workingCopy/common/workingCopyService.service.js';
import { WorkingCopyCapabilities } from '../../../../services/workingCopy/common/workingCopy.js';
import { IFilesConfigurationService } from '../../../../services/filesConfiguration/common/filesConfigurationService.service.js';
import { IViewDescriptorService } from '../../../../common/views.service.js';
import { IOpenerService } from '../../../../../platform/opener/common/opener.service.js';
import '../../../../../base/browser/ui/splitview/splitview.js';
import { compareFileNamesDefault } from '../../../../../base/common/comparers.js';
import { Codicon } from '../../../../../base/common/codicons.js';
import { KeyMod as KeyMod$1, KeyCode } from '../../../../../base/common/keyCodes.js';
import { KeybindingWeight } from '../../../../../platform/keybinding/common/keybindingsRegistry.js';
import { ICommandService } from '../../../../../platform/commands/common/commands.service.js';
import { Schemas } from '../../../../../base/common/network.js';
import { extUriIgnorePathCase } from '../../../../../base/common/resources.js';
import { mainWindow } from '../../../../../base/browser/window.js';
import { EditorGroupView } from '../../../../browser/parts/editor/editorGroupView.js';
import { IHoverService } from '../../../../../platform/hover/browser/hover.service.js';
import { IFileService } from '../../../../../platform/files/common/files.service.js';
import { Orientation } from '../../../../../base/browser/ui/sash/sash.js';

var OpenEditorsView_1;
registerCss(openeditors);
const $ = $$1;
let OpenEditorsView = class OpenEditorsView extends ViewPane {
    static {
        OpenEditorsView_1 = this;
    }
    static {
        this.DEFAULT_VISIBLE_OPEN_EDITORS = 9;
    }
    static {
        this.DEFAULT_MIN_VISIBLE_OPEN_EDITORS = 0;
    }
    static {
        this.ID = "workbench.explorer.openEditorsView";
    }
    static {
        this.NAME = ( localize2(9801, "Open Editors"));
    }
    constructor(
        options,
        instantiationService,
        viewDescriptorService,
        contextMenuService,
        editorGroupService,
        configurationService,
        keybindingService,
        contextKeyService,
        themeService,
        telemetryService,
        hoverService,
        workingCopyService,
        filesConfigurationService,
        openerService,
        fileService
    ) {
        super(
            options,
            keybindingService,
            contextMenuService,
            configurationService,
            contextKeyService,
            viewDescriptorService,
            instantiationService,
            openerService,
            themeService,
            hoverService
        );
        this.editorGroupService = editorGroupService;
        this.telemetryService = telemetryService;
        this.workingCopyService = workingCopyService;
        this.filesConfigurationService = filesConfigurationService;
        this.fileService = fileService;
        this.needsRefresh = false;
        this.elements = [];
        this.blockFocusActiveEditorTracking = false;
        this.structuralRefreshDelay = 0;
        this.sortOrder = configurationService.getValue("explorer.openEditors.sortOrder");
        this.registerUpdateEvents();
        this._register(
            this.configurationService.onDidChangeConfiguration(e => this.onConfigurationChange(e))
        );
        this._register(
            this.workingCopyService.onDidChangeDirty(workingCopy => this.updateDirtyIndicator(workingCopy))
        );
    }
    registerUpdateEvents() {
        const updateWholeList = () => {
            if (!this.isBodyVisible() || !this.list) {
                this.needsRefresh = true;
                return;
            }
            this.listRefreshScheduler?.schedule(this.structuralRefreshDelay);
        };
        const groupDisposables = this._register(( new DisposableMap()));
        const addGroupListener = group => {
            const groupModelChangeListener = group.onDidModelChange(e => {
                if (this.listRefreshScheduler?.isScheduled()) {
                    return;
                }
                if (!this.isBodyVisible() || !this.list) {
                    this.needsRefresh = true;
                    return;
                }
                const index = this.getIndex(group, e.editor);
                switch (e.kind) {
                case GroupModelChangeKind.EDITOR_ACTIVE:
                    this.focusActiveEditor();
                    break;
                case GroupModelChangeKind.GROUP_INDEX:
                case GroupModelChangeKind.GROUP_LABEL:
                    if (index >= 0) {
                        this.list.splice(index, 1, [group]);
                    }
                    break;
                case GroupModelChangeKind.EDITOR_DIRTY:
                case GroupModelChangeKind.EDITOR_STICKY:
                case GroupModelChangeKind.EDITOR_CAPABILITIES:
                case GroupModelChangeKind.EDITOR_PIN:
                case GroupModelChangeKind.EDITOR_LABEL:
                    this.list.splice(index, 1, [( new OpenEditor(e.editor, group))]);
                    this.focusActiveEditor();
                    break;
                case GroupModelChangeKind.EDITOR_OPEN:
                case GroupModelChangeKind.EDITOR_MOVE:
                case GroupModelChangeKind.EDITOR_CLOSE:
                    updateWholeList();
                    break;
                }
            });
            groupDisposables.set(group.id, groupModelChangeListener);
        };
        this.editorGroupService.groups.forEach(g => addGroupListener(g));
        this._register(this.editorGroupService.onDidAddGroup(group => {
            addGroupListener(group);
            updateWholeList();
        }));
        this._register(this.editorGroupService.onDidMoveGroup(() => updateWholeList()));
        this._register(
            this.editorGroupService.onDidChangeActiveGroup(() => this.focusActiveEditor())
        );
        this._register(this.editorGroupService.onDidRemoveGroup(group => {
            groupDisposables.deleteAndDispose(group.id);
            updateWholeList();
        }));
    }
    renderHeaderTitle(container) {
        super.renderHeaderTitle(container, this.title);
        const count = append(container, $(".open-editors-dirty-count-container"));
        this.dirtyCountElement = append(count, $(".dirty-count.monaco-count-badge.long"));
        this.dirtyCountElement.style.backgroundColor = asCssVariable(badgeBackground);
        this.dirtyCountElement.style.color = asCssVariable(badgeForeground);
        this.dirtyCountElement.style.border = `1px solid ${asCssVariable(contrastBorder)}`;
        this.updateDirtyIndicator();
    }
    renderBody(container) {
        super.renderBody(container);
        container.classList.add("open-editors");
        container.classList.add("show-file-icons");
        const delegate = ( new OpenEditorsDelegate());
        if (this.list) {
            this.list.dispose();
        }
        if (this.listLabels) {
            this.listLabels.clear();
        }
        this.dnd = ( new OpenEditorsDragAndDrop(this.sortOrder, this.instantiationService, this.editorGroupService));
        this.listLabels = this.instantiationService.createInstance(ResourceLabels, {
            onDidChangeVisibility: this.onDidChangeBodyVisibility
        });
        this.list = this.instantiationService.createInstance(WorkbenchList, "OpenEditors", container, delegate, [( new EditorGroupRenderer(this.keybindingService, this.instantiationService)), ( new OpenEditorRenderer(
            this.listLabels,
            this.instantiationService,
            this.keybindingService,
            this.configurationService
        ))], {
            identityProvider: {
                getId: element => element instanceof OpenEditor ? element.getId() : ( element.id.toString())
            },
            dnd: this.dnd,
            overrideStyles: this.getLocationBasedColors().listOverrideStyles,
            accessibilityProvider: ( new OpenEditorsAccessibilityProvider())
        });
        this._register(this.list);
        this._register(this.listLabels);
        let labelChangeListeners = [];
        this.listRefreshScheduler = this._register(( new RunOnceScheduler(() => {
            if (!this.list) {
                return;
            }
            labelChangeListeners = dispose(labelChangeListeners);
            const previousLength = this.list.length;
            const elements = this.getElements();
            this.list.splice(0, this.list.length, elements);
            this.focusActiveEditor();
            if (previousLength !== this.list.length) {
                this.updateSize();
            }
            this.needsRefresh = false;
            if (this.sortOrder === "alphabetical" || this.sortOrder === "fullPath") {
                elements.forEach(e => {
                    if (e instanceof OpenEditor) {
                        labelChangeListeners.push(e.editor.onDidChangeLabel(() => this.listRefreshScheduler?.schedule()));
                    }
                });
            }
        }, this.structuralRefreshDelay)));
        this.updateSize();
        this.handleContextKeys();
        this._register(this.list.onContextMenu(e => this.onListContextMenu(e)));
        this._register(this.list.onMouseMiddleClick(e => {
            if (e && e.element instanceof OpenEditor) {
                if (preventEditorClose(
                    e.element.group,
                    e.element.editor,
                    EditorCloseMethod.MOUSE,
                    this.editorGroupService.partOptions
                )) {
                    return;
                }
                e.element.group.closeEditor(e.element.editor, {
                    preserveFocus: true
                });
            }
        }));
        this._register(this.list.onDidOpen(e => {
            const element = e.element;
            if (!element) {
                return;
            } else if (element instanceof OpenEditor) {
                if (isMouseEvent(e.browserEvent) && e.browserEvent.button === 1) {
                    return;
                }
                this.withActiveEditorFocusTrackingDisabled(() => {
                    this.openEditor(element, {
                        preserveFocus: e.editorOptions.preserveFocus,
                        pinned: e.editorOptions.pinned,
                        sideBySide: e.sideBySide
                    });
                });
            } else {
                this.withActiveEditorFocusTrackingDisabled(() => {
                    this.editorGroupService.activateGroup(element);
                    if (!e.editorOptions.preserveFocus) {
                        element.focus();
                    }
                });
            }
        }));
        this.listRefreshScheduler.schedule(0);
        this._register(this.onDidChangeBodyVisibility(visible => {
            if (visible && this.needsRefresh) {
                this.listRefreshScheduler?.schedule(0);
            }
        }));
        const containerModel = this.viewDescriptorService.getViewContainerModel(this.viewDescriptorService.getViewContainerByViewId(this.id));
        this._register(containerModel.onDidChangeAllViewDescriptors(() => {
            this.updateSize();
        }));
    }
    handleContextKeys() {
        if (!this.list) {
            return;
        }
        OpenEditorsFocusedContext.bindTo(this.list.contextKeyService);
        ExplorerFocusedContext.bindTo(this.list.contextKeyService);
        const groupFocusedContext = OpenEditorsGroupContext.bindTo(this.contextKeyService);
        const dirtyEditorFocusedContext = OpenEditorsDirtyEditorContext.bindTo(this.contextKeyService);
        const readonlyEditorFocusedContext = OpenEditorsReadonlyEditorContext.bindTo(this.contextKeyService);
        const openEditorsSelectedFileOrUntitledContext = OpenEditorsSelectedFileOrUntitledContext.bindTo(this.contextKeyService);
        const resourceContext = this.instantiationService.createInstance(ResourceContextKey);
        this._register(resourceContext);
        this._register(this.list.onDidChangeFocus(e => {
            resourceContext.reset();
            groupFocusedContext.reset();
            dirtyEditorFocusedContext.reset();
            readonlyEditorFocusedContext.reset();
            const element = e.elements.length ? e.elements[0] : undefined;
            if (element instanceof OpenEditor) {
                const resource = element.getResource();
                dirtyEditorFocusedContext.set(element.editor.isDirty() && !element.editor.isSaving());
                readonlyEditorFocusedContext.set(!!element.editor.isReadonly());
                resourceContext.set(resource ?? null);
            } else if (element) {
                groupFocusedContext.set(true);
            }
        }));
        this._register(this.list.onDidChangeSelection(e => {
            const selectedAreFileOrUntitled = e.elements.every(e => {
                if (e instanceof OpenEditor) {
                    const resource = e.getResource();
                    return resource && (resource.scheme === Schemas.untitled || this.fileService.hasProvider(resource));
                }
                return false;
            });
            openEditorsSelectedFileOrUntitledContext.set(selectedAreFileOrUntitled);
        }));
    }
    focus() {
        super.focus();
        this.list?.domFocus();
    }
    layoutBody(height, width) {
        super.layoutBody(height, width);
        this.list?.layout(height, width);
    }
    get showGroups() {
        return this.editorGroupService.groups.length > 1;
    }
    getElements() {
        this.elements = [];
        this.editorGroupService.getGroups(GroupsOrder.GRID_APPEARANCE).forEach(g => {
            if (this.showGroups) {
                this.elements.push(g);
            }
            let editors = ( g.editors.map(ei => ( new OpenEditor(ei, g))));
            if (this.sortOrder === "alphabetical") {
                editors = editors.sort(
                    (first, second) => compareFileNamesDefault(first.editor.getName(), second.editor.getName())
                );
            } else if (this.sortOrder === "fullPath") {
                editors = editors.sort((first, second) => {
                    const firstResource = first.editor.resource;
                    const secondResource = second.editor.resource;
                    if (firstResource === undefined && secondResource === undefined) {
                        return compareFileNamesDefault(first.editor.getName(), second.editor.getName());
                    } else if (firstResource === undefined) {
                        return -1;
                    } else if (secondResource === undefined) {
                        return 1;
                    } else {
                        const firstScheme = firstResource.scheme;
                        const secondScheme = secondResource.scheme;
                        if (firstScheme !== Schemas.file && secondScheme !== Schemas.file) {
                            return extUriIgnorePathCase.compare(firstResource, secondResource);
                        } else if (firstScheme !== Schemas.file) {
                            return -1;
                        } else if (secondScheme !== Schemas.file) {
                            return 1;
                        } else {
                            return extUriIgnorePathCase.compare(firstResource, secondResource);
                        }
                    }
                });
            }
            this.elements.push(...editors);
        });
        return this.elements;
    }
    getIndex(group, editor) {
        if (!editor) {
            return this.elements.findIndex(e => !(e instanceof OpenEditor) && e.id === group.id);
        }
        return this.elements.findIndex(
            e => e instanceof OpenEditor && e.editor === editor && e.group.id === group.id
        );
    }
    openEditor(element, options) {
        if (element) {
            this.telemetryService.publicLog2("workbenchActionExecuted", {
                id: "workbench.files.openFile",
                from: "openEditors"
            });
            const preserveActivateGroup = options.sideBySide && options.preserveFocus;
            if (!preserveActivateGroup) {
                this.editorGroupService.activateGroup(element.group);
            }
            const targetGroup = options.sideBySide ? this.editorGroupService.sideGroup : element.group;
            targetGroup.openEditor(element.editor, options);
        }
    }
    onListContextMenu(e) {
        if (!e.element) {
            return;
        }
        const element = e.element;
        this.contextMenuService.showContextMenu({
            menuId: MenuId.OpenEditorsContext,
            menuActionOptions: {
                shouldForwardArgs: true,
                arg: element instanceof OpenEditor ? EditorResourceAccessor.getOriginalUri(element.editor) : {}
            },
            contextKeyService: this.list?.contextKeyService,
            getAnchor: () => e.anchor,
            getActionsContext: () => element instanceof OpenEditor ? {
                groupId: element.groupId,
                editorIndex: element.group.getIndexOfEditor(element.editor)
            } : {
                groupId: element.id
            }
        });
    }
    withActiveEditorFocusTrackingDisabled(fn) {
        this.blockFocusActiveEditorTracking = true;
        try {
            fn();
        } finally {
            this.blockFocusActiveEditorTracking = false;
        }
    }
    focusActiveEditor() {
        if (!this.list || this.blockFocusActiveEditorTracking) {
            return;
        }
        if (this.list.length && this.editorGroupService.activeGroup) {
            const index = this.getIndex(
                this.editorGroupService.activeGroup,
                this.editorGroupService.activeGroup.activeEditor
            );
            if (index >= 0) {
                try {
                    this.list.setFocus([index]);
                    this.list.setSelection([index]);
                    this.list.reveal(index);
                } catch (e) {}
                return;
            }
        }
        this.list.setFocus([]);
        this.list.setSelection([]);
    }
    onConfigurationChange(event) {
        if (event.affectsConfiguration("explorer.openEditors")) {
            this.updateSize();
        }
        if (event.affectsConfiguration("explorer.decorations") || event.affectsConfiguration("explorer.openEditors.sortOrder")) {
            this.sortOrder = this.configurationService.getValue("explorer.openEditors.sortOrder");
            if (this.dnd) {
                this.dnd.sortOrder = this.sortOrder;
            }
            this.listRefreshScheduler?.schedule();
        }
    }
    updateSize() {
        this.minimumBodySize = this.orientation === Orientation.VERTICAL ? this.getMinExpandedBodySize() : 170;
        this.maximumBodySize = this.orientation === Orientation.VERTICAL ? this.getMaxExpandedBodySize() : Number.POSITIVE_INFINITY;
    }
    updateDirtyIndicator(workingCopy) {
        if (workingCopy) {
            const gotDirty = workingCopy.isDirty();
            if (gotDirty && !(workingCopy.capabilities & WorkingCopyCapabilities.Untitled) && this.filesConfigurationService.hasShortAutoSaveDelay(workingCopy.resource)) {
                return;
            }
        }
        const dirty = this.workingCopyService.dirtyCount;
        if (dirty === 0) {
            this.dirtyCountElement.classList.add("hidden");
        } else {
            this.dirtyCountElement.textContent = ( localize(9802, "{0} unsaved", dirty));
            this.dirtyCountElement.classList.remove("hidden");
        }
    }
    get elementCount() {
        return ( this.editorGroupService.groups.map(g => g.count)).reduce(
            (first, second) => first + second,
            this.showGroups ? this.editorGroupService.groups.length : 0
        );
    }
    getMaxExpandedBodySize() {
        let minVisibleOpenEditors = this.configurationService.getValue("explorer.openEditors.minVisible");
        if (typeof minVisibleOpenEditors !== "number") {
            minVisibleOpenEditors = OpenEditorsView_1.DEFAULT_MIN_VISIBLE_OPEN_EDITORS;
        }
        const containerModel = this.viewDescriptorService.getViewContainerModel(this.viewDescriptorService.getViewContainerByViewId(this.id));
        if (containerModel.visibleViewDescriptors.length <= 1) {
            return Number.POSITIVE_INFINITY;
        }
        return (Math.max(this.elementCount, minVisibleOpenEditors)) * OpenEditorsDelegate.ITEM_HEIGHT;
    }
    getMinExpandedBodySize() {
        let visibleOpenEditors = this.configurationService.getValue("explorer.openEditors.visible");
        if (typeof visibleOpenEditors !== "number") {
            visibleOpenEditors = OpenEditorsView_1.DEFAULT_VISIBLE_OPEN_EDITORS;
        }
        return this.computeMinExpandedBodySize(visibleOpenEditors);
    }
    computeMinExpandedBodySize(visibleOpenEditors = OpenEditorsView_1.DEFAULT_VISIBLE_OPEN_EDITORS) {
        const itemsToShow = Math.min(Math.max(visibleOpenEditors, 1), this.elementCount);
        return itemsToShow * OpenEditorsDelegate.ITEM_HEIGHT;
    }
    setStructuralRefreshDelay(delay) {
        this.structuralRefreshDelay = delay;
    }
    getOptimalWidth() {
        if (!this.list) {
            return super.getOptimalWidth();
        }
        const parentNode = this.list.getHTMLElement();
        const childNodes = [].slice.call(parentNode.querySelectorAll(".open-editor > a"));
        return getLargestChildWidth(parentNode, childNodes);
    }
};
OpenEditorsView = OpenEditorsView_1 = ( __decorate([( __param(1, IInstantiationService)), ( __param(2, IViewDescriptorService)), ( __param(3, IContextMenuService)), ( __param(4, IEditorGroupsService)), ( __param(5, IConfigurationService)), ( __param(6, IKeybindingService)), ( __param(7, IContextKeyService)), ( __param(8, IThemeService)), ( __param(9, ITelemetryService)), ( __param(10, IHoverService)), ( __param(11, IWorkingCopyService)), ( __param(12, IFilesConfigurationService)), ( __param(13, IOpenerService)), ( __param(14, IFileService))], OpenEditorsView));
class OpenEditorActionRunner extends ActionRunner {
    async run(action) {
        if (!this.editor) {
            return;
        }
        return super.run(action, {
            groupId: this.editor.groupId,
            editorIndex: this.editor.group.getIndexOfEditor(this.editor.editor)
        });
    }
}
class OpenEditorsDelegate {
    static {
        this.ITEM_HEIGHT = 22;
    }
    getHeight(_element) {
        return OpenEditorsDelegate.ITEM_HEIGHT;
    }
    getTemplateId(element) {
        if (element instanceof OpenEditor) {
            return OpenEditorRenderer.ID;
        }
        return EditorGroupRenderer.ID;
    }
}
class EditorGroupRenderer {
    static {
        this.ID = "editorgroup";
    }
    constructor(keybindingService, instantiationService) {
        this.keybindingService = keybindingService;
        this.instantiationService = instantiationService;
    }
    get templateId() {
        return EditorGroupRenderer.ID;
    }
    renderTemplate(container) {
        const editorGroupTemplate = Object.create(null);
        editorGroupTemplate.root = append(container, $(".editor-group"));
        editorGroupTemplate.name = append(editorGroupTemplate.root, $("span.name"));
        editorGroupTemplate.actionBar = ( new ActionBar(container));
        const saveAllInGroupAction = this.instantiationService.createInstance(SaveAllInGroupAction, SaveAllInGroupAction.ID, SaveAllInGroupAction.LABEL);
        const saveAllInGroupKey = this.keybindingService.lookupKeybinding(saveAllInGroupAction.id);
        editorGroupTemplate.actionBar.push(saveAllInGroupAction, {
            icon: true,
            label: false,
            keybinding: saveAllInGroupKey ? saveAllInGroupKey.getLabel() : undefined
        });
        const closeGroupAction = this.instantiationService.createInstance(CloseGroupAction, CloseGroupAction.ID, CloseGroupAction.LABEL);
        const closeGroupActionKey = this.keybindingService.lookupKeybinding(closeGroupAction.id);
        editorGroupTemplate.actionBar.push(closeGroupAction, {
            icon: true,
            label: false,
            keybinding: closeGroupActionKey ? closeGroupActionKey.getLabel() : undefined
        });
        return editorGroupTemplate;
    }
    renderElement(editorGroup, _index, templateData) {
        templateData.editorGroup = editorGroup;
        templateData.name.textContent = editorGroup.label;
        templateData.actionBar.context = {
            groupId: editorGroup.id
        };
    }
    disposeTemplate(templateData) {
        templateData.actionBar.dispose();
    }
}
class OpenEditorRenderer {
    static {
        this.ID = "openeditor";
    }
    constructor(labels, instantiationService, keybindingService, configurationService) {
        this.labels = labels;
        this.instantiationService = instantiationService;
        this.keybindingService = keybindingService;
        this.configurationService = configurationService;
        this.closeEditorAction = this.instantiationService.createInstance(CloseEditorAction, CloseEditorAction.ID, CloseEditorAction.LABEL);
        this.unpinEditorAction = this.instantiationService.createInstance(UnpinEditorAction, UnpinEditorAction.ID, UnpinEditorAction.LABEL);
    }
    get templateId() {
        return OpenEditorRenderer.ID;
    }
    renderTemplate(container) {
        const editorTemplate = Object.create(null);
        editorTemplate.container = container;
        editorTemplate.actionRunner = ( new OpenEditorActionRunner());
        editorTemplate.actionBar = ( new ActionBar(container, {
            actionRunner: editorTemplate.actionRunner
        }));
        editorTemplate.root = this.labels.create(container);
        return editorTemplate;
    }
    renderElement(openedEditor, _index, templateData) {
        const editor = openedEditor.editor;
        templateData.actionRunner.editor = openedEditor;
        templateData.container.classList.toggle("dirty", editor.isDirty() && !editor.isSaving());
        templateData.container.classList.toggle("sticky", openedEditor.isSticky());
        templateData.root.setResource({
            resource: EditorResourceAccessor.getOriginalUri(editor, {
                supportSideBySide: SideBySideEditor.BOTH
            }),
            name: editor.getName(),
            description: editor.getDescription(Verbosity.MEDIUM)
        }, {
            italic: openedEditor.isPreview(),
            extraClasses: ["open-editor"].concat(openedEditor.editor.getLabelExtraClasses()),
            fileDecorations: this.configurationService.getValue().explorer.decorations,
            title: editor.getTitle(Verbosity.LONG),
            icon: editor.getIcon()
        });
        const editorAction = openedEditor.isSticky() ? this.unpinEditorAction : this.closeEditorAction;
        if (!templateData.actionBar.hasAction(editorAction)) {
            if (!templateData.actionBar.isEmpty()) {
                templateData.actionBar.clear();
            }
            templateData.actionBar.push(editorAction, {
                icon: true,
                label: false,
                keybinding: this.keybindingService.lookupKeybinding(editorAction.id)?.getLabel()
            });
        }
    }
    disposeTemplate(templateData) {
        templateData.actionBar.dispose();
        templateData.root.dispose();
        templateData.actionRunner.dispose();
    }
}
class OpenEditorsDragAndDrop {
    set sortOrder(value) {
        this._sortOrder = value;
    }
    constructor(sortOrder, instantiationService, editorGroupService) {
        this.instantiationService = instantiationService;
        this.editorGroupService = editorGroupService;
        this._sortOrder = sortOrder;
    }
    get dropHandler() {
        return this.instantiationService.createInstance(ResourcesDropHandler, {
            allowWorkspaceOpen: false
        });
    }
    getDragURI(element) {
        if (element instanceof OpenEditor) {
            const resource = element.getResource();
            if (resource) {
                return ( resource.toString());
            }
        }
        return null;
    }
    getDragLabel(elements) {
        if (elements.length > 1) {
            return String(elements.length);
        }
        const element = elements[0];
        return element instanceof OpenEditor ? element.editor.getName() : element.label;
    }
    onDragStart(data, originalEvent) {
        const items = data.elements;
        const editors = [];
        if (items) {
            for (const item of items) {
                if (item instanceof OpenEditor) {
                    editors.push(item);
                }
            }
        }
        if (editors.length) {
            this.instantiationService.invokeFunction(fillEditorsDragData, editors, originalEvent);
        }
    }
    onDragOver(data, _targetElement, _targetIndex, targetSector, originalEvent) {
        if (data instanceof NativeDragAndDropData) {
            if (!containsDragType(originalEvent, DataTransfers.FILES, CodeDataTransfers.FILES)) {
                return false;
            }
        }
        if (this._sortOrder !== "editorOrder") {
            if (data instanceof ElementsDragAndDropData) {
                return false;
            } else {
                return {
                    accept: true,
                    effect: {
                        type: ListDragOverEffectType.Move
                    },
                    feedback: [-1]
                };
            }
        }
        let dropEffectPosition = undefined;
        switch (targetSector) {
        case ListViewTargetSector.TOP:
        case ListViewTargetSector.CENTER_TOP:
            dropEffectPosition = (_targetIndex === 0 && _targetElement instanceof EditorGroupView) ? ListDragOverEffectPosition.After : ListDragOverEffectPosition.Before;
            break;
        case ListViewTargetSector.CENTER_BOTTOM:
        case ListViewTargetSector.BOTTOM:
            dropEffectPosition = ListDragOverEffectPosition.After;
            break;
        }
        return {
            accept: true,
            effect: {
                type: ListDragOverEffectType.Move,
                position: dropEffectPosition
            },
            feedback: [_targetIndex]
        };
    }
    drop(data, targetElement, _targetIndex, targetSector, originalEvent) {
        let group = targetElement instanceof OpenEditor ? targetElement.group : targetElement || this.editorGroupService.groups[this.editorGroupService.count - 1];
        let targetEditorIndex = targetElement instanceof OpenEditor ? targetElement.group.getIndexOfEditor(targetElement.editor) : 0;
        switch (targetSector) {
        case ListViewTargetSector.TOP:
        case ListViewTargetSector.CENTER_TOP:
            if (targetElement instanceof EditorGroupView && group.index !== 0) {
                group = this.editorGroupService.groups[group.index - 1];
                targetEditorIndex = group.count;
            }
            break;
        case ListViewTargetSector.BOTTOM:
        case ListViewTargetSector.CENTER_BOTTOM:
            if (targetElement instanceof OpenEditor) {
                targetEditorIndex++;
            }
            break;
        }
        if (data instanceof ElementsDragAndDropData) {
            for (const oe of data.elements) {
                const sourceEditorIndex = oe.group.getIndexOfEditor(oe.editor);
                if (oe.group === group && sourceEditorIndex < targetEditorIndex) {
                    targetEditorIndex--;
                }
                oe.group.moveEditor(oe.editor, group, {
                    index: targetEditorIndex,
                    preserveFocus: true
                });
                targetEditorIndex++;
            }
            this.editorGroupService.activateGroup(group);
        } else {
            this.dropHandler.handleDrop(originalEvent, mainWindow, () => group, () => group.focus(), {
                index: targetEditorIndex
            });
        }
    }
    dispose() {}
}
OpenEditorsDragAndDrop.__decorator = ( __decorate([memoize], OpenEditorsDragAndDrop.prototype, "dropHandler", null));
class OpenEditorsAccessibilityProvider {
    getWidgetAriaLabel() {
        return localize(9801, "Open Editors");
    }
    getAriaLabel(element) {
        if (element instanceof OpenEditor) {
            return `${element.editor.getName()}, ${element.editor.getDescription()}`;
        }
        return element.ariaLabel;
    }
}
const toggleEditorGroupLayoutId = "workbench.action.toggleEditorGroupLayout";
registerAction2(class extends Action2 {
    constructor() {
        super({
            id: "workbench.action.toggleEditorGroupLayout",
            title: ( localize2(9803, "Toggle Vertical/Horizontal Editor Layout")),
            f1: true,
            keybinding: {
                primary: KeyMod$1.Shift | KeyMod$1.Alt | KeyCode.Digit0,
                mac: {
                    primary: KeyMod$1.CtrlCmd | KeyMod$1.Alt | KeyCode.Digit0
                },
                weight: KeybindingWeight.WorkbenchContrib
            },
            icon: Codicon.editorLayout,
            menu: {
                id: MenuId.ViewTitle,
                group: "navigation",
                when: ( ContextKeyExpr.and(( ContextKeyExpr.equals("view", OpenEditorsView.ID)), MultipleEditorGroupsContext)),
                order: 10
            }
        });
    }
    async run(accessor) {
        const editorGroupService = accessor.get(IEditorGroupsService);
        const newOrientation = (editorGroupService.orientation === GroupOrientation.VERTICAL) ? GroupOrientation.HORIZONTAL : GroupOrientation.VERTICAL;
        editorGroupService.setGroupOrientation(newOrientation);
        editorGroupService.activeGroup.focus();
    }
});
MenuRegistry.appendMenuItem(MenuId.MenubarLayoutMenu, {
    group: "5_flip",
    command: {
        id: toggleEditorGroupLayoutId,
        title: {
            ...( localize2(9804, "Flip Layout")),
            mnemonicTitle: ( localize(9805, "Flip &&Layout"))
        }
    },
    order: 1
});
registerAction2(class extends Action2 {
    constructor() {
        super({
            id: "workbench.action.files.saveAll",
            title: SAVE_ALL_LABEL,
            f1: true,
            icon: Codicon.saveAll,
            menu: {
                id: MenuId.ViewTitle,
                group: "navigation",
                when: ( ContextKeyExpr.equals("view", OpenEditorsView.ID)),
                order: 20
            }
        });
    }
    async run(accessor) {
        const commandService = accessor.get(ICommandService);
        await commandService.executeCommand(SAVE_ALL_COMMAND_ID);
    }
});
registerAction2(class extends Action2 {
    constructor() {
        super({
            id: "openEditors.closeAll",
            title: CloseAllEditorsAction.LABEL,
            f1: false,
            icon: Codicon.closeAll,
            menu: {
                id: MenuId.ViewTitle,
                group: "navigation",
                when: ( ContextKeyExpr.equals("view", OpenEditorsView.ID)),
                order: 30
            }
        });
    }
    async run(accessor) {
        const instantiationService = accessor.get(IInstantiationService);
        const closeAll = ( new CloseAllEditorsAction());
        await instantiationService.invokeFunction(accessor => closeAll.run(accessor));
    }
});
registerAction2(class extends Action2 {
    constructor() {
        super({
            id: "openEditors.newUntitledFile",
            title: ( localize2(9806, "New Untitled Text File")),
            f1: false,
            icon: Codicon.newFile,
            menu: {
                id: MenuId.ViewTitle,
                group: "navigation",
                when: ( ContextKeyExpr.equals("view", OpenEditorsView.ID)),
                order: 5
            }
        });
    }
    async run(accessor) {
        const commandService = accessor.get(ICommandService);
        await commandService.executeCommand(NEW_UNTITLED_FILE_COMMAND_ID);
    }
});

export { OpenEditorsView };
