
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { Disposable, toDisposable } from '../../../../base/common/lifecycle.js';
import { MultiDiffEditorWidget } from '../../../../editor/browser/widget/multiDiffEditor/multiDiffEditorWidget.js';
import { ITextResourceConfigurationService } from '../../../../editor/common/services/textResourceConfiguration.service.js';
import { MenuId } from '../../../../platform/actions/common/actions.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { IStorageService } from '../../../../platform/storage/common/storage.service.js';
import { ITelemetryService } from '../../../../platform/telemetry/common/telemetry.service.js';
import { IThemeService } from '../../../../platform/theme/common/themeService.service.js';
import { ResourceLabel } from '../../../browser/labels.js';
import { AbstractEditorWithViewState } from '../../../browser/parts/editor/editorWithViewState.js';
import { MultiDiffEditorInput } from './multiDiffEditorInput.js';
import { IEditorGroupsService } from '../../../services/editor/common/editorGroupsService.service.js';
import { IEditorService } from '../../../services/editor/common/editorService.service.js';
import { Range } from '../../../../editor/common/core/range.js';
import { IEditorProgressService } from '../../../../platform/progress/common/progress.service.js';
import '../../../../base/common/observableInternal/index.js';
import { FloatingEditorToolbarWidget } from '../../../../editor/contrib/floatingMenu/browser/floatingMenu.js';
import { observableValue } from '../../../../base/common/observableInternal/observables/observableValue.js';
import { derived } from '../../../../base/common/observableInternal/observables/derived.js';
import { autorun } from '../../../../base/common/observableInternal/reactions/autorun.js';

var MultiDiffEditor_1;
let MultiDiffEditor = class MultiDiffEditor extends AbstractEditorWithViewState {
    static {
        MultiDiffEditor_1 = this;
    }
    static {
        this.ID = "multiDiffEditor";
    }
    get viewModel() {
        return this._viewModel;
    }
    constructor(
        group,
        instantiationService,
        telemetryService,
        themeService,
        storageService,
        editorService,
        editorGroupService,
        textResourceConfigurationService,
        editorProgressService
    ) {
        super(
            MultiDiffEditor_1.ID,
            group,
            "multiDiffEditor",
            telemetryService,
            instantiationService,
            storageService,
            textResourceConfigurationService,
            themeService,
            editorService,
            editorGroupService
        );
        this.editorProgressService = editorProgressService;
        this._multiDiffEditorWidget = undefined;
    }
    createEditor(parent) {
        this._multiDiffEditorWidget = this._register(this.instantiationService.createInstance(
            MultiDiffEditorWidget,
            parent,
            this.instantiationService.createInstance(WorkbenchUIElementFactory)
        ));
        this._register(this._multiDiffEditorWidget.onDidChangeActiveControl(() => {
            this._onDidChangeControl.fire();
        }));
        this._contentOverlay = this._register(( new MultiDiffEditorContentMenuOverlay(
            this._multiDiffEditorWidget.getRootElement(),
            this._multiDiffEditorWidget.getContextKeyService(),
            this._multiDiffEditorWidget.getScopedInstantiationService()
        )));
    }
    async setInput(input, options, context, token) {
        await super.setInput(input, options, context, token);
        this._viewModel = await input.getViewModel();
        this._contentOverlay?.updateResource(input.resource);
        this._multiDiffEditorWidget.setViewModel(this._viewModel);
        const viewState = this.loadEditorViewState(input, context);
        if (viewState) {
            this._multiDiffEditorWidget.setViewState(viewState);
        }
        this._applyOptions(options);
    }
    setOptions(options) {
        this._applyOptions(options);
    }
    _applyOptions(options) {
        const viewState = options?.viewState;
        if (!viewState || !viewState.revealData) {
            return;
        }
        this._multiDiffEditorWidget?.reveal(viewState.revealData.resource, {
            range: viewState.revealData.range ? Range.lift(viewState.revealData.range) : undefined,
            highlight: true
        });
    }
    async clearInput() {
        await super.clearInput();
        this._contentOverlay?.updateResource(undefined);
        this._multiDiffEditorWidget.setViewModel(undefined);
    }
    layout(dimension) {
        this._multiDiffEditorWidget.layout(dimension);
    }
    getControl() {
        return this._multiDiffEditorWidget.getActiveControl();
    }
    focus() {
        super.focus();
        this._multiDiffEditorWidget?.getActiveControl()?.focus();
    }
    hasFocus() {
        return this._multiDiffEditorWidget?.getActiveControl()?.hasTextFocus() || super.hasFocus();
    }
    computeEditorViewState(resource) {
        return this._multiDiffEditorWidget.getViewState();
    }
    tracksEditorViewState(input) {
        return input instanceof MultiDiffEditorInput;
    }
    toEditorViewStateResource(input) {
        return input.resource;
    }
    tryGetCodeEditor(resource) {
        return this._multiDiffEditorWidget.tryGetCodeEditor(resource);
    }
    findDocumentDiffItem(resource) {
        const i = this._multiDiffEditorWidget.findDocumentDiffItem(resource);
        if (!i) {
            return undefined;
        }
        const i2 = i;
        return i2.multiDiffEditorItem;
    }
    goToNextChange() {
        this._multiDiffEditorWidget?.goToNextChange();
    }
    goToPreviousChange() {
        this._multiDiffEditorWidget?.goToPreviousChange();
    }
    async showWhile(promise) {
        return this.editorProgressService.showWhile(promise);
    }
};
MultiDiffEditor = MultiDiffEditor_1 = ( __decorate([( __param(1, IInstantiationService)), ( __param(2, ITelemetryService)), ( __param(3, IThemeService)), ( __param(4, IStorageService)), ( __param(5, IEditorService)), ( __param(6, IEditorGroupsService)), ( __param(7, ITextResourceConfigurationService)), ( __param(8, IEditorProgressService))], MultiDiffEditor));
class MultiDiffEditorContentMenuOverlay extends Disposable {
    constructor(root, contextKeyService, instantiationService) {
        super();
        this.resourceObs = observableValue(this, undefined);
        const widget = instantiationService.createInstance(
            FloatingEditorToolbarWidget,
            MenuId.MultiDiffEditorContent,
            contextKeyService,
            this.resourceObs
        );
        widget.element.classList.add("multi-diff-root-floating-menu");
        this._register(widget);
        const showToolbarObs = derived(reader => {
            const resource = this.resourceObs.read(reader);
            const hasActions = widget.hasActions.read(reader);
            return resource !== undefined && hasActions;
        });
        this._register(autorun(reader => {
            const showToolbar = showToolbarObs.read(reader);
            if (!showToolbar) {
                return;
            }
            root.appendChild(widget.element);
            reader.store.add(toDisposable(() => {
                widget.element.remove();
            }));
        }));
    }
    updateResource(resource) {
        this.resourceObs.set(resource, undefined);
    }
}
let WorkbenchUIElementFactory = class WorkbenchUIElementFactory {
    constructor(_instantiationService) {
        this._instantiationService = _instantiationService;
    }
    createResourceLabel(element) {
        const label = this._instantiationService.createInstance(ResourceLabel, element, {});
        return {
            setUri(uri, options = {}) {
                if (!uri) {
                    label.element.clear();
                } else {
                    label.element.setFile(uri, {
                        strikethrough: options.strikethrough
                    });
                }
            },
            dispose() {
                label.dispose();
            }
        };
    }
};
WorkbenchUIElementFactory = ( __decorate([( __param(0, IInstantiationService))], WorkbenchUIElementFactory));

export { MultiDiffEditor };
