/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
import { getLsConfiguration } from './config.js';
import { MdExtractLinkDefinitionCodeActionProvider } from './languageFeatures/codeActions/extractLinkDef.js';
import { MdRemoveLinkDefinitionCodeActionProvider } from './languageFeatures/codeActions/removeLinkDefinition.js';
import { MdDefinitionProvider } from './languageFeatures/definitions.js';
import { DiagnosticComputer, DiagnosticsManager } from './languageFeatures/diagnostics.js';
import { MdDocumentHighlightProvider } from './languageFeatures/documentHighlights.js';
import { createWorkspaceLinkCache, MdLinkProvider } from './languageFeatures/documentLinks.js';
import { MdDocumentSymbolProvider } from './languageFeatures/documentSymbols.js';
import { MdFileRenameProvider } from './languageFeatures/fileRename.js';
import { MdFoldingProvider } from './languageFeatures/folding.js';
import { MdHoverProvider } from './languageFeatures/hover.js';
import { MdOrganizeLinkDefinitionProvider } from './languageFeatures/organizeLinkDefs.js';
import { MdPathCompletionProvider } from './languageFeatures/pathCompletions.js';
import { MdReferencesProvider } from './languageFeatures/references.js';
import { MdRenameProvider } from './languageFeatures/rename.js';
import { MdSelectionRangeProvider } from './languageFeatures/smartSelect.js';
import { MdUpdatePastedLinksProvider } from './languageFeatures/updatePastedLinks.js';
import { MdWorkspaceSymbolProvider } from './languageFeatures/workspaceSymbols.js';
import { MdTableOfContentsProvider } from './tableOfContents.js';
import { isWorkspaceWithFileWatching } from './workspace.js';
export { PreferredMdPathExtensionStyle } from './config.js';
export { DiagnosticCode, DiagnosticLevel } from './languageFeatures/diagnostics.js';
export { IncludeWorkspaceHeaderCompletions } from './languageFeatures/pathCompletions.js';
export { RenameNotSupportedAtLocationError } from './languageFeatures/rename.js';
export { LogLevel } from './logging.js';
export { githubSlugifier } from './slugify.js';
/**
 * Create a new instance of the {@link IMdLanguageService language service}.
 */
export function createLanguageService(init) {
    const config = getLsConfiguration(init);
    const logger = init.logger;
    const tocProvider = new MdTableOfContentsProvider(init.parser, init.workspace, logger);
    const foldingProvider = new MdFoldingProvider(init.parser, tocProvider, logger);
    const linkProvider = new MdLinkProvider(config, init.parser, init.workspace, tocProvider, logger);
    const pathCompletionProvider = new MdPathCompletionProvider(config, init.workspace, linkProvider, tocProvider);
    const linkCache = createWorkspaceLinkCache(init.parser, init.workspace);
    const smartSelectProvider = new MdSelectionRangeProvider(init.parser, tocProvider, linkProvider, logger);
    const referencesProvider = new MdReferencesProvider(config, init.parser, init.workspace, tocProvider, linkCache, logger);
    const definitionsProvider = new MdDefinitionProvider(config, init.workspace, tocProvider, linkCache);
    const renameProvider = new MdRenameProvider(config, init.workspace, init.parser, referencesProvider, tocProvider, init.parser.slugifier, logger);
    const fileRenameProvider = new MdFileRenameProvider(config, init.workspace, linkCache, referencesProvider);
    const diagnosticsComputer = new DiagnosticComputer(config, init.workspace, linkProvider, tocProvider, logger);
    const docSymbolProvider = new MdDocumentSymbolProvider(tocProvider, linkProvider, logger);
    const workspaceSymbolProvider = new MdWorkspaceSymbolProvider(init.workspace, docSymbolProvider);
    const organizeLinkDefinitions = new MdOrganizeLinkDefinitionProvider(linkProvider);
    const documentHighlightProvider = new MdDocumentHighlightProvider(config, tocProvider, linkProvider);
    const rewritePastedLinksProvider = new MdUpdatePastedLinksProvider(config, linkProvider);
    const hoverProvider = new MdHoverProvider(linkProvider);
    const extractCodeActionProvider = new MdExtractLinkDefinitionCodeActionProvider(linkProvider);
    const removeLinkDefinitionActionProvider = new MdRemoveLinkDefinitionCodeActionProvider();
    return Object.freeze({
        dispose: () => {
            linkCache.dispose();
            tocProvider.dispose();
            workspaceSymbolProvider.dispose();
            linkProvider.dispose();
            referencesProvider.dispose();
        },
        getDocumentLinks: linkProvider.provideDocumentLinks.bind(linkProvider),
        resolveDocumentLink: linkProvider.resolveDocumentLink.bind(linkProvider),
        resolveLinkTarget: linkProvider.resolveLinkTarget.bind(linkProvider),
        getDocumentSymbols: docSymbolProvider.provideDocumentSymbols.bind(docSymbolProvider),
        getFoldingRanges: foldingProvider.provideFoldingRanges.bind(foldingProvider),
        getSelectionRanges: smartSelectProvider.provideSelectionRanges.bind(smartSelectProvider),
        getWorkspaceSymbols: workspaceSymbolProvider.provideWorkspaceSymbols.bind(workspaceSymbolProvider),
        getCompletionItems: pathCompletionProvider.provideCompletionItems.bind(pathCompletionProvider),
        getReferences: referencesProvider.provideReferences.bind(referencesProvider),
        getFileReferences: async (resource, token) => {
            return (await referencesProvider.getReferencesToFileInWorkspace(resource, token)).map(x => x.location);
        },
        getDefinition: definitionsProvider.provideDefinition.bind(definitionsProvider),
        organizeLinkDefinitions: organizeLinkDefinitions.getOrganizeLinkDefinitionEdits.bind(organizeLinkDefinitions),
        prepareRename: renameProvider.prepareRename.bind(renameProvider),
        getRenameEdit: renameProvider.provideRenameEdits.bind(renameProvider),
        getRenameFilesInWorkspaceEdit: fileRenameProvider.getRenameFilesInWorkspaceEdit.bind(fileRenameProvider),
        getCodeActions: async (doc, range, context, token) => {
            return (await Promise.all([
                extractCodeActionProvider.getActions(doc, range, context, token),
                Array.from(removeLinkDefinitionActionProvider.getActions(doc, range, context)),
            ])).flat();
        },
        getDocumentHighlights: (document, position, token) => {
            return documentHighlightProvider.getDocumentHighlights(document, position, token);
        },
        prepareUpdatePastedLinks: rewritePastedLinksProvider.prepareDocumentPaste.bind(rewritePastedLinksProvider),
        getUpdatePastedLinksEdit: rewritePastedLinksProvider.provideDocumentPasteEdits.bind(rewritePastedLinksProvider),
        getHover: hoverProvider.provideHover.bind(hoverProvider),
        computeDiagnostics: async (doc, options, token) => {
            return (await diagnosticsComputer.compute(doc, options, token))?.diagnostics;
        },
        createPullDiagnosticsManager: () => {
            if (!isWorkspaceWithFileWatching(init.workspace)) {
                throw new Error(`Workspace does not support file watching. Diagnostics manager not supported`);
            }
            return new DiagnosticsManager(config, init.workspace, linkProvider, tocProvider, logger);
        }
    });
}
//# sourceMappingURL=index.js.map