"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RenameAdapter = void 0;
const tslib_1 = require("tslib");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const Converter = tslib_1.__importStar(require("../../../../common/vscode/converter"));
const types = tslib_1.__importStar(require("../../../../common/vscode/ext-types"));
const utils_1 = require("../../../../common/vscode/utils");
class RenameAdapter {
    static supportsResolving(provider) {
        return typeof provider.prepareRename === 'function';
    }
    constructor(provider, documents) {
        this.provider = provider;
        this.documents = documents;
        this.debug = (0, ide_core_common_1.getDebugLogger)();
    }
    provideRenameEdits(resource, position, newName, token) {
        const document = this.documents.getDocumentData(resource);
        if (!document) {
            return Promise.reject(new Error(`There is no document for ${resource}`));
        }
        const doc = document.document;
        const pos = Converter.toPosition(position);
        return Promise.resolve(this.provider.provideRenameEdits(doc, pos, newName, token)).then((value) => {
            if (!value) {
                return undefined;
            }
            return Converter.WorkspaceEdit.from(value);
        }, (error) => {
            const rejectReason = RenameAdapter.asMessage(error);
            if (rejectReason) {
                return {
                    rejectReason,
                    edits: [],
                };
            }
            else {
                return Promise.reject(error);
            }
        });
    }
    resolveRenameLocation(resource, position, token) {
        if (typeof this.provider.prepareRename !== 'function') {
            return Promise.resolve(undefined);
        }
        const document = this.documents.getDocumentData(resource);
        if (!document) {
            return Promise.reject(new Error(`There is no document for ${resource}`));
        }
        const doc = document.document;
        const pos = Converter.toPosition(position);
        return Promise.resolve(this.provider.prepareRename(doc, pos, token)).then((rangeOrLocation) => {
            let range;
            let text;
            if (rangeOrLocation && types.Range.isRange(rangeOrLocation)) {
                range = rangeOrLocation;
                text = doc.getText(rangeOrLocation);
            }
            else if (rangeOrLocation && (0, utils_1.isObject)(rangeOrLocation)) {
                range = rangeOrLocation.range;
                text = rangeOrLocation.placeholder;
            }
            if (!range || !text) {
                return undefined;
            }
            if (range.start.line > pos.line || range.end.line < pos.line) {
                this.debug.warn('INVALID rename location: position line must be within range start/end lines');
                return undefined;
            }
            return {
                range: Converter.fromRange(range),
                text,
            };
        }, (error) => {
            const rejectReason = RenameAdapter.asMessage(error);
            if (rejectReason) {
                return Promise.resolve({
                    rejectReason,
                });
            }
            else {
                return Promise.reject(error);
            }
        });
    }
    static asMessage(err) {
        if (typeof err === 'string') {
            return err;
        }
        else if (err instanceof Error && typeof err.message === 'string') {
            return err.message;
        }
        else {
            return undefined;
        }
    }
}
exports.RenameAdapter = RenameAdapter;
//# sourceMappingURL=rename.js.map