"use strict";
/* ---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataTransferCache = void 0;
const ide_core_common_1 = require("@opensumi/ide-core-common");
// Modified from https://github.com/microsoft/vscode/blob/f86ceb6749721ca068f0407914521ad11ca704a8/src/vs/workbench/api/common/shared/dataTransferCache.ts
class DataTransferCache {
    constructor() {
        this.requestIdPool = 0;
        this.dataTransfers = new Map();
    }
    add(dataTransfer) {
        const requestId = this.requestIdPool++;
        this.dataTransfers.set(requestId, [...dataTransfer.values()]);
        return {
            id: requestId,
            dispose: () => {
                this.dataTransfers.delete(requestId);
            },
        };
    }
    async resolveDropFileData(requestId, dataItemId) {
        const entry = this.dataTransfers.get(requestId);
        if (!entry) {
            throw new Error('No data transfer found');
        }
        const item = entry.find((x) => x.id === dataItemId);
        if (!item) {
            throw new Error('No item found in data transfer');
        }
        const file = item.asFile();
        if (!file) {
            throw new Error('Found data transfer item is not a file');
        }
        return ide_core_common_1.BinaryBuffer.wrap(await file.data());
    }
    dispose() {
        this.dataTransfers.clear();
    }
}
exports.DataTransferCache = DataTransferCache;
//# sourceMappingURL=data-transfer-cache.js.map