"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InlineEditAdapter = 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 ext_types_1 = require("../../../../common/vscode/ext-types");
class InlineEditAdapter {
    async provideInlineEdits(uri, context, token) {
        var _a;
        const doc = this._documents.getDocument(uri);
        const result = await this._provider.provideInlineEdit(doc, {
            triggerKind: this.languageTriggerKindToVSCodeTriggerKind[context.triggerKind],
            requestUuid: context.requestUuid,
        }, token);
        if (!result) {
            // undefined and null are valid results
            return undefined;
        }
        if (token.isCancellationRequested) {
            // cancelled -> return without further ado, esp no caching
            // of results as they will leak
            return undefined;
        }
        let disposableStore;
        const pid = this._references.createReferenceId({
            dispose() {
                disposableStore === null || disposableStore === void 0 ? void 0 : disposableStore.dispose();
            },
            item: result,
        });
        let acceptCommand;
        if (result.accepted) {
            if (!disposableStore) {
                disposableStore = new ide_core_common_1.DisposableStore();
            }
            acceptCommand = this._commands.toInternal(result.accepted, disposableStore);
        }
        let rejectCommand;
        if (result.rejected) {
            if (!disposableStore) {
                disposableStore = new ide_core_common_1.DisposableStore();
            }
            rejectCommand = this._commands.toInternal(result.rejected, disposableStore);
        }
        let shownCommand;
        if (result.shown) {
            if (!disposableStore) {
                disposableStore = new ide_core_common_1.DisposableStore();
            }
            shownCommand = this._commands.toInternal(result.shown, disposableStore);
        }
        if (!disposableStore) {
            disposableStore = new ide_core_common_1.DisposableStore();
        }
        const langResult = {
            pid,
            text: result.text,
            range: typeConvert.Range.from(result.range),
            showRange: typeConvert.Range.from(result.showRange),
            accepted: acceptCommand,
            rejected: rejectCommand,
            shown: shownCommand,
            commands: (_a = result.commands) === null || _a === void 0 ? void 0 : _a.map((c) => this._commands.toInternal(c, disposableStore)),
        };
        return langResult;
    }
    disposeEdit(pid) {
        const data = this._references.disposeReferenceId(pid);
        data === null || data === void 0 ? void 0 : data.dispose();
    }
    constructor(_extension, _documents, _provider, _commands) {
        this._documents = _documents;
        this._provider = _provider;
        this._commands = _commands;
        this._references = new ReferenceMap();
        this.languageTriggerKindToVSCodeTriggerKind = {
            [ext_types_1.InlineEditTriggerKind.Automatic]: ext_types_1.InlineEditTriggerKind.Automatic,
            [ext_types_1.InlineEditTriggerKind.Invoke]: ext_types_1.InlineEditTriggerKind.Invoke,
        };
    }
}
exports.InlineEditAdapter = InlineEditAdapter;
class ReferenceMap {
    constructor() {
        this._references = new Map();
        this._idPool = 1;
    }
    createReferenceId(value) {
        const id = this._idPool++;
        this._references.set(id, value);
        return id;
    }
    disposeReferenceId(referenceId) {
        const value = this._references.get(referenceId);
        this._references.delete(referenceId);
        return value;
    }
    get(referenceId) {
        return this._references.get(referenceId);
    }
}
//# sourceMappingURL=inlineEdit.js.map