"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocumentRangeSemanticTokensProviderImpl = exports.DocumentSemanticTokensProvider = void 0;
const semantic_tokens_1 = require("../../../../common/vscode/semantic-tokens");
function toUint8Array(arr) {
    if (arr instanceof Uint8Array) {
        return arr;
    }
    if (Array.isArray(arr)) {
        return Uint8Array.from(arr);
    }
    const newArr = [];
    for (const k in arr) {
        if (Object.prototype.hasOwnProperty.call(arr, k)) {
            newArr.push(Number(arr[k]));
        }
    }
    return Uint8Array.from(newArr);
}
class DocumentSemanticTokensProvider {
    constructor(proxy, handleId, legend) {
        this.proxy = proxy;
        this.handleId = handleId;
        this.legend = legend;
    }
    getLegend() {
        return this.legend;
    }
    async provideDocumentSemanticTokens(model, lastResultId, token) {
        const nLastResultId = lastResultId ? parseInt(lastResultId, 10) : 0;
        const encodedDto = await this.proxy.$provideDocumentSemanticTokens(this.handleId, model.uri, nLastResultId, token);
        if (!encodedDto) {
            return null;
        }
        if (token.isCancellationRequested) {
            return null;
        }
        // 这里经过通信层的 JSON.stringify , 实际 Uint8Array 会被转换为对象，需要再转回来
        const dto = (0, semantic_tokens_1.decodeSemanticTokensDto)(toUint8Array(encodedDto));
        if (dto.type === 'full') {
            return {
                resultId: String(dto.id),
                data: dto.data,
            };
        }
        return {
            resultId: String(dto.id),
            edits: dto.deltas,
        };
    }
    releaseDocumentSemanticTokens(resultId) {
        if (resultId) {
            this.proxy.$releaseDocumentSemanticTokens(this.handleId, parseInt(resultId, 10));
        }
    }
}
exports.DocumentSemanticTokensProvider = DocumentSemanticTokensProvider;
class DocumentRangeSemanticTokensProviderImpl {
    constructor(proxy, handleId, legend) {
        this.proxy = proxy;
        this.handleId = handleId;
        this.legend = legend;
    }
    getLegend() {
        return this.legend;
    }
    async provideDocumentRangeSemanticTokens(model, range, token) {
        const encodedDto = await this.proxy.$provideDocumentRangeSemanticTokens(this.handleId, model.uri, range, token);
        if (!encodedDto) {
            return null;
        }
        if (token.isCancellationRequested) {
            return null;
        }
        const dto = (0, semantic_tokens_1.decodeSemanticTokensDto)(toUint8Array(encodedDto));
        if (dto.type === 'full') {
            return {
                resultId: String(dto.id),
                data: dto.data,
            };
        }
        throw new Error('Unexpected');
    }
}
exports.DocumentRangeSemanticTokensProviderImpl = DocumentRangeSemanticTokensProviderImpl;
//# sourceMappingURL=semantic-token-provider.js.map