"use strict";
/** ******************************************************************************
 * Copyright (C) 2018 Red Hat, Inc. and others.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the Eclipse
 * Public License v. 2.0 are satisfied: GNU General Public License, version 2
 * with the GNU Classpath Exception which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 ********************************************************************************/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeLensAdapter = void 0;
const tslib_1 = require("tslib");
// Some code copied and modified from https://github.com/eclipse-theia/theia/tree/v1.14.0/packages/plugin-ext/src/plugin/languages/lens.ts
const ide_core_common_1 = require("@opensumi/ide-core-common");
const Converter = tslib_1.__importStar(require("../../../../common/vscode/converter"));
/** Adapts the calls from main to extension thread for providing/resolving the code lenses. */
class CodeLensAdapter {
    constructor(provider, documents, commandConverter) {
        this.provider = provider;
        this.documents = documents;
        this.commandConverter = commandConverter;
        this.cache = new ide_core_common_1.Cache('CodeLens');
        this.disposableStore = new Map();
    }
    async provideCodeLenses(resource, token) {
        const doc = this.documents.getDocumentData(resource.toString());
        if (!doc) {
            return Promise.reject(new Error(`There is no document for ${resource}`));
        }
        const lenses = await this.provider.provideCodeLenses(doc.document, token);
        if (!lenses || token.isCancellationRequested) {
            return undefined;
        }
        const cacheId = this.cache.add(lenses);
        const disposables = new ide_core_common_1.DisposableStore();
        this.disposableStore.set(cacheId, disposables);
        const result = {
            cacheId,
            lenses: [],
        };
        for (let i = 0; i < lenses.length; i++) {
            result.lenses.push({
                cacheId: [cacheId, i],
                range: Converter.fromRange(lenses[i].range),
                command: this.commandConverter.toInternal(lenses[i].command, disposables),
            });
        }
        return result;
    }
    async resolveCodeLens(symbol, token) {
        const lens = symbol.cacheId && this.cache.get(...symbol.cacheId);
        if (!lens) {
            return Promise.resolve(undefined);
        }
        let resolvedLens;
        if (typeof this.provider.resolveCodeLens !== 'function' || lens.isResolved) {
            resolvedLens = lens;
        }
        else {
            resolvedLens = (await this.provider.resolveCodeLens(lens, token));
        }
        if (!resolvedLens) {
            resolvedLens = lens;
        }
        if (token.isCancellationRequested) {
            return undefined;
        }
        const disposables = new ide_core_common_1.DisposableStore();
        symbol.command = this.commandConverter.toInternal(resolvedLens.command ? resolvedLens.command : CodeLensAdapter.BAD_CMD, disposables);
        return symbol;
    }
    releaseCodeLens(cacheId) {
        var _a;
        (_a = this.disposableStore.get(cacheId)) === null || _a === void 0 ? void 0 : _a.dispose();
        this.disposableStore.delete(cacheId);
        this.cache.delete(cacheId);
    }
}
exports.CodeLensAdapter = CodeLensAdapter;
CodeLensAdapter.BAD_CMD = { command: 'missing', title: '<<MISSING COMMAND>>' };
//# sourceMappingURL=lens.js.map