import getExtensionServiceOverride from '@codingame/monaco-vscode-extensions-service-override';
import getModelServiceOverride from '@codingame/monaco-vscode-model-service-override';
import getDialogsServiceOverride from '@codingame/monaco-vscode-dialogs-service-override';
import getConfigurationServiceOverride from '@codingame/monaco-vscode-configuration-service-override';
import getKeybindingsServiceOverride from '@codingame/monaco-vscode-keybindings-service-override';
import getTextmateServiceOverride from '@codingame/monaco-vscode-textmate-service-override';
import getThemeServiceOverride from '@codingame/monaco-vscode-theme-service-override';
import getLanguagesServiceOverride from '@codingame/monaco-vscode-languages-service-override';
import getPreferencesServiceOverride from '@codingame/monaco-vscode-preferences-service-override';
import getSnippetServiceOverride from '@codingame/monaco-vscode-snippets-service-override';
import getAccessibilityServiceOverride from '@codingame/monaco-vscode-accessibility-service-override';
import getLanguageDetectionWorkerServiceOverride from '@codingame/monaco-vscode-language-detection-worker-service-override';
import getEditorServiceOverride from '@codingame/monaco-vscode-editor-service-override';
import getStorageServiceOverride from '@codingame/monaco-vscode-storage-service-override';
import getLifecycleServiceOverride from '@codingame/monaco-vscode-lifecycle-service-override';
import getQuickAccessServiceOverride from '@codingame/monaco-vscode-quickaccess-service-override';
import getLogServiceOverride from '@codingame/monaco-vscode-log-service-override';
import getEmmetServiceOverride from '@codingame/monaco-vscode-emmet-service-override';
import getWorkingCopyServiceOverride from '@codingame/monaco-vscode-working-copy-service-override';
import { initialize as initialize$1 } from '@codingame/monaco-vscode-api';
import * as monaco from 'monaco-editor';
import { OverlayFileSystemProvider, EmptyFileSystemProvider, RegisteredFileSystemProvider, registerCustomProvider, RegisteredMemoryFile } from '@codingame/monaco-vscode-files-service-override';
import EditorOpenHandlerRegistry from './tools/EditorOpenHandlerRegistry.js';
import { whenReady } from './extensions.js';
import 'vscode/localExtensionHost';
import './languages/index.js';
import './worker.js';

// Overwrite the default overlay filesystem to remove the InMemoryFileSystemProvider fallback
const overlayFileSystem = new OverlayFileSystemProvider();
overlayFileSystem.register(0, new EmptyFileSystemProvider());
const defaultFilesystemProvider = new RegisteredFileSystemProvider(false);
overlayFileSystem.register(1, defaultFilesystemProvider);
registerCustomProvider('file', overlayFileSystem);
function registerFile(file) {
    return defaultFilesystemProvider.registerFile(file);
}
const editorOpenHandlerRegistry = new EditorOpenHandlerRegistry();
let _useGlobalPicker = false;
function setUseGlobalPicker(useGlobalPicker = true) {
    _useGlobalPicker = useGlobalPicker;
}
function useGlobalPicker() {
    // should picker and keybindings be global or per-editor
    return typeof _useGlobalPicker === 'function' ? _useGlobalPicker() : _useGlobalPicker;
}
let services = {
    ...getLogServiceOverride(),
    ...getExtensionServiceOverride(),
    ...getModelServiceOverride(),
    ...getDialogsServiceOverride(),
    ...getConfigurationServiceOverride(),
    ...getKeybindingsServiceOverride(),
    ...getTextmateServiceOverride(),
    ...getThemeServiceOverride(),
    ...getLanguagesServiceOverride(),
    ...getPreferencesServiceOverride(),
    ...getSnippetServiceOverride(),
    ...getLanguageDetectionWorkerServiceOverride(),
    ...getEditorServiceOverride((model, input, sideBySide) => {
        return editorOpenHandlerRegistry.openCodeEditor(model, input, sideBySide);
    }),
    ...getAccessibilityServiceOverride(),
    ...getStorageServiceOverride({
        fallbackOverride: {
            'workbench.activity.showAccounts': false
        }
    }),
    ...getLifecycleServiceOverride(),
    ...getQuickAccessServiceOverride({
        shouldUseGlobalPicker() {
            return useGlobalPicker();
        }
    }),
    ...getEmmetServiceOverride(),
    ...getWorkingCopyServiceOverride({
        storage: null
    })
};
function registerServices(newServices) {
    services = {
        ...services,
        ...newServices
    };
}
function generateAndInitializeWorkspace(workspaceFile = monaco.Uri.file('/workspace.code-workspace'), label) {
    registerFile(new RegisteredMemoryFile(workspaceFile, JSON.stringify({
        folders: [
            {
                path: '/tmp/project'
            }
        ]
    })));
    return {
        open: async () => false,
        workspace: {
            workspaceUri: workspaceFile,
            label
        },
        trusted: true
    };
}
let initialized = false;
let setInitialized;
const initializePromise = new Promise((resolve) => {
    setInitialized = resolve;
});
void initializePromise.then(() => {
    initialized = true;
});
function isInitialized() {
    return initialized;
}
async function initialize(constructionOptions = {}, { container, registerAdditionalExtensions = true, waitForDefaultExtensions = true } = {}) {
    if (constructionOptions.workspaceProvider == null) {
        constructionOptions = {
            ...constructionOptions,
            workspaceProvider: generateAndInitializeWorkspace()
        };
    }
    await initialize$1(services, container, constructionOptions, {
        userHome: monaco.Uri.file('/')
    });
    if (registerAdditionalExtensions) {
        const { whenReady } = await import('./additionalExtensions.js').then(module => module);
        if (waitForDefaultExtensions) {
            await whenReady();
        }
    }
    if (waitForDefaultExtensions) {
        await whenReady();
    }
    setInitialized();
}

export { editorOpenHandlerRegistry, generateAndInitializeWorkspace, initialize, initializePromise, isInitialized, registerFile, registerServices, setUseGlobalPicker, useGlobalPicker };
