"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const dom = require("../../../base/browser/dom.js");
const mouseEvent = require("../../../base/browser/mouseEvent.js");
const menu = require("../../../base/browser/ui/menu/menu.js");
const actions = require("../../../base/common/actions.js");
const errors = require("../../../base/common/errors.js");
const lifecycle = require("../../../base/common/lifecycle.js");
require("./contextMenuHandler.css.js");
const styler = require("../../theme/common/styler.js");
class ContextMenuHandler {
  constructor(contextViewService, telemetryService, notificationService, keybindingService, themeService) {
    this.contextViewService = contextViewService;
    this.telemetryService = telemetryService;
    this.notificationService = notificationService;
    this.keybindingService = keybindingService;
    this.themeService = themeService;
    this.focusToReturn = null;
    this.block = null;
    this.options = { blockMouse: true };
  }
  configure(options) {
    this.options = options;
  }
  showContextMenu(delegate) {
    const actions$1 = delegate.getActions();
    if (!actions$1.length) {
      return;
    }
    this.focusToReturn = document.activeElement;
    let menu$1;
    let shadowRootElement = dom.isHTMLElement(delegate.domForShadowRoot) ? delegate.domForShadowRoot : void 0;
    this.contextViewService.showContextView({
      getAnchor: () => delegate.getAnchor(),
      canRelayout: false,
      anchorAlignment: delegate.anchorAlignment,
      anchorAxisAlignment: delegate.anchorAxisAlignment,
      render: (container) => {
        let className = delegate.getMenuClassName ? delegate.getMenuClassName() : "";
        if (className) {
          container.className += " " + className;
        }
        if (this.options.blockMouse) {
          this.block = container.appendChild(dom.$(".context-view-block"));
          this.block.style.position = "fixed";
          this.block.style.cursor = "initial";
          this.block.style.left = "0";
          this.block.style.top = "0";
          this.block.style.width = "100%";
          this.block.style.height = "100%";
          this.block.style.zIndex = "-1";
          dom.addDisposableListener(this.block, dom.EventType.MOUSE_DOWN, (e) => e.stopPropagation());
        }
        const menuDisposables = new lifecycle.DisposableStore();
        const actionRunner = delegate.actionRunner || new actions.ActionRunner();
        actionRunner.onBeforeRun(this.onActionRun, this, menuDisposables);
        actionRunner.onDidRun(this.onDidActionRun, this, menuDisposables);
        menu$1 = new menu.Menu(container, actions$1, {
          actionViewItemProvider: delegate.getActionViewItem,
          context: delegate.getActionsContext ? delegate.getActionsContext() : null,
          actionRunner,
          getKeyBinding: delegate.getKeyBinding ? delegate.getKeyBinding : (action) => this.keybindingService.lookupKeybinding(action.id)
        });
        menuDisposables.add(styler.attachMenuStyler(menu$1, this.themeService));
        menu$1.onDidCancel(() => this.contextViewService.hideContextView(true), null, menuDisposables);
        menu$1.onDidBlur(() => this.contextViewService.hideContextView(true), null, menuDisposables);
        menuDisposables.add(dom.addDisposableListener(window, dom.EventType.BLUR, () => this.contextViewService.hideContextView(true)));
        menuDisposables.add(dom.addDisposableListener(window, dom.EventType.MOUSE_DOWN, (e) => {
          if (e.defaultPrevented) {
            return;
          }
          let event = new mouseEvent.StandardMouseEvent(e);
          let element = event.target;
          if (event.rightButton) {
            return;
          }
          while (element) {
            if (element === container) {
              return;
            }
            element = element.parentElement;
          }
          this.contextViewService.hideContextView(true);
        }));
        return lifecycle.combinedDisposable(menuDisposables, menu$1);
      },
      focus: () => {
        if (menu$1) {
          menu$1.focus(!!delegate.autoSelectFirstItem);
        }
      },
      onHide: (didCancel) => {
        if (delegate.onHide) {
          delegate.onHide(!!didCancel);
        }
        if (this.block) {
          this.block.remove();
          this.block = null;
        }
        if (this.focusToReturn) {
          this.focusToReturn.focus();
        }
      }
    }, shadowRootElement, !!shadowRootElement);
  }
  onActionRun(e) {
    this.telemetryService.publicLog2("workbenchActionExecuted", { id: e.action.id, from: "contextMenu" });
    this.contextViewService.hideContextView(false);
    if (this.focusToReturn) {
      this.focusToReturn.focus();
    }
  }
  onDidActionRun(e) {
    if (e.error && !errors.isCancellationError(e.error)) {
      this.notificationService.error(e.error);
    }
  }
}
exports.ContextMenuHandler = ContextMenuHandler;
