"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LinkedEditingRangeAdapter = void 0;
const tslib_1 = require("tslib");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const typeConvert = tslib_1.__importStar(require("../../../../common/vscode/converter"));
const { coalesce } = ide_core_common_1.arrays;
class LinkedEditingRangeAdapter {
    constructor(_documents, _provider) {
        this._documents = _documents;
        this._provider = _provider;
    }
    provideLinkedEditingRanges(resource, position, token) {
        const doc = this._documents.getDocument(resource);
        if (!doc) {
            return Promise.reject(new Error(`There is no document for ${resource}`));
        }
        const pos = typeConvert.Position.to(position);
        return (0, ide_core_common_1.asPromise)(() => this._provider.provideLinkedEditingRanges(doc, pos, token)).then((value) => {
            if (value && Array.isArray(value.ranges)) {
                return {
                    ranges: coalesce(value.ranges.map(typeConvert.Range.from)),
                    wordPattern: value.wordPattern,
                };
            }
            return undefined;
        });
    }
}
exports.LinkedEditingRangeAdapter = LinkedEditingRangeAdapter;
//# sourceMappingURL=linked-editing-range.js.map