"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeclarationAdapter = void 0;
const tslib_1 = require("tslib");
const Converter = tslib_1.__importStar(require("../../../../common/vscode/converter"));
const types = tslib_1.__importStar(require("../../../../common/vscode/ext-types"));
const util_1 = require("./util");
class DeclarationAdapter {
    constructor(provider, documents) {
        this.provider = provider;
        this.documents = documents;
    }
    provideDeclaration(resource, position, token) {
        const documentData = this.documents.getDocumentData(resource);
        if (!documentData) {
            return Promise.reject(new Error(`There is no document for ${resource}`));
        }
        const document = documentData.document;
        const zeroBasedPosition = Converter.toPosition(position);
        return Promise.resolve(this.provider.provideDeclaration(document, zeroBasedPosition, token)).then((definition) => {
            if (!definition) {
                return undefined;
            }
            if (definition instanceof types.Location) {
                return Converter.fromLocation(definition);
            }
            if ((0, util_1.isLocationArray)(definition)) {
                const locations = [];
                for (const location of definition) {
                    locations.push(Converter.fromLocation(location));
                }
                return locations;
            }
            if ((0, util_1.isDefinitionLinkArray)(definition)) {
                const definitionLinks = [];
                for (const definitionLink of definition) {
                    definitionLinks.push(Converter.DefinitionLink.from(definitionLink));
                }
                return definitionLinks;
            }
        });
    }
}
exports.DeclarationAdapter = DeclarationAdapter;
//# sourceMappingURL=declaration.js.map