
import { __decorate, __param } from '../../../../../external/tslib/tslib.es6.js';
import { DisposableStore } from '../../base/common/lifecycle.js';
import { localize } from '../../nls.js';
import { RawContextKey } from '../../platform/contextkey/common/contextkey.js';
import { IContextKeyService } from '../../platform/contextkey/common/contextkey.service.js';
import { isEqual, basename, dirname, extname } from '../../base/common/resources.js';
import { ILanguageService } from '../../editor/common/languages/language.service.js';
import { IFileService } from '../../platform/files/common/files.service.js';
import { IModelService } from '../../editor/common/services/model.service.js';
import { Schemas } from '../../base/common/network.js';
import { DEFAULT_EDITOR_ASSOCIATION } from './editor.js';

var ResourceContextKey_1;
const WorkbenchStateContext = ( new RawContextKey('workbenchState', undefined, { type: 'string', description: ( localize(
    3757,
    "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, ( localize(3758, "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, ( localize(3759, "Whether there are any working copies with unsaved changes"))));
const RemoteNameContext = ( new RawContextKey('remoteName', '', ( localize(
    3760,
    "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', '', ( localize(
    3761,
    "The scheme of the current workspace is from a virtual file system or an empty string."
))));
const TemporaryWorkspaceContext = ( new RawContextKey('temporaryWorkspace', false, ( localize(
    3762,
    "The scheme of the current workspace is from a temporary file system."
))));
const IsMainWindowFullscreenContext = ( new RawContextKey('isFullscreen', false, ( localize(3763, "Whether the main window is in fullscreen mode"))));
const IsAuxiliaryWindowFocusedContext = ( new RawContextKey('isAuxiliaryWindowFocusedContext', false, ( localize(3764, "Whether an auxiliary window is focused"))));
const HasWebFileSystemAccess = ( new RawContextKey('hasWebFileSystemAccess', false, true));
const EmbedderIdentifierContext = ( new RawContextKey('embedderIdentifier', undefined, ( localize(
    3765,
    'The identifier of the embedder according to the product service, if one is defined'
))));
const ActiveEditorDirtyContext = ( new RawContextKey('activeEditorIsDirty', false, ( localize(3766, "Whether the active editor has unsaved changes"))));
const ActiveEditorPinnedContext = ( new RawContextKey('activeEditorIsNotPreview', false, ( localize(3767, "Whether the active editor is not in preview mode"))));
const ActiveEditorFirstInGroupContext = ( new RawContextKey('activeEditorIsFirstInGroup', false, ( localize(3768, "Whether the active editor is the first one in its group"))));
const ActiveEditorLastInGroupContext = ( new RawContextKey('activeEditorIsLastInGroup', false, ( localize(3769, "Whether the active editor is the last one in its group"))));
const ActiveEditorStickyContext = ( new RawContextKey('activeEditorIsPinned', false, ( localize(3770, "Whether the active editor is pinned"))));
const ActiveEditorReadonlyContext = ( new RawContextKey('activeEditorIsReadonly', false, ( localize(3771, "Whether the active editor is read-only"))));
const ActiveCompareEditorCanSwapContext = ( new RawContextKey('activeCompareEditorCanSwap', false, ( localize(3772, "Whether the active compare editor can swap sides"))));
const ActiveEditorCanToggleReadonlyContext = ( new RawContextKey('activeEditorCanToggleReadonly', true, ( localize(
    3773,
    "Whether the active editor can toggle between being read-only or writeable"
))));
const ActiveEditorCanRevertContext = ( new RawContextKey('activeEditorCanRevert', false, ( localize(3774, "Whether the active editor can revert"))));
const ActiveEditorCanSplitInGroupContext = ( new RawContextKey('activeEditorCanSplitInGroup', true));
const ActiveEditorContext = ( new RawContextKey('activeEditor', null, { type: 'string', description: ( localize(3775, "The identifier of the active editor")) }));
const ActiveEditorAvailableEditorIdsContext = ( new RawContextKey('activeEditorAvailableEditorIds', '', ( localize(
    3776,
    "The available editor identifiers that are usable for the active editor"
))));
const TextCompareEditorVisibleContext = ( new RawContextKey('textCompareEditorVisible', false, ( localize(3777, "Whether a text compare editor is visible"))));
const TextCompareEditorActiveContext = ( new RawContextKey('textCompareEditorActive', false, ( localize(3778, "Whether a text compare editor is active"))));
const SideBySideEditorActiveContext = ( new RawContextKey('sideBySideEditorActive', false, ( localize(3779, "Whether a side by side editor is active"))));
const EditorGroupEditorsCountContext = ( new RawContextKey('groupEditorsCount', 0, ( localize(3780, "The number of opened editor groups"))));
const ActiveEditorGroupEmptyContext = ( new RawContextKey('activeEditorGroupEmpty', false, ( localize(3781, "Whether the active editor group is empty"))));
const ActiveEditorGroupIndexContext = ( new RawContextKey('activeEditorGroupIndex', 0, ( localize(3782, "The index of the active editor group"))));
const ActiveEditorGroupLastContext = ( new RawContextKey('activeEditorGroupLast', false, ( localize(3783, "Whether the active editor group is the last group"))));
const ActiveEditorGroupLockedContext = ( new RawContextKey('activeEditorGroupLocked', false, ( localize(3784, "Whether the active editor group is locked"))));
const MultipleEditorGroupsContext = ( new RawContextKey('multipleEditorGroups', false, ( localize(3785, "Whether there are multiple editor groups opened"))));
const MultipleEditorsSelectedInGroupContext = ( new RawContextKey('multipleEditorsSelectedInGroup', false, ( localize(3786, "Whether multiple editors have been selected in an editor group"))));
const TwoEditorsSelectedInGroupContext = ( new RawContextKey('twoEditorsSelectedInGroup', false, ( localize(3787, "Whether exactly two editors have been selected in an editor group"))));
const SelectedEditorsInGroupFileOrUntitledResourceContextKey = ( new RawContextKey(
    'SelectedEditorsInGroupFileOrUntitledResourceContextKey',
    true,
    ( localize(
    3788,
    "Whether all selected editors in a group have a file or untitled resource associated"
))
));
const EditorPartMultipleEditorGroupsContext = ( new RawContextKey('editorPartMultipleEditorGroups', false, ( localize(3789, "Whether there are multiple editor groups opened in an editor part"))));
const EditorPartMaximizedEditorGroupContext = ( new RawContextKey('editorPartMaximizedEditorGroup', false, ( localize(3790, "Editor Part has a maximized group"))));
const IsAuxiliaryEditorPartContext = ( new RawContextKey('isAuxiliaryEditorPart', false, ( localize(3791, "Editor Part is in an auxiliary window"))));
const EditorsVisibleContext = ( new RawContextKey('editorIsOpen', false, ( localize(3792, "Whether an editor is open"))));
const InEditorZenModeContext = ( new RawContextKey('inZenMode', false, ( localize(3793, "Whether Zen mode is enabled"))));
const IsMainEditorCenteredLayoutContext = ( new RawContextKey('isCenteredLayout', false, ( localize(3794, "Whether centered layout is enabled for the main editor"))));
const SplitEditorsVertically = ( new RawContextKey('splitEditorsVertically', false, ( localize(3795, "Whether editors split vertically"))));
const MainEditorAreaVisibleContext = ( new RawContextKey('mainEditorAreaVisible', true, ( localize(3796, "Whether the editor area in the main window is visible"))));
const EditorTabsVisibleContext = ( new RawContextKey('editorTabsVisible', true, ( localize(3797, "Whether editor tabs are visible"))));
const SideBarVisibleContext = ( new RawContextKey('sideBarVisible', false, ( localize(3798, "Whether the sidebar is visible"))));
const SidebarFocusContext = ( new RawContextKey('sideBarFocus', false, ( localize(3799, "Whether the sidebar has keyboard focus"))));
const ActiveViewletContext = ( new RawContextKey('activeViewlet', '', ( localize(3800, "The identifier of the active viewlet"))));
const StatusBarFocused = ( new RawContextKey('statusBarFocused', false, ( localize(3801, "Whether the status bar has keyboard focus"))));
const TitleBarStyleContext = ( new RawContextKey('titleBarStyle', 'custom', ( localize(3802, "Style of the window title bar"))));
const TitleBarVisibleContext = ( new RawContextKey('titleBarVisible', false, ( localize(3803, "Whether the title bar is visible"))));
const BannerFocused = ( new RawContextKey('bannerFocused', false, ( localize(3804, "Whether the banner has keyboard focus"))));
const NotificationFocusedContext = ( new RawContextKey('notificationFocus', true, ( localize(3805, "Whether a notification has keyboard focus"))));
const NotificationsCenterVisibleContext = ( new RawContextKey('notificationCenterVisible', false, ( localize(3806, "Whether the notifications center is visible"))));
const NotificationsToastsVisibleContext = ( new RawContextKey('notificationToastsVisible', false, ( localize(3807, "Whether a notification toast is visible"))));
const ActiveAuxiliaryContext = ( new RawContextKey('activeAuxiliary', '', ( localize(3808, "The identifier of the active auxiliary panel"))));
const AuxiliaryBarFocusContext = ( new RawContextKey('auxiliaryBarFocus', false, ( localize(3809, "Whether the auxiliary bar has keyboard focus"))));
const AuxiliaryBarVisibleContext = ( new RawContextKey('auxiliaryBarVisible', false, ( localize(3810, "Whether the auxiliary bar is visible"))));
const ActivePanelContext = ( new RawContextKey('activePanel', '', ( localize(3811, "The identifier of the active panel"))));
const PanelFocusContext = ( new RawContextKey('panelFocus', false, ( localize(3812, "Whether the panel has keyboard focus"))));
const PanelPositionContext = ( new RawContextKey('panelPosition', 'bottom', ( localize(3813, "The position of the panel, always 'bottom'"))));
const PanelAlignmentContext = ( new RawContextKey('panelAlignment', 'center', ( localize(
    3814,
    "The alignment of the panel, either 'center', 'left', 'right' or 'justify'"
))));
const PanelVisibleContext = ( new RawContextKey('panelVisible', false, ( localize(3815, "Whether the panel is visible"))));
const PanelMaximizedContext = ( new RawContextKey('panelMaximized', false, ( localize(3816, "Whether the panel is maximized"))));
const FocusedViewContext = ( new RawContextKey('focusedView', '', ( localize(3817, "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: ( localize(3818, "The scheme of the resource")) })); }
    static { this.Filename = ( new RawContextKey(
        'resourceFilename',
        undefined,
        { type: 'string', description: ( localize(3819, "The file name of the resource")) }
    )); }
    static { this.Dirname = ( new RawContextKey(
        'resourceDirname',
        undefined,
        { type: 'string', description: ( localize(3820, "The folder name the resource is contained in")) }
    )); }
    static { this.Path = ( new RawContextKey('resourcePath', undefined, { type: 'string', description: ( localize(3821, "The full path of the resource")) })); }
    static { this.LangId = ( new RawContextKey('resourceLangId', undefined, { type: 'string', description: ( localize(3822, "The language identifier of the resource")) })); }
    static { this.Resource = ( new RawContextKey('resource', undefined, { type: 'URI', description: ( localize(3823, "The full value of the resource including scheme and path")) })); }
    static { this.Extension = ( new RawContextKey(
        'resourceExtname',
        undefined,
        { type: 'string', description: ( localize(3824, "The extension name of the resource")) }
    )); }
    static { this.HasResource = ( new RawContextKey('resourceSet', undefined, { type: 'boolean', description: ( localize(3825, "Whether a resource is present or not")) })); }
    static { this.IsFileSystemResource = ( new RawContextKey(
        'isFileSystemResource',
        undefined,
        { type: 'boolean', description: ( localize(3826, "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;
    if (editorResource?.scheme === Schemas.untitled && editor.editorId !== DEFAULT_EDITOR_ASSOCIATION.id) {
        contextKey.set('');
    }
    else {
        const editors = editorResource ? ( editorResolverService.getEditors(editorResource).map(editor => editor.id)) : [];
        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, MultipleEditorsSelectedInGroupContext, NotificationFocusedContext, NotificationsCenterVisibleContext, NotificationsToastsVisibleContext, OpenFolderWorkspaceSupportContext, PanelAlignmentContext, PanelFocusContext, PanelMaximizedContext, PanelPositionContext, PanelVisibleContext, RemoteNameContext, ResourceContextKey, SelectedEditorsInGroupFileOrUntitledResourceContextKey, SideBarVisibleContext, SideBySideEditorActiveContext, SidebarFocusContext, SplitEditorsVertically, StatusBarFocused, TemporaryWorkspaceContext, TextCompareEditorActiveContext, TextCompareEditorVisibleContext, TitleBarStyleContext, TitleBarVisibleContext, TwoEditorsSelectedInGroupContext, VirtualWorkspaceContext, WorkbenchStateContext, WorkspaceFolderCountContext, applyAvailableEditorIds, getVisbileViewContextKey };
