"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocumentHighlightAdapter = void 0;
const tslib_1 = require("tslib");
const Converter = tslib_1.__importStar(require("../../../../common/vscode/converter"));
class DocumentHighlightAdapter {
    constructor(provider, documents) {
        this.provider = provider;
        this.documents = documents;
    }
    provideDocumentHighlights(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.provideDocumentHighlights(document, zeroBasedPosition, token)).then((documentHighlights) => {
            if (!documentHighlights) {
                return undefined;
            }
            if (this.isDocumentHighlightArray(documentHighlights)) {
                const highlights = [];
                for (const highlight of documentHighlights) {
                    highlights.push(Converter.DocumentHighlight.from(highlight));
                }
                return highlights;
            }
        });
    }
    isDocumentHighlightArray(array) {
        return Array.isArray(array) && array.length > 0 && array[0].range;
    }
}
exports.DocumentHighlightAdapter = DocumentHighlightAdapter;
//# sourceMappingURL=document-highlight.js.map