import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { coalesce } from '../../../../base/common/arrays.js';
import { createCancelablePromise, raceCancellation } from '../../../../base/common/async.js';
import { matchesMimeType, VSDataTransfer } from '../../../../base/common/dataTransfer.js';
import { HierarchicalKind } from '../../../../base/common/hierarchicalKind.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { toExternalVSDataTransfer } from '../../../browser/dnd.js';
import { Range } from '../../../common/core/range.js';
import { ILanguageFeaturesService } from '../../../common/services/languageFeatures.js';
import { DraggedTreeItemsIdentifier } from '../../../common/services/treeViewsDnd.js';
import { ITreeViewsDnDService } from '../../../common/services/treeViewsDndService.js';
import { EditorStateCancellationTokenSource } from '../../editorState/browser/editorState.js';
import { InlineProgressManager } from '../../inlineProgress/browser/inlineProgress.js';
import { localizeWithPath } from '../../../../nls.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
import { LocalSelectionTransfer } from '../../../../platform/dnd/browser/dnd.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { sortEditsByYieldTo } from './edit.js';
import { PostEditWidgetManager } from './postEditWidget.js';
var DropIntoEditorController_1;
const _moduleId = "vs/editor/contrib/dropOrPasteInto/browser/dropIntoEditorController";
const defaultProviderConfig = 'editor.experimental.dropIntoEditor.defaultProvider';
const changeDropTypeCommandId = 'editor.changeDropType';
const dropWidgetVisibleCtx = ( (new RawContextKey('dropWidgetVisible', false, ( localizeWithPath(_moduleId, 0, "Whether the drop widget is showing")))));
let DropIntoEditorController = class DropIntoEditorController extends Disposable {
    static { DropIntoEditorController_1 = this; }
    static { this.ID = 'editor.contrib.dropIntoEditorController'; }
    static get(editor) {
        return editor.getContribution(DropIntoEditorController_1.ID);
    }
    constructor(editor, instantiationService, _configService, _languageFeaturesService, _treeViewsDragAndDropService) {
        super();
        this._configService = _configService;
        this._languageFeaturesService = _languageFeaturesService;
        this._treeViewsDragAndDropService = _treeViewsDragAndDropService;
        this.treeItemsTransfer = LocalSelectionTransfer.getInstance();
        this._dropProgressManager = this._register(instantiationService.createInstance(InlineProgressManager, 'dropIntoEditor', editor));
        this._postDropWidgetManager = this._register(instantiationService.createInstance(PostEditWidgetManager, 'dropIntoEditor', editor, dropWidgetVisibleCtx, { id: changeDropTypeCommandId, label: ( localizeWithPath(_moduleId, 1, "Show drop options...")) }));
        this._register(editor.onDropIntoEditor(e => this.onDropIntoEditor(editor, e.position, e.event)));
    }
    clearWidgets() {
        this._postDropWidgetManager.clear();
    }
    changeDropType() {
        this._postDropWidgetManager.tryShowSelector();
    }
    async onDropIntoEditor(editor, position, dragEvent) {
        if (!dragEvent.dataTransfer || !editor.hasModel()) {
            return;
        }
        this._currentOperation?.cancel();
        editor.focus();
        editor.setPosition(position);
        const p = createCancelablePromise(async (token) => {
            const tokenSource = ( (new EditorStateCancellationTokenSource(editor, 1 , undefined, token)));
            try {
                const ourDataTransfer = await this.extractDataTransferData(dragEvent);
                if (ourDataTransfer.size === 0 || tokenSource.token.isCancellationRequested) {
                    return;
                }
                const model = editor.getModel();
                if (!model) {
                    return;
                }
                const providers = this._languageFeaturesService.documentOnDropEditProvider
                    .ordered(model)
                    .filter(provider => {
                    if (!provider.dropMimeTypes) {
                        return true;
                    }
                    return (
                         (provider.dropMimeTypes.some(mime => ourDataTransfer.matches(mime)))
                    );
                });
                const edits = await this.getDropEdits(providers, model, position, ourDataTransfer, tokenSource);
                if (tokenSource.token.isCancellationRequested) {
                    return;
                }
                if (edits.length) {
                    const activeEditIndex = this.getInitialActiveEditIndex(model, edits);
                    const canShowWidget = editor.getOption(36 ).showDropSelector === 'afterDrop';
                    await this._postDropWidgetManager.applyEditAndShowIfNeeded([Range.fromPositions(position)], { activeEditIndex, allEdits: edits }, canShowWidget, async (edit) => edit, token);
                }
            }
            finally {
                tokenSource.dispose();
                if (this._currentOperation === p) {
                    this._currentOperation = undefined;
                }
            }
        });
        this._dropProgressManager.showWhile(position, ( localizeWithPath(_moduleId, 2, "Running drop handlers. Click to cancel")), p);
        this._currentOperation = p;
    }
    async getDropEdits(providers, model, position, dataTransfer, tokenSource) {
        const results = await raceCancellation(Promise.all(( (providers.map(async (provider) => {
            try {
                const edits = await provider.provideDocumentOnDropEdits(model, position, dataTransfer, tokenSource.token);
                return edits?.map(edit => ({ ...edit, providerId: provider.id }));
            }
            catch (err) {
                console.error(err);
            }
            return undefined;
        })))), tokenSource.token);
        const edits = coalesce(results ?? []).flat();
        return sortEditsByYieldTo(edits);
    }
    getInitialActiveEditIndex(model, edits) {
        const preferredProviders = this._configService.getValue(defaultProviderConfig, { resource: model.uri });
        for (const [configMime, desiredKindStr] of Object.entries(preferredProviders)) {
            const desiredKind = ( (new HierarchicalKind(desiredKindStr)));
            const editIndex = edits.findIndex(edit => desiredKind.value === edit.providerId
                && edit.handledMimeType && matchesMimeType(configMime, [edit.handledMimeType]));
            if (editIndex >= 0) {
                return editIndex;
            }
        }
        return 0;
    }
    async extractDataTransferData(dragEvent) {
        if (!dragEvent.dataTransfer) {
            return (
                 (new VSDataTransfer())
            );
        }
        const dataTransfer = toExternalVSDataTransfer(dragEvent.dataTransfer);
        if (this.treeItemsTransfer.hasData(DraggedTreeItemsIdentifier.prototype)) {
            const data = this.treeItemsTransfer.getData(DraggedTreeItemsIdentifier.prototype);
            if (Array.isArray(data)) {
                for (const id of data) {
                    const treeDataTransfer = await this._treeViewsDragAndDropService.removeDragOperationTransfer(id.identifier);
                    if (treeDataTransfer) {
                        for (const [type, value] of treeDataTransfer) {
                            dataTransfer.replace(type, value);
                        }
                    }
                }
            }
        }
        return dataTransfer;
    }
};
DropIntoEditorController = DropIntoEditorController_1 = ( (__decorate([
    ( (__param(1, IInstantiationService))),
    ( (__param(2, IConfigurationService))),
    ( (__param(3, ILanguageFeaturesService))),
    ( (__param(4, ITreeViewsDnDService)))
], DropIntoEditorController)));
export { DropIntoEditorController, changeDropTypeCommandId, defaultProviderConfig, dropWidgetVisibleCtx };
