
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { h } from '../../../../base/browser/dom.js';
import { Disposable, toDisposable } from '../../../../base/common/lifecycle.js';
import { getActionBarActions, MenuEntryActionViewItem } from '../../../../platform/actions/browser/menuEntryActionViewItem.js';
import '../../../../base/common/observableInternal/index.js';
import { MenuWorkbenchToolBar, HiddenItemStrategy } from '../../../../platform/actions/browser/toolbar.js';
import { MenuId, MenuItemAction } from '../../../../platform/actions/common/actions.js';
import { IMenuService } from '../../../../platform/actions/common/actions.service.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.service.js';
import { OverlayWidgetPositionPreference } from '../../../browser/editorBrowser.js';
import { observableCodeEditor } from '../../../browser/observableCodeEditor.js';
import { derived } from '../../../../base/common/observableInternal/observables/derived.js';
import { autorun } from '../../../../base/common/observableInternal/reactions/autorun.js';
import { constObservable } from '../../../../base/common/observableInternal/observables/constObservable.js';
import { observableFromEvent } from '../../../../base/common/observableInternal/observables/observableFromEvent.js';

let FloatingEditorToolbar = class FloatingEditorToolbar extends Disposable {
    static {
        this.ID = "editor.contrib.floatingToolbar";
    }
    constructor(editor, instantiationService, keybindingService, menuService) {
        super();
        const editorObs = this._register(observableCodeEditor(editor));
        const editorUriObs = derived(reader => editorObs.model.read(reader)?.uri);
        const widget = this._register(instantiationService.createInstance(
            FloatingEditorToolbarWidget,
            MenuId.EditorContent,
            editor.contextKeyService,
            editorUriObs
        ));
        this._register(autorun(reader => {
            const hasActions = widget.hasActions.read(reader);
            if (!hasActions) {
                return;
            }
            reader.store.add(editorObs.createOverlayWidget({
                allowEditorOverflow: false,
                domNode: widget.element,
                minContentWidthInPx: constObservable(0),
                position: constObservable({
                    preference: OverlayWidgetPositionPreference.BOTTOM_RIGHT_CORNER
                })
            }));
        }));
    }
};
FloatingEditorToolbar = ( __decorate([( __param(1, IInstantiationService)), ( __param(2, IKeybindingService)), ( __param(3, IMenuService))], FloatingEditorToolbar));
let FloatingEditorToolbarWidget = class FloatingEditorToolbarWidget extends Disposable {
    constructor(
        _menuId,
        _scopedContextKeyService,
        _toolbarContext,
        instantiationService,
        keybindingService,
        menuService
    ) {
        super();
        const menu = this._register(menuService.createMenu(_menuId, _scopedContextKeyService));
        const menuGroupsObs = observableFromEvent(this, menu.onDidChange, () => menu.getActions());
        const menuPrimaryActionIdObs = derived(reader => {
            const menuGroups = menuGroupsObs.read(reader);
            const {
                primary
            } = getActionBarActions(menuGroups, () => true);
            return primary.length > 0 ? primary[0].id : undefined;
        });
        this.hasActions = derived(reader => menuGroupsObs.read(reader).length > 0);
        this.element = h("div.floating-menu-overlay-widget").root;
        this._register(toDisposable(() => this.element.remove()));
        this.element.style.height = "26px";
        this._register(autorun(reader => {
            const hasActions = this.hasActions.read(reader);
            const menuPrimaryActionId = menuPrimaryActionIdObs.read(reader);
            if (!hasActions) {
                return;
            }
            const toolbar = instantiationService.createInstance(MenuWorkbenchToolBar, this.element, _menuId, {
                actionViewItemProvider: (action, options) => {
                    if (!(action instanceof MenuItemAction)) {
                        return undefined;
                    }
                    return instantiationService.createInstance(class extends MenuEntryActionViewItem {
                        render(container) {
                            super.render(container);
                            if (action.id === menuPrimaryActionId) {
                                this.element?.classList.add("primary");
                            }
                        }
                        updateLabel() {
                            const keybinding = keybindingService.lookupKeybinding(action.id);
                            const keybindingLabel = keybinding ? keybinding.getLabel() : undefined;
                            if (this.options.label && this.label) {
                                this.label.textContent = keybindingLabel ? `${this._commandAction.label} (${keybindingLabel})` : this._commandAction.label;
                            }
                        }
                    }, action, {
                        ...options,
                        keybindingNotRenderedWithLabel: true
                    });
                },
                hiddenItemStrategy: HiddenItemStrategy.Ignore,
                menuOptions: {
                    shouldForwardArgs: true
                },
                telemetrySource: "editor.overlayToolbar",
                toolbarOptions: {
                    primaryGroup: () => true,
                    useSeparatorsInPrimaryActions: true
                }
            });
            reader.store.add(toolbar);
            reader.store.add(autorun(reader => {
                const context = _toolbarContext.read(reader);
                toolbar.context = context;
            }));
        }));
    }
};
FloatingEditorToolbarWidget = ( __decorate([( __param(3, IInstantiationService)), ( __param(4, IKeybindingService)), ( __param(5, IMenuService))], FloatingEditorToolbarWidget));

export { FloatingEditorToolbar, FloatingEditorToolbarWidget };
