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 { ActionRunner, SubmenuAction } from '../../../base/common/actions.js';
import { Codicon } from '../../../base/common/codicons.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 { createAndFillInActionBarActions } from './menuEntryActionViewItem.js';
import { MenuItemAction } from '../common/actions.js';
import { IMenuService } from '../common/actions.service.js';
import { IContextKeyService } from '../../contextkey/common/contextkey.service.js';
import { IContextMenuService } from '../../contextview/browser/contextView.service.js';
import { IHoverService } from '../../hover/browser/hover.service.js';
import { IKeybindingService } from '../../keybinding/common/keybinding.service.js';
import { ITelemetryService } from '../../telemetry/common/telemetry.service.js';
const _moduleId = "vs/platform/actions/browser/buttonbar";
let WorkbenchButtonBar = class WorkbenchButtonBar extends ButtonBar {
    constructor(container, _options, _contextMenuService, _keybindingService, telemetryService, _hoverService) {
        super(container);
        this._options = _options;
        this._contextMenuService = _contextMenuService;
        this._keybindingService = _keybindingService;
        this._hoverService = _hoverService;
        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, secondary) {
        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(_moduleId, 0, "{0} ({1})", action.label, kb.getLabel()));
            }
            else {
                tooltip = action.label;
            }
            this._updateStore.add(this._hoverService.setupManagedHover(hoverDelegate, btn.element, tooltip));
            this._updateStore.add(btn.onDidClick(async () => {
                this._actionRunner.run(action);
            }));
        }
        if (secondary.length > 0) {
            const btn = this.addButton({
                secondary: true,
                ariaLabel: ( localizeWithPath(_moduleId, 1, "More Actions"))
            });
            btn.icon = Codicon.dropDownButton;
            btn.element.classList.add('default-colors', 'monaco-text-button');
            btn.enabled = true;
            this._updateStore.add(this._hoverService.setupManagedHover(hoverDelegate, btn.element, ( localizeWithPath(_moduleId, 1, "More Actions"))));
            this._updateStore.add(btn.onDidClick(async () => {
                this._contextMenuService.showContextMenu({
                    getAnchor: () => btn.element,
                    getActions: () => secondary,
                    actionRunner: this._actionRunner,
                    onHide: () => btn.element.setAttribute('aria-expanded', 'false')
                });
                btn.element.setAttribute('aria-expanded', 'true');
            }));
        }
        this._onDidChange.fire(this);
    }
};
WorkbenchButtonBar = ( (__decorate([
    ( (__param(2, IContextMenuService))),
    ( (__param(3, IKeybindingService))),
    ( (__param(4, ITelemetryService))),
    ( (__param(5, IHoverService)))
], WorkbenchButtonBar)));
let MenuWorkbenchButtonBar = class MenuWorkbenchButtonBar extends WorkbenchButtonBar {
    constructor(container, menuId, options, menuService, contextKeyService, contextMenuService, keybindingService, telemetryService, hoverService) {
        super(container, options, contextMenuService, keybindingService, telemetryService, hoverService);
        const menu = menuService.createMenu(menuId, contextKeyService);
        this._store.add(menu);
        const update = () => {
            this.clear();
            const primary = [];
            const secondary = [];
            createAndFillInActionBarActions(menu, options?.menuOptions, { primary, secondary }, options?.toolbarOptions?.primaryGroup);
            super.update(primary, secondary);
        };
        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))),
    ( (__param(8, IHoverService)))
], MenuWorkbenchButtonBar)));
export { MenuWorkbenchButtonBar, WorkbenchButtonBar };
