/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { UriList } from '../../../../base/common/dataTransfer.js';
import { Mimes } from '../../../../base/common/mime.js';
import { Schemas } from '../../../../base/common/network.js';
import { relativePath } from '../../../../base/common/resources.js';
import { URI } from '../../../../base/common/uri.js';
import { localize } from '../../../../nls.js';
import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js';
class DefaultTextDropProvider {
    constructor() {
        this.dropMimeTypes = [Mimes.text, 'text'];
    }
    provideDocumentOnDropEdits(_model, _position, dataTransfer, _token) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const textEntry = (_a = dataTransfer.get('text')) !== null && _a !== void 0 ? _a : dataTransfer.get(Mimes.text);
            if (textEntry) {
                const text = yield textEntry.asString();
                return {
                    label: localize('defaultDropProvider.text.label', "Drop as plain text"),
                    insertText: text
                };
            }
            return undefined;
        });
    }
}
let DefaultUriListDropProvider = class DefaultUriListDropProvider {
    constructor(_workspaceContextService) {
        this._workspaceContextService = _workspaceContextService;
        this.dropMimeTypes = [Mimes.uriList];
    }
    provideDocumentOnDropEdits(_model, _position, dataTransfer, _token) {
        return __awaiter(this, void 0, void 0, function* () {
            const urlListEntry = dataTransfer.get(Mimes.uriList);
            if (urlListEntry) {
                const urlList = yield urlListEntry.asString();
                const entry = this.getUriListInsertText(urlList);
                if (entry) {
                    return {
                        label: entry.count > 1
                            ? localize('defaultDropProvider.uri.label', "Drop as uri")
                            : localize('defaultDropProvider.uriList.label', "Drop as uri list"),
                        insertText: entry.snippet
                    };
                }
            }
            return undefined;
        });
    }
    getUriListInsertText(strUriList) {
        const entries = [];
        for (const entry of UriList.parse(strUriList)) {
            try {
                entries.push({ uri: URI.parse(entry), originalText: entry });
            }
            catch (_a) {
                // noop
            }
        }
        if (!entries.length) {
            return;
        }
        const snippet = entries
            .map(({ uri, originalText }) => {
            const root = this._workspaceContextService.getWorkspaceFolder(uri);
            if (root) {
                const rel = relativePath(root.uri, uri);
                if (rel) {
                    return rel;
                }
            }
            return uri.scheme === Schemas.file ? uri.fsPath : originalText;
        })
            .join(' ');
        return { snippet, count: entries.length };
    }
};
DefaultUriListDropProvider = __decorate([
    __param(0, IWorkspaceContextService)
], DefaultUriListDropProvider);
let registeredDefaultProviders = false;
export function registerDefaultDropProviders(languageFeaturesService, workspaceContextService) {
    if (!registeredDefaultProviders) {
        registeredDefaultProviders = true;
        languageFeaturesService.documentOnDropEditProvider.register('*', new DefaultTextDropProvider());
        languageFeaturesService.documentOnDropEditProvider.register('*', new DefaultUriListDropProvider(workspaceContextService));
    }
}
