"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const json = __importStar(require("vscode-json-languageservice"));
exports.default = (settings) => (context) => {
    const triggerCharacters = {
        // https://github.com/microsoft/vscode/blob/09850876e652688fb142e2e19fd00fd38c0bc4ba/extensions/json-language-features/server/src/jsonServer.ts#L150
        triggerCharacters: ['"', ':'],
    };
    if (!context) {
        return triggerCharacters;
    }
    const jsonDocuments = new WeakMap();
    const jsonLs = json.getLanguageService({ schemaRequestService: context.schemaRequestService });
    if (settings) {
        jsonLs.configure(settings);
    }
    return {
        ...triggerCharacters,
        async resolveRuleContext(context) {
            await worker(context.document, async (jsonDocument) => {
                context.json = {
                    document: jsonDocument,
                    languageService: jsonLs,
                };
            });
            return context;
        },
        provideCompletionItems(document, position) {
            return worker(document, async (jsonDocument) => {
                return await jsonLs.doComplete(document, position, jsonDocument);
            });
        },
        resolveCompletionItem(item) {
            return jsonLs.doResolve(item);
        },
        provideDefinition(document, position) {
            return worker(document, async (jsonDocument) => {
                return await jsonLs.findDefinition(document, position, jsonDocument);
            });
        },
        provideSyntacticDiagnostics(document) {
            return worker(document, async (jsonDocument) => {
                const documentLanguageSettings = undefined; // await getSettings(); // TODO
                return await jsonLs.doValidation(document, jsonDocument, documentLanguageSettings, undefined);
            });
        },
        provideHover(document, position) {
            return worker(document, async (jsonDocument) => {
                return await jsonLs.doHover(document, position, jsonDocument);
            });
        },
        provideDocumentLinks(document) {
            return worker(document, async (jsonDocument) => {
                return await jsonLs.findLinks(document, jsonDocument);
            });
        },
        provideDocumentSymbols(document) {
            return worker(document, async (jsonDocument) => {
                return await jsonLs.findDocumentSymbols2(document, jsonDocument);
            });
        },
        provideDocumentColors(document) {
            return worker(document, async (jsonDocument) => {
                return await jsonLs.findDocumentColors(document, jsonDocument);
            });
        },
        provideColorPresentations(document, color, range) {
            return worker(document, async (jsonDocument) => {
                return await jsonLs.getColorPresentations(document, jsonDocument, color, range);
            });
        },
        provideFoldingRanges(document) {
            return worker(document, async () => {
                return await jsonLs.getFoldingRanges(document);
            });
        },
        provideSelectionRanges(document, positions) {
            return worker(document, async (jsonDocument) => {
                return await jsonLs.getSelectionRanges(document, positions, jsonDocument);
            });
        },
        provideDocumentFormattingEdits(document, range, options) {
            return worker(document, async () => {
                const options_2 = await context.configurationHost?.getConfiguration('json.format');
                if (!(options_2?.enable ?? true)) {
                    return;
                }
                return jsonLs.format(document, range, {
                    ...options_2,
                    ...options,
                });
            });
        },
    };
    function worker(document, callback) {
        const jsonDocument = getJsonDocument(document);
        if (!jsonDocument)
            return;
        return callback(jsonDocument);
    }
    function getJsonDocument(textDocument) {
        if (textDocument.languageId !== 'json' && textDocument.languageId !== 'jsonc')
            return;
        const cache = jsonDocuments.get(textDocument);
        if (cache) {
            const [cacheVersion, cacheDoc] = cache;
            if (cacheVersion === textDocument.version) {
                return cacheDoc;
            }
        }
        const doc = jsonLs.parseJSONDocument(textDocument);
        jsonDocuments.set(textDocument, [textDocument.version, doc]);
        return doc;
    }
};
//# sourceMappingURL=index.js.map