"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Red Hat, Inc. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDefinition = void 0;
const vscode_languageserver_protocol_1 = require("vscode-languageserver-protocol");
const yaml_1 = require("yaml");
const yaml_documents_1 = require("../parser/yaml-documents");
const arrUtils_1 = require("../utils/arrUtils");
const textBuffer_1 = require("../utils/textBuffer");
function getDefinition(document, params) {
    try {
        const yamlDocument = yaml_documents_1.yamlDocumentsCache.getYamlDocument(document);
        const offset = document.offsetAt(params.position);
        const currentDoc = arrUtils_1.matchOffsetToDocument(offset, yamlDocument);
        if (currentDoc) {
            const [node] = currentDoc.getNodeFromPosition(offset, new textBuffer_1.TextBuffer(document));
            if (node && yaml_1.isAlias(node)) {
                const defNode = node.resolve(currentDoc.internalDocument);
                if (defNode && defNode.range) {
                    const targetRange = vscode_languageserver_protocol_1.Range.create(document.positionAt(defNode.range[0]), document.positionAt(defNode.range[2]));
                    const selectionRange = vscode_languageserver_protocol_1.Range.create(document.positionAt(defNode.range[0]), document.positionAt(defNode.range[1]));
                    return [vscode_languageserver_protocol_1.LocationLink.create(document.uri, targetRange, selectionRange)];
                }
            }
        }
    }
    catch (err) {
        this.telemetry.sendError('yaml.definition.error', { error: err });
    }
    return undefined;
}
exports.getDefinition = getDefinition;
//# sourceMappingURL=yamlDefinition.js.map