"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const dom = require("../../../../base/browser/dom.js");
const actionViewItems = require("../../../../base/browser/ui/actionbar/actionViewItems.js");
const actions$1 = require("../../../../base/common/actions.js");
const lifecycle = require("../../../../base/common/lifecycle.js");
const platform = require("../../../../base/common/platform.js");
const editorExtensions = require("../../../browser/editorExtensions.js");
const editorContextKeys = require("../../../common/editorContextKeys.js");
const nls = require("../../../../nls.js");
const actions = require("../../../../platform/actions/common/actions.js");
const contextkey = require("../../../../platform/contextkey/common/contextkey.js");
const contextView = require("../../../../platform/contextview/browser/contextView.js");
const keybinding = require("../../../../platform/keybinding/common/keybinding.js");
var __decorate = globalThis && globalThis.__decorate || function(decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
    r = Reflect.decorate(decorators, target, key, desc);
  else
    for (var i = decorators.length - 1; i >= 0; i--)
      if (d = decorators[i])
        r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = globalThis && globalThis.__param || function(paramIndex, decorator) {
  return function(target, key) {
    decorator(target, key, paramIndex);
  };
};
exports.ContextMenuController = class ContextMenuController {
  constructor(editor, _contextMenuService, _contextViewService, _contextKeyService, _keybindingService, _menuService) {
    this._contextMenuService = _contextMenuService;
    this._contextViewService = _contextViewService;
    this._contextKeyService = _contextKeyService;
    this._keybindingService = _keybindingService;
    this._menuService = _menuService;
    this._toDispose = new lifecycle.DisposableStore();
    this._contextMenuIsBeingShownCount = 0;
    this._editor = editor;
    this._toDispose.add(this._editor.onContextMenu((e) => this._onContextMenu(e)));
    this._toDispose.add(this._editor.onMouseWheel((e) => {
      if (this._contextMenuIsBeingShownCount > 0) {
        const view = this._contextViewService.getContextViewElement();
        const target = e.srcElement;
        if (!(target.shadowRoot && dom.getShadowRoot(view) === target.shadowRoot)) {
          this._contextViewService.hideContextView();
        }
      }
    }));
    this._toDispose.add(this._editor.onKeyDown((e) => {
      if (e.keyCode === 58) {
        e.preventDefault();
        e.stopPropagation();
        this.showContextMenu();
      }
    }));
  }
  static get(editor) {
    return editor.getContribution(ContextMenuController.ID);
  }
  _onContextMenu(e) {
    if (!this._editor.hasModel()) {
      return;
    }
    if (!this._editor.getOption(
      20
      /* contextmenu */
    )) {
      this._editor.focus();
      if (e.target.position && !this._editor.getSelection().containsPosition(e.target.position)) {
        this._editor.setPosition(e.target.position);
      }
      return;
    }
    if (e.target.type === 12) {
      return;
    }
    if (e.target.type === 6 && e.target.detail.injectedText) {
      return;
    }
    e.event.preventDefault();
    e.event.stopPropagation();
    if (e.target.type !== 6 && e.target.type !== 7 && e.target.type !== 1) {
      return;
    }
    this._editor.focus();
    if (e.target.position) {
      let hasSelectionAtPosition = false;
      for (const selection of this._editor.getSelections()) {
        if (selection.containsPosition(e.target.position)) {
          hasSelectionAtPosition = true;
          break;
        }
      }
      if (!hasSelectionAtPosition) {
        this._editor.setPosition(e.target.position);
      }
    }
    let anchor = null;
    if (e.target.type !== 1) {
      anchor = { x: e.event.posx - 1, width: 2, y: e.event.posy - 1, height: 2 };
    }
    this.showContextMenu(anchor);
  }
  showContextMenu(anchor) {
    if (!this._editor.getOption(
      20
      /* contextmenu */
    )) {
      return;
    }
    if (!this._editor.hasModel()) {
      return;
    }
    if (!this._contextMenuService) {
      this._editor.focus();
      return;
    }
    const menuActions = this._getMenuActions(this._editor.getModel(), this._editor.isSimpleWidget ? actions.MenuId.SimpleEditorContext : actions.MenuId.EditorContext);
    if (menuActions.length > 0) {
      this._doShowContextMenu(menuActions, anchor);
    }
  }
  _getMenuActions(model, menuId) {
    const result = [];
    const menu = this._menuService.createMenu(menuId, this._contextKeyService);
    const groups = menu.getActions({ arg: model.uri });
    menu.dispose();
    for (let group of groups) {
      const [, actions$2] = group;
      let addedItems = 0;
      for (const action of actions$2) {
        if (action instanceof actions.SubmenuItemAction) {
          const subActions = this._getMenuActions(model, action.item.submenu);
          if (subActions.length > 0) {
            result.push(new actions$1.SubmenuAction(action.id, action.label, subActions));
            addedItems++;
          }
        } else {
          result.push(action);
          addedItems++;
        }
      }
      if (addedItems) {
        result.push(new actions$1.Separator());
      }
    }
    if (result.length) {
      result.pop();
    }
    return result;
  }
  _doShowContextMenu(actions2, anchor = null) {
    if (!this._editor.hasModel()) {
      return;
    }
    const oldHoverSetting = this._editor.getOption(
      53
      /* hover */
    );
    this._editor.updateOptions({
      hover: {
        enabled: false
      }
    });
    if (!anchor) {
      this._editor.revealPosition(
        this._editor.getPosition(),
        1
        /* Immediate */
      );
      this._editor.render();
      const cursorCoords = this._editor.getScrolledVisiblePosition(this._editor.getPosition());
      const editorCoords = dom.getDomNodePagePosition(this._editor.getDomNode());
      const posx = editorCoords.left + cursorCoords.left;
      const posy = editorCoords.top + cursorCoords.top + cursorCoords.height;
      anchor = { x: posx, y: posy };
    }
    const useShadowDOM = this._editor.getOption(
      115
      /* useShadowDOM */
    ) && !platform.isIOS;
    this._contextMenuIsBeingShownCount++;
    this._contextMenuService.showContextMenu({
      domForShadowRoot: useShadowDOM ? this._editor.getDomNode() : void 0,
      getAnchor: () => anchor,
      getActions: () => actions2,
      getActionViewItem: (action) => {
        const keybinding2 = this._keybindingFor(action);
        if (keybinding2) {
          return new actionViewItems.ActionViewItem(action, action, { label: true, keybinding: keybinding2.getLabel(), isMenu: true });
        }
        const customActionViewItem = action;
        if (typeof customActionViewItem.getActionViewItem === "function") {
          return customActionViewItem.getActionViewItem();
        }
        return new actionViewItems.ActionViewItem(action, action, { icon: true, label: true, isMenu: true });
      },
      getKeyBinding: (action) => {
        return this._keybindingFor(action);
      },
      onHide: (wasCancelled) => {
        this._contextMenuIsBeingShownCount--;
        this._editor.focus();
        this._editor.updateOptions({
          hover: oldHoverSetting
        });
      }
    });
  }
  _keybindingFor(action) {
    return this._keybindingService.lookupKeybinding(action.id);
  }
  dispose() {
    if (this._contextMenuIsBeingShownCount > 0) {
      this._contextViewService.hideContextView();
    }
    this._toDispose.dispose();
  }
};
exports.ContextMenuController.ID = "editor.contrib.contextmenu";
exports.ContextMenuController = __decorate([
  __param(1, contextView.IContextMenuService),
  __param(2, contextView.IContextViewService),
  __param(3, contextkey.IContextKeyService),
  __param(4, keybinding.IKeybindingService),
  __param(5, actions.IMenuService)
], exports.ContextMenuController);
class ShowContextMenu extends editorExtensions.EditorAction {
  constructor() {
    super({
      id: "editor.action.showContextMenu",
      label: nls.localize("action.showContextMenu.label", "Show Editor Context Menu"),
      alias: "Show Editor Context Menu",
      precondition: void 0,
      kbOpts: {
        kbExpr: editorContextKeys.EditorContextKeys.textInputFocus,
        primary: 1024 | 68,
        weight: 100
        /* EditorContrib */
      }
    });
  }
  run(accessor, editor) {
    var _a;
    (_a = exports.ContextMenuController.get(editor)) === null || _a === void 0 ? void 0 : _a.showContextMenu();
  }
}
editorExtensions.registerEditorContribution(exports.ContextMenuController.ID, exports.ContextMenuController);
editorExtensions.registerEditorAction(ShowContextMenu);
