import { __decorate, __param } from '../../../../../../external/tslib/tslib.es6.js';
import { addDisposableListener, EventType, trackFocus, $ } from '../../../base/browser/dom.js';
import { ActionBar } from '../../../base/browser/ui/actionbar/actionbar.js';
import { KeyCode, KeyMod as KeyMod$1 } from '../../../base/common/keyCodes.js';
import { Disposable, MutableDisposable, DisposableStore } from '../../../base/common/lifecycle.js';
import './actionWidget.css.js';
import { localizeWithPath, localize2WithPath } from '../../../nls.js';
import { acceptSelectedActionCommand, previewSelectedActionCommand, ActionList } from './actionList.js';
import { registerAction2, Action2 } from '../../actions/common/actions.js';
import { RawContextKey } from '../../contextkey/common/contextkey.js';
import { IContextKeyService } from '../../contextkey/common/contextkey.service.js';
import { IContextViewService } from '../../contextview/browser/contextView.service.js';
import { registerSingleton, InstantiationType } from '../../instantiation/common/extensions.js';
import { createDecorator, IInstantiationService } from '../../instantiation/common/instantiation.js';
import { KeybindingWeight } from '../../keybinding/common/keybindingsRegistry.js';
import { registerColor } from '../../theme/common/colorUtils.js';
import '../../theme/common/colors/baseColors.js';
import '../../theme/common/colors/chartsColors.js';
import '../../theme/common/colors/editorColors.js';
import { inputActiveOptionBackground } from '../../theme/common/colors/inputColors.js';
import '../../theme/common/colors/listColors.js';
import '../../theme/common/colors/menuColors.js';
import '../../theme/common/colors/minimapColors.js';
import '../../theme/common/colors/miscColors.js';
import '../../theme/common/colors/quickpickColors.js';
import '../../theme/common/colors/searchColors.js';
const _moduleId = "vs/platform/actionWidget/browser/actionWidget";
registerColor('actionBar.toggledBackground', inputActiveOptionBackground, ( localizeWithPath(_moduleId, 0, 'Background color for toggled action items in action bar.')));
const ActionWidgetContextKeys = {
    Visible: ( (new RawContextKey('codeActionMenuVisible', false, ( localizeWithPath(_moduleId, 1, "Whether the action widget list is visible")))))
};
const IActionWidgetService = ( (createDecorator('actionWidgetService')));
let ActionWidgetService = class ActionWidgetService extends Disposable {
    get isVisible() {
        return ActionWidgetContextKeys.Visible.getValue(this._contextKeyService) || false;
    }
    constructor(_contextViewService, _contextKeyService, _instantiationService) {
        super();
        this._contextViewService = _contextViewService;
        this._contextKeyService = _contextKeyService;
        this._instantiationService = _instantiationService;
        this._list = this._register(( (new MutableDisposable())));
    }
    show(user, supportsPreview, items, delegate, anchor, container, actionBarActions) {
        const visibleContext = ActionWidgetContextKeys.Visible.bindTo(this._contextKeyService);
        const list = this._instantiationService.createInstance(ActionList, user, supportsPreview, items, delegate);
        this._contextViewService.showContextView({
            getAnchor: () => anchor,
            render: (container) => {
                visibleContext.set(true);
                return this._renderWidget(container, list, actionBarActions ?? []);
            },
            onHide: (didCancel) => {
                visibleContext.reset();
                this._onWidgetClosed(didCancel);
            },
        }, container, false);
    }
    acceptSelected(preview) {
        this._list.value?.acceptSelected(preview);
    }
    focusPrevious() {
        this._list?.value?.focusPrevious();
    }
    focusNext() {
        this._list?.value?.focusNext();
    }
    hide(didCancel) {
        this._list.value?.hide(didCancel);
        this._list.clear();
    }
    clear() {
        this._list.clear();
    }
    _renderWidget(element, list, actionBarActions) {
        const widget = document.createElement('div');
        widget.classList.add('action-widget');
        element.appendChild(widget);
        this._list.value = list;
        if (this._list.value) {
            widget.appendChild(this._list.value.domNode);
        }
        else {
            throw ( (new Error('List has no value')));
        }
        const renderDisposables = ( (new DisposableStore()));
        const menuBlock = document.createElement('div');
        const block = element.appendChild(menuBlock);
        block.classList.add('context-view-block');
        renderDisposables.add(addDisposableListener(block, EventType.MOUSE_DOWN, e => e.stopPropagation()));
        const pointerBlockDiv = document.createElement('div');
        const pointerBlock = element.appendChild(pointerBlockDiv);
        pointerBlock.classList.add('context-view-pointerBlock');
        renderDisposables.add(addDisposableListener(pointerBlock, EventType.POINTER_MOVE, () => pointerBlock.remove()));
        renderDisposables.add(addDisposableListener(pointerBlock, EventType.MOUSE_DOWN, () => pointerBlock.remove()));
        let actionBarWidth = 0;
        if (actionBarActions.length) {
            const actionBar = this._createActionBar('.action-widget-action-bar', actionBarActions);
            if (actionBar) {
                widget.appendChild(actionBar.getContainer().parentElement);
                renderDisposables.add(actionBar);
                actionBarWidth = actionBar.getContainer().offsetWidth;
            }
        }
        const width = this._list.value?.layout(actionBarWidth);
        widget.style.width = `${width}px`;
        const focusTracker = renderDisposables.add(trackFocus(element));
        renderDisposables.add(focusTracker.onDidBlur(() => this.hide(true)));
        return renderDisposables;
    }
    _createActionBar(className, actions) {
        if (!actions.length) {
            return undefined;
        }
        const container = $(className);
        const actionBar = ( (new ActionBar(container)));
        actionBar.push(actions, { icon: false, label: true });
        return actionBar;
    }
    _onWidgetClosed(didCancel) {
        this._list.value?.hide(didCancel);
    }
};
ActionWidgetService = ( (__decorate([
    ( (__param(0, IContextViewService))),
    ( (__param(1, IContextKeyService))),
    ( (__param(2, IInstantiationService)))
], ActionWidgetService)));
registerSingleton(IActionWidgetService, ActionWidgetService, InstantiationType.Delayed);
const weight = KeybindingWeight.EditorContrib + 1000;
registerAction2(class extends Action2 {
    constructor() {
        super({
            id: 'hideCodeActionWidget',
            title: ( localize2WithPath(_moduleId, 2, "Hide action widget")),
            precondition: ActionWidgetContextKeys.Visible,
            keybinding: {
                weight,
                primary: KeyCode.Escape,
                secondary: [KeyMod$1.Shift | KeyCode.Escape]
            },
        });
    }
    run(accessor) {
        accessor.get(IActionWidgetService).hide(true);
    }
});
registerAction2(class extends Action2 {
    constructor() {
        super({
            id: 'selectPrevCodeAction',
            title: ( localize2WithPath(_moduleId, 3, "Select previous action")),
            precondition: ActionWidgetContextKeys.Visible,
            keybinding: {
                weight,
                primary: KeyCode.UpArrow,
                secondary: [KeyMod$1.CtrlCmd | KeyCode.UpArrow],
                mac: { primary: KeyCode.UpArrow, secondary: [KeyMod$1.CtrlCmd | KeyCode.UpArrow, KeyMod$1.WinCtrl | KeyCode.KeyP] },
            }
        });
    }
    run(accessor) {
        const widgetService = accessor.get(IActionWidgetService);
        if (widgetService instanceof ActionWidgetService) {
            widgetService.focusPrevious();
        }
    }
});
registerAction2(class extends Action2 {
    constructor() {
        super({
            id: 'selectNextCodeAction',
            title: ( localize2WithPath(_moduleId, 4, "Select next action")),
            precondition: ActionWidgetContextKeys.Visible,
            keybinding: {
                weight,
                primary: KeyCode.DownArrow,
                secondary: [KeyMod$1.CtrlCmd | KeyCode.DownArrow],
                mac: { primary: KeyCode.DownArrow, secondary: [KeyMod$1.CtrlCmd | KeyCode.DownArrow, KeyMod$1.WinCtrl | KeyCode.KeyN] }
            }
        });
    }
    run(accessor) {
        const widgetService = accessor.get(IActionWidgetService);
        if (widgetService instanceof ActionWidgetService) {
            widgetService.focusNext();
        }
    }
});
registerAction2(class extends Action2 {
    constructor() {
        super({
            id: acceptSelectedActionCommand,
            title: ( localize2WithPath(_moduleId, 5, "Accept selected action")),
            precondition: ActionWidgetContextKeys.Visible,
            keybinding: {
                weight,
                primary: KeyCode.Enter,
                secondary: [KeyMod$1.CtrlCmd | KeyCode.Period],
            }
        });
    }
    run(accessor) {
        const widgetService = accessor.get(IActionWidgetService);
        if (widgetService instanceof ActionWidgetService) {
            widgetService.acceptSelected();
        }
    }
});
registerAction2(class extends Action2 {
    constructor() {
        super({
            id: previewSelectedActionCommand,
            title: ( localize2WithPath(_moduleId, 6, "Preview selected action")),
            precondition: ActionWidgetContextKeys.Visible,
            keybinding: {
                weight,
                primary: KeyMod$1.CtrlCmd | KeyCode.Enter,
            }
        });
    }
    run(accessor) {
        const widgetService = accessor.get(IActionWidgetService);
        if (widgetService instanceof ActionWidgetService) {
            widgetService.acceptSelected(true);
        }
    }
});
export { IActionWidgetService };
