"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SignatureHelpAdapter = void 0;
const tslib_1 = require("tslib");
const typeConvert = tslib_1.__importStar(require("../../../../common/vscode/converter"));
class SignatureHelpAdapter {
    constructor(delegate, documents) {
        this.delegate = delegate;
        this.documents = documents;
        this.cacheId = 0;
        this.cache = new Map();
    }
    async provideSignatureHelp(resource, position, token, context) {
        const documentData = this.documents.getDocumentData(resource);
        if (!documentData) {
            return Promise.reject(new Error(`There are no document for  ${resource}`));
        }
        const cacheId = this.cacheId++;
        const document = documentData.document;
        const zeroBasedPosition = typeConvert.toPosition(position);
        const vscodeContext = this.reviveContext(context);
        const value = await this.delegate.provideSignatureHelp(document, zeroBasedPosition, token, vscodeContext);
        if (value) {
            this.cache.set(cacheId, value);
            return {
                ...typeConvert.SignatureHelp.from(value),
                id: cacheId,
            };
        }
    }
    reviveContext(context) {
        let activeSignatureHelp;
        if (context.activeSignatureHelp) {
            const revivedSignatureHelp = typeConvert.SignatureHelp.to(context.activeSignatureHelp);
            const saved = this.cache.get(context.activeSignatureHelp.id);
            if (saved) {
                activeSignatureHelp = saved;
                activeSignatureHelp.activeSignature = revivedSignatureHelp.activeSignature;
                activeSignatureHelp.activeParameter = revivedSignatureHelp.activeParameter;
            }
            else {
                activeSignatureHelp = revivedSignatureHelp;
            }
        }
        return { ...context, activeSignatureHelp };
    }
    releaseSignatureHelp(id) {
        this.cache.delete(id);
    }
}
exports.SignatureHelpAdapter = SignatureHelpAdapter;
//# sourceMappingURL=signature.js.map