"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.register = void 0;
const vscode = require("vscode-languageserver-protocol");
const featureWorkers_1 = require("../utils/featureWorkers");
const common_1 = require("../utils/common");
function register(context) {
    return async (uri, token = vscode.CancellationToken.None) => {
        const pluginLinks = await (0, featureWorkers_1.documentFeatureWorker)(context, uri, file => !!file.capabilities.documentSymbol, async (plugin, document) => {
            if (token.isCancellationRequested)
                return;
            const links = await plugin.provideDocumentLinks?.(document, token);
            links?.forEach(link => {
                link.data = {
                    uri,
                    original: {
                        data: link.data,
                    },
                    pluginId: Object.keys(context.plugins).find(key => context.plugins[key] === plugin),
                };
            });
            return links;
        }, (links, map) => links.map(link => {
            if (!map)
                return link;
            const range = map.toSourceRange(link.range);
            if (range) {
                return {
                    ...link,
                    range,
                };
            }
        }).filter(common_1.notEmpty), arr => arr.flat()) ?? [];
        const maps = context.documents.getMapsBySourceFileUri(uri);
        const fictitiousLinks = maps ? getFictitiousLinks(context.documents.getDocumentByUri(maps.snapshot, uri), maps.maps) : [];
        return [
            ...pluginLinks,
            ...fictitiousLinks,
        ];
        function getFictitiousLinks(document, maps) {
            const result = [];
            for (const [_, map] of maps) {
                for (const mapped of map.map.mappings) {
                    if (!mapped.data.displayWithLink)
                        continue;
                    if (mapped.sourceRange[0] === mapped.sourceRange[1])
                        continue;
                    result.push({
                        range: {
                            start: document.positionAt(mapped.sourceRange[0]),
                            end: document.positionAt(mapped.sourceRange[1]),
                        },
                        target: uri, // TODO
                    });
                }
            }
            return result;
        }
    };
}
exports.register = register;
//# sourceMappingURL=documentLinks.js.map