"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.LocalSelectionTransfer = exports.DraggedTreeItemsIdentifier = exports.DataTransfers = exports.VSDataTransfer = exports.CodeDataTransfers = void 0;
exports.createStringDataTransferItem = createStringDataTransferItem;
exports.createFileDataTransferItem = createFileDataTransferItem;
exports.createFileDataTransferItemFromFile = createFileDataTransferItemFromFile;
exports.toVSDataTransfer = toVSDataTransfer;
// Modified from https://github.com/microsoft/vscode/blob/f86ceb6749721ca068f0407914521ad11ca704a8/src/vs/base/common/dataTransfer.ts
const ide_core_common_1 = require("@opensumi/ide-core-common");
exports.CodeDataTransfers = {
    EDITORS: 'CodeEditors',
    FILES: 'CodeFiles',
};
function createStringDataTransferItem(stringOrPromise) {
    return {
        id: (0, ide_core_common_1.uuid)(),
        asString: async () => stringOrPromise,
        asFile: () => undefined,
        value: typeof stringOrPromise === 'string' ? stringOrPromise : undefined,
    };
}
function createFileDataTransferItem(fileName, uri, data) {
    return {
        id: (0, ide_core_common_1.uuid)(),
        asString: async () => '',
        asFile: () => ({ name: fileName, uri, data }),
        value: undefined,
    };
}
class VSDataTransfer {
    constructor() {
        this._entries = new Map();
    }
    get size() {
        return this._entries.size;
    }
    has(mimeType) {
        return this._entries.has(this.toKey(mimeType));
    }
    get(mimeType) {
        var _a;
        return (_a = this._entries.get(this.toKey(mimeType))) === null || _a === void 0 ? void 0 : _a[0];
    }
    append(mimeType, value) {
        const existing = this._entries.get(mimeType);
        if (existing) {
            existing.push(value);
        }
        else {
            this._entries.set(this.toKey(mimeType), [value]);
        }
    }
    replace(mimeType, value) {
        this._entries.set(this.toKey(mimeType), [value]);
    }
    delete(mimeType) {
        this._entries.delete(this.toKey(mimeType));
    }
    *entries() {
        for (const [mine, items] of this._entries.entries()) {
            for (const item of items) {
                yield [mine, item];
            }
        }
    }
    values() {
        return Array.from(this._entries.values()).flat();
    }
    forEach(f) {
        for (const [mime, item] of this.entries()) {
            f(item, mime);
        }
    }
    toKey(mimeType) {
        return mimeType.toLowerCase();
    }
}
exports.VSDataTransfer = VSDataTransfer;
exports.DataTransfers = {
    /**
     * Application specific resource transfer type
     */
    RESOURCES: 'ResourceURLs',
    /**
     * Browser specific transfer type to download
     */
    DOWNLOAD_URL: 'DownloadURL',
    /**
     * Browser specific transfer type for files
     */
    FILES: 'Files',
    /**
     * Typically transfer type for copy/paste transfers.
     */
    TEXT: ide_core_common_1.Mimes.text,
};
class DraggedTreeItemsIdentifier {
    constructor(identifier) {
        this.identifier = identifier;
    }
}
exports.DraggedTreeItemsIdentifier = DraggedTreeItemsIdentifier;
/**
 * A singleton to store transfer data during drag & drop operations that are only valid within the application.
 */
class LocalSelectionTransfer {
    constructor() {
        // protect against external instantiation
    }
    static getInstance() {
        return LocalSelectionTransfer.INSTANCE;
    }
    hasData(proto) {
        return proto && proto === this.proto;
    }
    clearData(proto) {
        if (this.hasData(proto)) {
            this.proto = undefined;
            this.data = undefined;
        }
    }
    getData(proto) {
        if (this.hasData(proto)) {
            return this.data;
        }
        return undefined;
    }
    setData(data, proto) {
        if (proto) {
            this.data = data;
            this.proto = proto;
        }
    }
}
exports.LocalSelectionTransfer = LocalSelectionTransfer;
LocalSelectionTransfer.INSTANCE = new LocalSelectionTransfer();
function createFileDataTransferItemFromFile(file) {
    const uri = file.path
        ? ide_core_common_1.Uri.parse(file.path)
        : undefined;
    return createFileDataTransferItem(file.name, uri, async () => new Uint8Array(await file.arrayBuffer()));
}
function toVSDataTransfer(dataTransfer) {
    const vsDataTransfer = new VSDataTransfer();
    for (const item of dataTransfer.items) {
        const type = item.type;
        if (item.kind === 'string') {
            const asStringValue = new Promise((resolve) => item.getAsString(resolve));
            vsDataTransfer.append(type, createStringDataTransferItem(asStringValue));
        }
        else if (item.kind === 'file') {
            const file = item.getAsFile();
            if (file) {
                vsDataTransfer.append(type, createFileDataTransferItemFromFile(file));
            }
        }
    }
    return vsDataTransfer;
}
//# sourceMappingURL=data-transfer.js.map