import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import './media/explorerviewlet.css.js';
import { localize2WithPath, localizeWithPath } from '../../../../nls.js';
import { mark } from '../../../../base/common/performance.js';
import { VIEW_ID, VIEWLET_ID, ExplorerViewletVisibleContext } from '../common/files.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { ExplorerView } from './views/explorerView.js';
import { EmptyView } from './views/emptyView.js';
import { OpenEditorsView } from './views/openEditorsView.js';
import { IStorageService } from '../../../../platform/storage/common/storage.service.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { IExtensionService } from '../../../services/extensions/common/extensions.service.js';
import { WorkbenchState } from '../../../../platform/workspace/common/workspace.js';
import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.service.js';
import { ITelemetryService } from '../../../../platform/telemetry/common/telemetry.service.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 { ViewContainerLocation, Extensions, ViewContentGroups } from '../../../common/views.js';
import { IViewDescriptorService } from '../../../common/views.service.js';
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.service.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { IWorkbenchLayoutService } from '../../../services/layout/browser/layoutService.service.js';
import { ViewPaneContainer } from '../../../browser/parts/views/viewPaneContainer.js';
import { KeyChord, KeyMod as KeyMod$1, KeyCode } from '../../../../base/common/keyCodes.js';
import { Registry } from '../../../../platform/registry/common/platform.js';
import { ProgressLocation } from '../../../../platform/progress/common/progress.js';
import { IProgressService } from '../../../../platform/progress/common/progress.service.js';
import { SyncDescriptor } from '../../../../platform/instantiation/common/descriptors.js';
import { WorkbenchStateContext, OpenFolderWorkspaceSupportContext, RemoteNameContext } from '../../../common/contextkeys.js';
import { IsWebContext } from '../../../../platform/contextkey/common/contextkeys.js';
import { AddRootFolderAction, OpenFileFolderAction, OpenFolderAction, OpenFolderViaWorkspaceAction } from '../../../browser/actions/workspaceActions.js';
import { OpenRecentAction } from '../../../browser/actions/windowActions.js';
import { isMacintosh, isWeb } from '../../../../base/common/platform.js';
import { Codicon } from '../../../../base/common/codicons.js';
import { registerIcon } from '../../../../platform/theme/common/iconRegistry.js';
import { isMouseEvent } from '../../../../base/browser/dom.js';
const _moduleId = "vs/workbench/contrib/files/browser/explorerViewlet";
const explorerViewIcon = registerIcon('explorer-view-icon', Codicon.files, ( localizeWithPath(_moduleId, 0, 'View icon of the explorer view.')));
const openEditorsViewIcon = registerIcon('open-editors-view-icon', Codicon.book, ( localizeWithPath(_moduleId, 1, 'View icon of the open editors view.')));
let ExplorerViewletViewsContribution = class ExplorerViewletViewsContribution extends Disposable {
    static { this.ID = 'workbench.contrib.explorerViewletViews'; }
    constructor(workspaceContextService, progressService) {
        super();
        this.workspaceContextService = workspaceContextService;
        progressService.withProgress({ location: ProgressLocation.Explorer }, () => workspaceContextService.getCompleteWorkspace()).finally(() => {
            this.registerViews();
            this._register(workspaceContextService.onDidChangeWorkbenchState(() => this.registerViews()));
            this._register(workspaceContextService.onDidChangeWorkspaceFolders(() => this.registerViews()));
        });
    }
    registerViews() {
        mark('code/willRegisterExplorerViews');
        const viewDescriptors = viewsRegistry.getViews(VIEW_CONTAINER);
        const viewDescriptorsToRegister = [];
        const viewDescriptorsToDeregister = [];
        const openEditorsViewDescriptor = this.createOpenEditorsViewDescriptor();
        if (!( (viewDescriptors.some(v => v.id === openEditorsViewDescriptor.id)))) {
            viewDescriptorsToRegister.push(openEditorsViewDescriptor);
        }
        const explorerViewDescriptor = this.createExplorerViewDescriptor();
        const registeredExplorerViewDescriptor = viewDescriptors.find(v => v.id === explorerViewDescriptor.id);
        const emptyViewDescriptor = this.createEmptyViewDescriptor();
        const registeredEmptyViewDescriptor = viewDescriptors.find(v => v.id === emptyViewDescriptor.id);
        if (this.workspaceContextService.getWorkbenchState() === WorkbenchState.EMPTY || this.workspaceContextService.getWorkspace().folders.length === 0) {
            if (registeredExplorerViewDescriptor) {
                viewDescriptorsToDeregister.push(registeredExplorerViewDescriptor);
            }
            if (!registeredEmptyViewDescriptor) {
                viewDescriptorsToRegister.push(emptyViewDescriptor);
            }
        }
        else {
            if (registeredEmptyViewDescriptor) {
                viewDescriptorsToDeregister.push(registeredEmptyViewDescriptor);
            }
            if (!registeredExplorerViewDescriptor) {
                viewDescriptorsToRegister.push(explorerViewDescriptor);
            }
        }
        if (viewDescriptorsToDeregister.length) {
            viewsRegistry.deregisterViews(viewDescriptorsToDeregister, VIEW_CONTAINER);
        }
        if (viewDescriptorsToRegister.length) {
            viewsRegistry.registerViews(viewDescriptorsToRegister, VIEW_CONTAINER);
        }
        mark('code/didRegisterExplorerViews');
    }
    createOpenEditorsViewDescriptor() {
        return {
            id: OpenEditorsView.ID,
            name: OpenEditorsView.NAME,
            ctorDescriptor: ( (new SyncDescriptor(OpenEditorsView))),
            containerIcon: openEditorsViewIcon,
            order: 0,
            canToggleVisibility: true,
            canMoveView: true,
            collapsed: false,
            hideByDefault: true,
            focusCommand: {
                id: 'workbench.files.action.focusOpenEditorsView',
                keybindings: { primary: KeyChord(KeyMod$1.CtrlCmd | KeyCode.KeyK, KeyCode.KeyE) }
            }
        };
    }
    createEmptyViewDescriptor() {
        return {
            id: EmptyView.ID,
            name: EmptyView.NAME,
            containerIcon: explorerViewIcon,
            ctorDescriptor: ( (new SyncDescriptor(EmptyView))),
            order: 1,
            canToggleVisibility: true,
            focusCommand: {
                id: 'workbench.explorer.fileView.focus'
            }
        };
    }
    createExplorerViewDescriptor() {
        return {
            id: VIEW_ID,
            name: ( localize2WithPath(_moduleId, 2, "Folders")),
            containerIcon: explorerViewIcon,
            ctorDescriptor: ( (new SyncDescriptor(ExplorerView))),
            order: 1,
            canMoveView: true,
            canToggleVisibility: false,
            focusCommand: {
                id: 'workbench.explorer.fileView.focus'
            }
        };
    }
};
ExplorerViewletViewsContribution = ( (__decorate([
    ( (__param(0, IWorkspaceContextService))),
    ( (__param(1, IProgressService)))
], ExplorerViewletViewsContribution)));
let ExplorerViewPaneContainer = class ExplorerViewPaneContainer extends ViewPaneContainer {
    constructor(layoutService, telemetryService, contextService, storageService, configurationService, instantiationService, contextKeyService, themeService, contextMenuService, extensionService, viewDescriptorService) {
        super(VIEWLET_ID, { mergeViewWithContainerWhenSingleView: true }, instantiationService, configurationService, layoutService, contextMenuService, telemetryService, extensionService, themeService, storageService, contextService, viewDescriptorService);
        this.viewletVisibleContextKey = ExplorerViewletVisibleContext.bindTo(contextKeyService);
        this._register(this.contextService.onDidChangeWorkspaceName(e => this.updateTitleArea()));
    }
    create(parent) {
        super.create(parent);
        parent.classList.add('explorer-viewlet');
    }
    createView(viewDescriptor, options) {
        if (viewDescriptor.id === VIEW_ID) {
            return this.instantiationService.createInstance(ExplorerView, {
                ...options, delegate: {
                    willOpenElement: e => {
                        if (!isMouseEvent(e)) {
                            return;
                        }
                        const openEditorsView = this.getOpenEditorsView();
                        if (openEditorsView) {
                            let delay = 0;
                            const config = this.configurationService.getValue();
                            if (!!config.workbench?.editor?.enablePreview) {
                                delay = 250;
                            }
                            openEditorsView.setStructuralRefreshDelay(delay);
                        }
                    },
                    didOpenElement: e => {
                        if (!isMouseEvent(e)) {
                            return;
                        }
                        const openEditorsView = this.getOpenEditorsView();
                        openEditorsView?.setStructuralRefreshDelay(0);
                    }
                }
            });
        }
        return super.createView(viewDescriptor, options);
    }
    getExplorerView() {
        return this.getView(VIEW_ID);
    }
    getOpenEditorsView() {
        return this.getView(OpenEditorsView.ID);
    }
    setVisible(visible) {
        this.viewletVisibleContextKey.set(visible);
        super.setVisible(visible);
    }
    focus() {
        const explorerView = this.getView(VIEW_ID);
        if (explorerView && this.panes.every(p => !p.isExpanded())) {
            explorerView.setExpanded(true);
        }
        if (explorerView?.isExpanded()) {
            explorerView.focus();
        }
        else {
            super.focus();
        }
    }
};
ExplorerViewPaneContainer = ( (__decorate([
    ( (__param(0, IWorkbenchLayoutService))),
    ( (__param(1, ITelemetryService))),
    ( (__param(2, IWorkspaceContextService))),
    ( (__param(3, IStorageService))),
    ( (__param(4, IConfigurationService))),
    ( (__param(5, IInstantiationService))),
    ( (__param(6, IContextKeyService))),
    ( (__param(7, IThemeService))),
    ( (__param(8, IContextMenuService))),
    ( (__param(9, IExtensionService))),
    ( (__param(10, IViewDescriptorService)))
], ExplorerViewPaneContainer)));
const viewContainerRegistry = ( (Registry.as(Extensions.ViewContainersRegistry)));
const VIEW_CONTAINER = viewContainerRegistry.registerViewContainer({
    id: VIEWLET_ID,
    title: ( localize2WithPath(_moduleId, 3, "Explorer")),
    ctorDescriptor: ( (new SyncDescriptor(ExplorerViewPaneContainer))),
    storageId: 'workbench.explorer.views.state',
    icon: explorerViewIcon,
    alwaysUseContainerInfo: true,
    hideIfEmpty: true,
    order: 0,
    openCommandActionDescriptor: {
        id: VIEWLET_ID,
        title: ( localize2WithPath(_moduleId, 3, "Explorer")),
        mnemonicTitle: ( localizeWithPath(_moduleId, 4, "&&Explorer")),
        keybindings: { primary: KeyMod$1.CtrlCmd | KeyMod$1.Shift | KeyCode.KeyE },
        order: 0
    },
}, ViewContainerLocation.Sidebar, { isDefault: true });
const openFolder = ( localizeWithPath(_moduleId, 5, "Open Folder"));
const addAFolder = ( localizeWithPath(_moduleId, 6, "add a folder"));
const openRecent = ( localizeWithPath(_moduleId, 7, "Open Recent"));
const addRootFolderButton = `[${openFolder}](command:${AddRootFolderAction.ID})`;
const addAFolderButton = `[${addAFolder}](command:${AddRootFolderAction.ID})`;
const openFolderButton = `[${openFolder}](command:${(isMacintosh && !isWeb) ? OpenFileFolderAction.ID : OpenFolderAction.ID})`;
const openFolderViaWorkspaceButton = `[${openFolder}](command:${OpenFolderViaWorkspaceAction.ID})`;
const openRecentButton = `[${openRecent}](command:${OpenRecentAction.ID})`;
const viewsRegistry = ( (Registry.as(Extensions.ViewsRegistry)));
viewsRegistry.registerViewWelcomeContent(EmptyView.ID, {
    content: ( localizeWithPath(
        _moduleId,
        8,
        "You have not yet added a folder to the workspace.\n{0}",
        addRootFolderButton
    )),
    when: ( (ContextKeyExpr.and(
    (WorkbenchStateContext.isEqualTo('workspace')),
    OpenFolderWorkspaceSupportContext))),
    group: ViewContentGroups.Open,
    order: 1
});
viewsRegistry.registerViewWelcomeContent(EmptyView.ID, {
    content: ( localizeWithPath(
        _moduleId,
        9,
        "You have not yet opened a folder.\n{0}\n{1}",
        openFolderViaWorkspaceButton,
        openRecentButton
    )),
    when: ( (ContextKeyExpr.and(
    (WorkbenchStateContext.isEqualTo('workspace')),
    (OpenFolderWorkspaceSupportContext.toNegated())))),
    group: ViewContentGroups.Open,
    order: 1
});
viewsRegistry.registerViewWelcomeContent(EmptyView.ID, {
    content: ( localizeWithPath(_moduleId, 10, "Connected to remote.\n{0}", openFolderButton)),
    when: ( (ContextKeyExpr.and(
    (WorkbenchStateContext.notEqualsTo('workspace')),
    (RemoteNameContext.notEqualsTo('')),
    (IsWebContext.toNegated())))),
    group: ViewContentGroups.Open,
    order: 1
});
viewsRegistry.registerViewWelcomeContent(EmptyView.ID, {
    content: ( localizeWithPath(
        _moduleId,
        11,
        "You have not yet opened a folder.\n{0}\nOpening a folder will close all currently open editors. To keep them open, {1} instead.",
        openFolderButton,
        addAFolderButton
    )),
    when: ( (ContextKeyExpr.and(
    (ContextKeyExpr.has('editorIsOpen')),  (ContextKeyExpr.or(
    (ContextKeyExpr.and(
         (WorkbenchStateContext.notEqualsTo('workspace')),
         (RemoteNameContext.isEqualTo(''))
    )),
    (ContextKeyExpr.and(
         (WorkbenchStateContext.notEqualsTo('workspace')),
        IsWebContext
    ))))))),
    group: ViewContentGroups.Open,
    order: 1
});
viewsRegistry.registerViewWelcomeContent(EmptyView.ID, {
    content: ( localizeWithPath(_moduleId, 12, "You have not yet opened a folder.\n{0}", openFolderButton)),
    when: ( (ContextKeyExpr.and(
    ( (ContextKeyExpr.has('editorIsOpen')))?.negate(),  (ContextKeyExpr.or(
    (ContextKeyExpr.and(
         (WorkbenchStateContext.notEqualsTo('workspace')),
         (RemoteNameContext.isEqualTo(''))
    )),
    (ContextKeyExpr.and(
         (WorkbenchStateContext.notEqualsTo('workspace')),
        IsWebContext
    ))))))),
    group: ViewContentGroups.Open,
    order: 1
});
export { ExplorerViewPaneContainer, ExplorerViewletViewsContribution, VIEW_CONTAINER };
