import { __decorate, __param } from '../../../../../external/tslib/tslib.es6.js';
import { DisposableStore } from '../../base/common/lifecycle.js';
import { localizeWithPath } from '../../nls.js';
import { RawContextKey, IContextKeyService } from '../../platform/contextkey/common/contextkey.js';
import { isEqual, basename, dirname, extname } from '../../base/common/resources.js';
import { ILanguageService } from '../../editor/common/languages/language.js';
import { IFileService } from '../../platform/files/common/files.js';
import { IModelService } from '../../editor/common/services/model.js';
import { Schemas } from '../../base/common/network.js';
import { DEFAULT_EDITOR_ASSOCIATION } from './editor.js';
import { isLinux } from '../../base/common/platform.js';
var ResourceContextKey_1;
const WorkbenchStateContext = ( new RawContextKey('workbenchState', undefined, { type: 'string', description: ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'workbenchState',
    "The kind of workspace opened in the window, either 'empty' (no workspace), 'folder' (single folder) or 'workspace' (multi-root workspace)"
)) }));
const WorkspaceFolderCountContext = ( new RawContextKey('workspaceFolderCount', 0, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'workspaceFolderCount',
    "The number of root folders in the workspace"
))));
const OpenFolderWorkspaceSupportContext = ( new RawContextKey('openFolderWorkspaceSupport', true, true));
const EnterMultiRootWorkspaceSupportContext = ( new RawContextKey('enterMultiRootWorkspaceSupport', true, true));
const EmptyWorkspaceSupportContext = ( new RawContextKey('emptyWorkspaceSupport', true, true));
const DirtyWorkingCopiesContext = ( new RawContextKey('dirtyWorkingCopies', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'dirtyWorkingCopies',
    "Whether there are any working copies with unsaved changes"
))));
const RemoteNameContext = ( new RawContextKey('remoteName', '', ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'remoteName',
    "The name of the remote the window is connected to or an empty string if not connected to any remote"
))));
const VirtualWorkspaceContext = ( new RawContextKey('virtualWorkspace', '', ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'virtualWorkspace',
    "The scheme of the current workspace is from a virtual file system or an empty string."
))));
const TemporaryWorkspaceContext = ( new RawContextKey('temporaryWorkspace', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'temporaryWorkspace',
    "The scheme of the current workspace is from a temporary file system."
))));
const IsMainWindowFullscreenContext = ( new RawContextKey('isFullscreen', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'isFullscreen',
    "Whether the main window is in fullscreen mode"
))));
const IsAuxiliaryWindowFocusedContext = ( new RawContextKey('isAuxiliaryWindowFocusedContext', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'isAuxiliaryWindowFocusedContext',
    "Whether an auxiliary window is focused"
))));
const HasWebFileSystemAccess = ( new RawContextKey('hasWebFileSystemAccess', false, true));
const EmbedderIdentifierContext = ( new RawContextKey('embedderIdentifier', undefined, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'embedderIdentifier',
    'The identifier of the embedder according to the product service, if one is defined'
))));
const ActiveEditorDirtyContext = ( new RawContextKey('activeEditorIsDirty', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeEditorIsDirty',
    "Whether the active editor has unsaved changes"
))));
const ActiveEditorPinnedContext = ( new RawContextKey('activeEditorIsNotPreview', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeEditorIsNotPreview',
    "Whether the active editor is not in preview mode"
))));
const ActiveEditorFirstInGroupContext = ( new RawContextKey('activeEditorIsFirstInGroup', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeEditorIsFirstInGroup',
    "Whether the active editor is the first one in its group"
))));
const ActiveEditorLastInGroupContext = ( new RawContextKey('activeEditorIsLastInGroup', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeEditorIsLastInGroup',
    "Whether the active editor is the last one in its group"
))));
const ActiveEditorStickyContext = ( new RawContextKey('activeEditorIsPinned', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeEditorIsPinned',
    "Whether the active editor is pinned"
))));
const ActiveEditorReadonlyContext = ( new RawContextKey('activeEditorIsReadonly', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeEditorIsReadonly',
    "Whether the active editor is read-only"
))));
const ActiveCompareEditorCanSwapContext = ( new RawContextKey('activeCompareEditorCanSwap', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeCompareEditorCanSwap',
    "Whether the active compare editor can swap sides"
))));
const ActiveEditorCanToggleReadonlyContext = ( new RawContextKey('activeEditorCanToggleReadonly', true, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeEditorCanToggleReadonly',
    "Whether the active editor can toggle between being read-only or writeable"
))));
const ActiveEditorCanRevertContext = ( new RawContextKey('activeEditorCanRevert', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeEditorCanRevert',
    "Whether the active editor can revert"
))));
const ActiveEditorCanSplitInGroupContext = ( new RawContextKey('activeEditorCanSplitInGroup', true));
const ActiveEditorContext = ( new RawContextKey('activeEditor', null, { type: 'string', description: ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeEditor',
    "The identifier of the active editor"
)) }));
const ActiveEditorAvailableEditorIdsContext = ( new RawContextKey('activeEditorAvailableEditorIds', '', ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeEditorAvailableEditorIds',
    "The available editor identifiers that are usable for the active editor"
))));
const TextCompareEditorVisibleContext = ( new RawContextKey('textCompareEditorVisible', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'textCompareEditorVisible',
    "Whether a text compare editor is visible"
))));
const TextCompareEditorActiveContext = ( new RawContextKey('textCompareEditorActive', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'textCompareEditorActive',
    "Whether a text compare editor is active"
))));
const SideBySideEditorActiveContext = ( new RawContextKey('sideBySideEditorActive', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'sideBySideEditorActive',
    "Whether a side by side editor is active"
))));
const EditorGroupEditorsCountContext = ( new RawContextKey('groupEditorsCount', 0, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'groupEditorsCount',
    "The number of opened editor groups"
))));
const ActiveEditorGroupEmptyContext = ( new RawContextKey('activeEditorGroupEmpty', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeEditorGroupEmpty',
    "Whether the active editor group is empty"
))));
const ActiveEditorGroupIndexContext = ( new RawContextKey('activeEditorGroupIndex', 0, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeEditorGroupIndex',
    "The index of the active editor group"
))));
const ActiveEditorGroupLastContext = ( new RawContextKey('activeEditorGroupLast', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeEditorGroupLast',
    "Whether the active editor group is the last group"
))));
const ActiveEditorGroupLockedContext = ( new RawContextKey('activeEditorGroupLocked', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeEditorGroupLocked',
    "Whether the active editor group is locked"
))));
const MultipleEditorGroupsContext = ( new RawContextKey('multipleEditorGroups', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'multipleEditorGroups',
    "Whether there are multiple editor groups opened"
))));
const EditorPartMultipleEditorGroupsContext = ( new RawContextKey('editorPartMultipleEditorGroups', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'editorPartMultipleEditorGroups',
    "Whether there are multiple editor groups opened in an editor part"
))));
const EditorPartMaximizedEditorGroupContext = ( new RawContextKey('editorPartMaximizedEditorGroup', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'editorPartEditorGroupMaximized',
    "Editor Part has a maximized group"
))));
const IsAuxiliaryEditorPartContext = ( new RawContextKey('isAuxiliaryEditorPart', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'isAuxiliaryEditorPart',
    "Editor Part is in an auxiliary window"
))));
const EditorsVisibleContext = ( new RawContextKey('editorIsOpen', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'editorIsOpen',
    "Whether an editor is open"
))));
const InEditorZenModeContext = ( new RawContextKey('inZenMode', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'inZenMode',
    "Whether Zen mode is enabled"
))));
const IsMainEditorCenteredLayoutContext = ( new RawContextKey('isCenteredLayout', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'isMainEditorCenteredLayout',
    "Whether centered layout is enabled for the main editor"
))));
const SplitEditorsVertically = ( new RawContextKey('splitEditorsVertically', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'splitEditorsVertically',
    "Whether editors split vertically"
))));
const MainEditorAreaVisibleContext = ( new RawContextKey('mainEditorAreaVisible', true, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'mainEditorAreaVisible',
    "Whether the editor area in the main window is visible"
))));
const EditorTabsVisibleContext = ( new RawContextKey('editorTabsVisible', true, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'editorTabsVisible',
    "Whether editor tabs are visible"
))));
const SideBarVisibleContext = ( new RawContextKey('sideBarVisible', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'sideBarVisible',
    "Whether the sidebar is visible"
))));
const SidebarFocusContext = ( new RawContextKey('sideBarFocus', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'sideBarFocus',
    "Whether the sidebar has keyboard focus"
))));
const ActiveViewletContext = ( new RawContextKey('activeViewlet', '', ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeViewlet',
    "The identifier of the active viewlet"
))));
const StatusBarFocused = ( new RawContextKey('statusBarFocused', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'statusBarFocused',
    "Whether the status bar has keyboard focus"
))));
const TitleBarStyleContext = ( new RawContextKey('titleBarStyle', isLinux ? 'native' : 'custom', ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'titleBarStyle',
    "Style of the window title bar"
))));
const TitleBarVisibleContext = ( new RawContextKey('titleBarVisible', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'titleBarVisible',
    "Whether the title bar is visible"
))));
const BannerFocused = ( new RawContextKey('bannerFocused', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'bannerFocused',
    "Whether the banner has keyboard focus"
))));
const NotificationFocusedContext = ( new RawContextKey('notificationFocus', true, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'notificationFocus',
    "Whether a notification has keyboard focus"
))));
const NotificationsCenterVisibleContext = ( new RawContextKey('notificationCenterVisible', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'notificationCenterVisible',
    "Whether the notifications center is visible"
))));
const NotificationsToastsVisibleContext = ( new RawContextKey('notificationToastsVisible', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'notificationToastsVisible',
    "Whether a notification toast is visible"
))));
const ActiveAuxiliaryContext = ( new RawContextKey('activeAuxiliary', '', ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activeAuxiliary',
    "The identifier of the active auxiliary panel"
))));
const AuxiliaryBarFocusContext = ( new RawContextKey('auxiliaryBarFocus', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'auxiliaryBarFocus',
    "Whether the auxiliary bar has keyboard focus"
))));
const AuxiliaryBarVisibleContext = ( new RawContextKey('auxiliaryBarVisible', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'auxiliaryBarVisible',
    "Whether the auxiliary bar is visible"
))));
const ActivePanelContext = ( new RawContextKey('activePanel', '', ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'activePanel',
    "The identifier of the active panel"
))));
const PanelFocusContext = ( new RawContextKey('panelFocus', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'panelFocus',
    "Whether the panel has keyboard focus"
))));
const PanelPositionContext = ( new RawContextKey('panelPosition', 'bottom', ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'panelPosition',
    "The position of the panel, always 'bottom'"
))));
const PanelAlignmentContext = ( new RawContextKey('panelAlignment', 'center', ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'panelAlignment',
    "The alignment of the panel, either 'center', 'left', 'right' or 'justify'"
))));
const PanelVisibleContext = ( new RawContextKey('panelVisible', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'panelVisible',
    "Whether the panel is visible"
))));
const PanelMaximizedContext = ( new RawContextKey('panelMaximized', false, ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'panelMaximized',
    "Whether the panel is maximized"
))));
const FocusedViewContext = ( new RawContextKey('focusedView', '', ( localizeWithPath(
    'vs/workbench/common/contextkeys',
    'focusedView',
    "The identifier of the view that has keyboard focus"
))));
function getVisbileViewContextKey(viewId) { return `view.${viewId}.visible`; }
let ResourceContextKey = class ResourceContextKey {
    static { ResourceContextKey_1 = this; }
    static { this.Scheme = ( new RawContextKey('resourceScheme', undefined, { type: 'string', description: ( localizeWithPath(
        'vs/workbench/common/contextkeys',
        'resourceScheme',
        "The scheme of the resource"
    )) })); }
    static { this.Filename = ( new RawContextKey(
        'resourceFilename',
        undefined,
        { type: 'string', description: ( localizeWithPath(
            'vs/workbench/common/contextkeys',
            'resourceFilename',
            "The file name of the resource"
        )) }
    )); }
    static { this.Dirname = ( new RawContextKey(
        'resourceDirname',
        undefined,
        { type: 'string', description: ( localizeWithPath(
            'vs/workbench/common/contextkeys',
            'resourceDirname',
            "The folder name the resource is contained in"
        )) }
    )); }
    static { this.Path = ( new RawContextKey('resourcePath', undefined, { type: 'string', description: ( localizeWithPath(
        'vs/workbench/common/contextkeys',
        'resourcePath',
        "The full path of the resource"
    )) })); }
    static { this.LangId = ( new RawContextKey('resourceLangId', undefined, { type: 'string', description: ( localizeWithPath(
        'vs/workbench/common/contextkeys',
        'resourceLangId',
        "The language identifier of the resource"
    )) })); }
    static { this.Resource = ( new RawContextKey('resource', undefined, { type: 'URI', description: ( localizeWithPath(
        'vs/workbench/common/contextkeys',
        'resource',
        "The full value of the resource including scheme and path"
    )) })); }
    static { this.Extension = ( new RawContextKey(
        'resourceExtname',
        undefined,
        { type: 'string', description: ( localizeWithPath(
            'vs/workbench/common/contextkeys',
            'resourceExtname',
            "The extension name of the resource"
        )) }
    )); }
    static { this.HasResource = ( new RawContextKey('resourceSet', undefined, { type: 'boolean', description: ( localizeWithPath(
        'vs/workbench/common/contextkeys',
        'resourceSet',
        "Whether a resource is present or not"
    )) })); }
    static { this.IsFileSystemResource = ( new RawContextKey(
        'isFileSystemResource',
        undefined,
        { type: 'boolean', description: ( localizeWithPath(
            'vs/workbench/common/contextkeys',
            'isFileSystemResource',
            "Whether the resource is backed by a file system provider"
        )) }
    )); }
    constructor(_contextKeyService, _fileService, _languageService, _modelService) {
        this._contextKeyService = _contextKeyService;
        this._fileService = _fileService;
        this._languageService = _languageService;
        this._modelService = _modelService;
        this._disposables = ( new DisposableStore());
        this._schemeKey = ResourceContextKey_1.Scheme.bindTo(this._contextKeyService);
        this._filenameKey = ResourceContextKey_1.Filename.bindTo(this._contextKeyService);
        this._dirnameKey = ResourceContextKey_1.Dirname.bindTo(this._contextKeyService);
        this._pathKey = ResourceContextKey_1.Path.bindTo(this._contextKeyService);
        this._langIdKey = ResourceContextKey_1.LangId.bindTo(this._contextKeyService);
        this._resourceKey = ResourceContextKey_1.Resource.bindTo(this._contextKeyService);
        this._extensionKey = ResourceContextKey_1.Extension.bindTo(this._contextKeyService);
        this._hasResource = ResourceContextKey_1.HasResource.bindTo(this._contextKeyService);
        this._isFileSystemResource = ResourceContextKey_1.IsFileSystemResource.bindTo(this._contextKeyService);
        this._disposables.add(_fileService.onDidChangeFileSystemProviderRegistrations(() => {
            const resource = this.get();
            this._isFileSystemResource.set(Boolean(resource && _fileService.hasProvider(resource)));
        }));
        this._disposables.add(_modelService.onModelAdded(model => {
            if (isEqual(model.uri, this.get())) {
                this._setLangId();
            }
        }));
        this._disposables.add(_modelService.onModelLanguageChanged(e => {
            if (isEqual(e.model.uri, this.get())) {
                this._setLangId();
            }
        }));
    }
    dispose() {
        this._disposables.dispose();
    }
    _setLangId() {
        const value = this.get();
        if (!value) {
            this._langIdKey.set(null);
            return;
        }
        const langId = this._modelService.getModel(value)?.getLanguageId() ?? this._languageService.guessLanguageIdByFilepathOrFirstLine(value);
        this._langIdKey.set(langId);
    }
    set(value) {
        value = value ?? undefined;
        if (isEqual(this._value, value)) {
            return;
        }
        this._value = value;
        this._contextKeyService.bufferChangeEvents(() => {
            this._resourceKey.set(value ? ( value.toString()) : null);
            this._schemeKey.set(value ? value.scheme : null);
            this._filenameKey.set(value ? basename(value) : null);
            this._dirnameKey.set(value ? this.uriToPath(dirname(value)) : null);
            this._pathKey.set(value ? this.uriToPath(value) : null);
            this._setLangId();
            this._extensionKey.set(value ? extname(value) : null);
            this._hasResource.set(Boolean(value));
            this._isFileSystemResource.set(value ? this._fileService.hasProvider(value) : false);
        });
    }
    uriToPath(uri) {
        if (uri.scheme === Schemas.file) {
            return uri.fsPath;
        }
        return uri.path;
    }
    reset() {
        this._value = undefined;
        this._contextKeyService.bufferChangeEvents(() => {
            this._resourceKey.reset();
            this._schemeKey.reset();
            this._filenameKey.reset();
            this._dirnameKey.reset();
            this._pathKey.reset();
            this._langIdKey.reset();
            this._extensionKey.reset();
            this._hasResource.reset();
            this._isFileSystemResource.reset();
        });
    }
    get() {
        return this._value;
    }
};
ResourceContextKey = ResourceContextKey_1 = ( __decorate([
    ( __param(0, IContextKeyService)),
    ( __param(1, IFileService)),
    ( __param(2, ILanguageService)),
    ( __param(3, IModelService))
], ResourceContextKey));
function applyAvailableEditorIds(contextKey, editor, editorResolverService) {
    if (!editor) {
        contextKey.set('');
        return;
    }
    const editorResource = editor.resource;
    const editors = editorResource ? ( editorResolverService.getEditors(editorResource).map(editor => editor.id)) : [];
    if (editorResource?.scheme === Schemas.untitled && editor.editorId !== DEFAULT_EDITOR_ASSOCIATION.id) {
        contextKey.set('');
    }
    else {
        contextKey.set(editors.join(','));
    }
}
export { ActiveAuxiliaryContext, ActiveCompareEditorCanSwapContext, ActiveEditorAvailableEditorIdsContext, ActiveEditorCanRevertContext, ActiveEditorCanSplitInGroupContext, ActiveEditorCanToggleReadonlyContext, ActiveEditorContext, ActiveEditorDirtyContext, ActiveEditorFirstInGroupContext, ActiveEditorGroupEmptyContext, ActiveEditorGroupIndexContext, ActiveEditorGroupLastContext, ActiveEditorGroupLockedContext, ActiveEditorLastInGroupContext, ActiveEditorPinnedContext, ActiveEditorReadonlyContext, ActiveEditorStickyContext, ActivePanelContext, ActiveViewletContext, AuxiliaryBarFocusContext, AuxiliaryBarVisibleContext, BannerFocused, DirtyWorkingCopiesContext, EditorGroupEditorsCountContext, EditorPartMaximizedEditorGroupContext, EditorPartMultipleEditorGroupsContext, EditorTabsVisibleContext, EditorsVisibleContext, EmbedderIdentifierContext, EmptyWorkspaceSupportContext, EnterMultiRootWorkspaceSupportContext, FocusedViewContext, HasWebFileSystemAccess, InEditorZenModeContext, IsAuxiliaryEditorPartContext, IsAuxiliaryWindowFocusedContext, IsMainEditorCenteredLayoutContext, IsMainWindowFullscreenContext, MainEditorAreaVisibleContext, MultipleEditorGroupsContext, NotificationFocusedContext, NotificationsCenterVisibleContext, NotificationsToastsVisibleContext, OpenFolderWorkspaceSupportContext, PanelAlignmentContext, PanelFocusContext, PanelMaximizedContext, PanelPositionContext, PanelVisibleContext, RemoteNameContext, ResourceContextKey, SideBarVisibleContext, SideBySideEditorActiveContext, SidebarFocusContext, SplitEditorsVertically, StatusBarFocused, TemporaryWorkspaceContext, TextCompareEditorActiveContext, TextCompareEditorVisibleContext, TitleBarStyleContext, TitleBarVisibleContext, VirtualWorkspaceContext, WorkbenchStateContext, WorkspaceFolderCountContext, applyAvailableEditorIds, getVisbileViewContextKey };
