import { __decorate, __param } from '../../../../../../external/tslib/tslib.es6.js';
import { ButtonBar } from '../../../base/browser/ui/button/button.js';
import { createInstantHoverDelegate } from '../../../base/browser/ui/hover/hoverDelegateFactory.js';
import { setupCustomHover } from '../../../base/browser/ui/hover/updatableHoverWidget.js';
import { ActionRunner, SubmenuAction } from '../../../base/common/actions.js';
import { Emitter } from '../../../base/common/event.js';
import { DisposableStore } from '../../../base/common/lifecycle.js';
import { ThemeIcon } from '../../../base/common/themables.js';
import { localizeWithPath } from '../../../nls.js';
import { IMenuService, MenuItemAction } from '../common/actions.js';
import { IContextKeyService } from '../../contextkey/common/contextkey.js';
import { IContextMenuService } from '../../contextview/browser/contextView.js';
import { IKeybindingService } from '../../keybinding/common/keybinding.js';
import { ITelemetryService } from '../../telemetry/common/telemetry.js';
let WorkbenchButtonBar = class WorkbenchButtonBar extends ButtonBar {
    constructor(container, _options, _contextMenuService, _keybindingService, telemetryService) {
        super(container);
        this._options = _options;
        this._contextMenuService = _contextMenuService;
        this._keybindingService = _keybindingService;
        this._store = ( new DisposableStore());
        this._updateStore = ( new DisposableStore());
        this._onDidChange = ( new Emitter());
        this.onDidChange = this._onDidChange.event;
        this._actionRunner = this._store.add(( new ActionRunner()));
        if (_options?.telemetrySource) {
            this._actionRunner.onDidRun(e => {
                telemetryService.publicLog2('workbenchActionExecuted', { id: e.action.id, from: _options.telemetrySource });
            }, undefined, this._store);
        }
    }
    dispose() {
        this._onDidChange.dispose();
        this._updateStore.dispose();
        this._store.dispose();
        super.dispose();
    }
    update(actions) {
        const conifgProvider = this._options?.buttonConfigProvider ?? (() => ({ showLabel: true }));
        this._updateStore.clear();
        this.clear();
        const hoverDelegate = this._updateStore.add(createInstantHoverDelegate());
        for (let i = 0; i < actions.length; i++) {
            const secondary = i > 0;
            const actionOrSubmenu = actions[i];
            let action;
            let btn;
            if (actionOrSubmenu instanceof SubmenuAction && actionOrSubmenu.actions.length > 0) {
                const [first, ...rest] = actionOrSubmenu.actions;
                action = first;
                btn = this.addButtonWithDropdown({
                    secondary: conifgProvider(action)?.isSecondary ?? secondary,
                    actionRunner: this._actionRunner,
                    actions: rest,
                    contextMenuProvider: this._contextMenuService,
                    ariaLabel: action.label
                });
            }
            else {
                action = actionOrSubmenu;
                btn = this.addButton({
                    secondary: conifgProvider(action)?.isSecondary ?? secondary,
                    ariaLabel: action.label
                });
            }
            btn.enabled = action.enabled;
            btn.element.classList.add('default-colors');
            if (conifgProvider(action)?.showLabel ?? true) {
                btn.label = action.label;
            }
            else {
                btn.element.classList.add('monaco-text-button');
            }
            if (conifgProvider(action)?.showIcon) {
                if (action instanceof MenuItemAction && ThemeIcon.isThemeIcon(action.item.icon)) {
                    btn.icon = action.item.icon;
                }
                else if (action.class) {
                    btn.element.classList.add(...action.class.split(' '));
                }
            }
            const kb = this._keybindingService.lookupKeybinding(action.id);
            let tooltip;
            if (kb) {
                tooltip = ( localizeWithPath(
                    'vs/platform/actions/browser/buttonbar',
                    'labelWithKeybinding',
                    "{0} ({1})",
                    action.label,
                    kb.getLabel()
                ));
            }
            else {
                tooltip = action.label;
            }
            this._updateStore.add(setupCustomHover(hoverDelegate, btn.element, tooltip));
            this._updateStore.add(btn.onDidClick(async () => {
                this._actionRunner.run(action);
            }));
        }
        this._onDidChange.fire(this);
    }
};
WorkbenchButtonBar = ( __decorate([
    ( __param(2, IContextMenuService)),
    ( __param(3, IKeybindingService)),
    ( __param(4, ITelemetryService))
], WorkbenchButtonBar));
let MenuWorkbenchButtonBar = class MenuWorkbenchButtonBar extends WorkbenchButtonBar {
    constructor(container, menuId, options, menuService, contextKeyService, contextMenuService, keybindingService, telemetryService) {
        super(container, options, contextMenuService, keybindingService, telemetryService);
        const menu = menuService.createMenu(menuId, contextKeyService);
        this._store.add(menu);
        const update = () => {
            this.clear();
            const actions = menu
                .getActions({ renderShortTitle: true })
                .flatMap(entry => entry[1]);
            super.update(actions);
        };
        this._store.add(menu.onDidChange(update));
        update();
    }
    dispose() {
        super.dispose();
    }
    update(_actions) {
        throw new Error('Use Menu or WorkbenchButtonBar');
    }
};
MenuWorkbenchButtonBar = ( __decorate([
    ( __param(3, IMenuService)),
    ( __param(4, IContextKeyService)),
    ( __param(5, IContextMenuService)),
    ( __param(6, IKeybindingService)),
    ( __param(7, ITelemetryService))
], MenuWorkbenchButtonBar));
export { MenuWorkbenchButtonBar, WorkbenchButtonBar };
