
import { __decorate, __param } from '../../../../../../../../external/tslib/tslib.es6.js';
import { Disposable } from '../../../../../base/common/lifecycle.js';
import '../../../../../base/common/arrays.js';
import '../../../../../base/common/event.js';
import { autorunWithStore } from '../../../../../base/common/observableInternal/autorun.js';
import '../../../../../base/common/observableInternal/derived.js';
import '../../../../../base/common/cancellation.js';
import { observableSignalFromEvent } from '../../../../../base/common/observableInternal/utils.js';
import { ICommandService } from '../../../../../platform/commands/common/commands.service.js';
import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';
import { InlineEditTriggerKind } from '../../../../common/languages.js';
import { ILanguageFeaturesService } from '../../../../common/services/languageFeatures.js';

var InlineEditsAdapterContribution_1;
let InlineEditsAdapterContribution = class InlineEditsAdapterContribution extends Disposable {
    static { InlineEditsAdapterContribution_1 = this; }
    static { this.ID = 'editor.contrib.inlineEditsAdapter'; }
    static { this.isFirst = true; }
    constructor(_editor, instantiationService) {
        super();
        this.instantiationService = instantiationService;
        if (InlineEditsAdapterContribution_1.isFirst) {
            InlineEditsAdapterContribution_1.isFirst = false;
            this.instantiationService.createInstance(InlineEditsAdapter);
        }
    }
};
InlineEditsAdapterContribution = InlineEditsAdapterContribution_1 = ( __decorate([
    ( __param(1, IInstantiationService))
], InlineEditsAdapterContribution));
let InlineEditsAdapter = class InlineEditsAdapter extends Disposable {
    constructor(_languageFeaturesService, _commandService) {
        super();
        this._languageFeaturesService = _languageFeaturesService;
        this._commandService = _commandService;
        const didChangeSignal = observableSignalFromEvent('didChangeSignal', this._languageFeaturesService.inlineEditProvider.onDidChange);
        this._register(autorunWithStore((reader, store) => {
            didChangeSignal.read(reader);
            store.add(this._languageFeaturesService.inlineCompletionsProvider.register('*', {
                async provideInlineCompletions(model, position, context, token) {
                    if (!context.includeInlineEdits) {
                        return undefined;
                    }
                    const allInlineEditProvider = _languageFeaturesService.inlineEditProvider.all(model);
                    const inlineEdits = await Promise.all(( allInlineEditProvider.map(async (provider) => {
                        const result = await provider.provideInlineEdit(model, {
                            triggerKind: InlineEditTriggerKind.Automatic,
                        }, token);
                        if (!result) {
                            return undefined;
                        }
                        return { result, provider };
                    })));
                    const definedEdits = inlineEdits.filter(e => !!e);
                    return {
                        edits: definedEdits,
                        items: ( definedEdits.map(e => {
                            return {
                                range: e.result.range,
                                insertText: e.result.text,
                                command: e.result.accepted,
                                shownCommand: e.result.shown,
                                isInlineEdit: true,
                                edit: e.result,
                            };
                        })),
                        commands: definedEdits.flatMap(e => e.result.commands ?? []),
                    };
                },
                handleRejection: (completions, item) => {
                    if (item.edit.rejected) {
                        this._commandService.executeCommand(item.edit.rejected.id, ...(item.edit.rejected.arguments ?? []));
                    }
                },
                freeInlineCompletions(c) {
                    for (const e of c.edits) {
                        e.provider.freeInlineEdit(e.result);
                    }
                },
                toString() {
                    return 'InlineEditsAdapter';
                }
            }));
        }));
    }
};
InlineEditsAdapter = ( __decorate([
    ( __param(0, ILanguageFeaturesService)),
    ( __param(1, ICommandService))
], InlineEditsAdapter));

export { InlineEditsAdapter, InlineEditsAdapterContribution };
