"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const dom = require("../../../../base/browser/dom.js");
const actions$1 = require("../../../../base/common/actions.js");
const cancellation = require("../../../../base/common/cancellation.js");
const editorExtensions = require("../../../browser/editorExtensions.js");
const range = require("../../../common/core/range.js");
const resolverService = require("../../../common/services/resolverService.js");
const goToCommands = require("../../gotoSymbol/browser/goToCommands.js");
const peekView = require("../../peekView/browser/peekView.js");
const actions = require("../../../../platform/actions/common/actions.js");
const commands = require("../../../../platform/commands/common/commands.js");
const contextkey = require("../../../../platform/contextkey/common/contextkey.js");
const contextView = require("../../../../platform/contextview/browser/contextView.js");
const instantiation = require("../../../../platform/instantiation/common/instantiation.js");
const notification = require("../../../../platform/notification/common/notification.js");
var __awaiter = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
  function adopt(value) {
    return value instanceof P ? value : new P(function(resolve) {
      resolve(value);
    });
  }
  return new (P || (P = Promise))(function(resolve, reject) {
    function fulfilled(value) {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    }
    function rejected(value) {
      try {
        step(generator["throw"](value));
      } catch (e) {
        reject(e);
      }
    }
    function step(result) {
      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
    }
    step((generator = generator.apply(thisArg, _arguments || [])).next());
  });
};
function showGoToContextMenu(accessor, editor, anchor, part) {
  var _a;
  return __awaiter(this, void 0, void 0, function* () {
    const resolverService$1 = accessor.get(resolverService.ITextModelService);
    const contextMenuService = accessor.get(contextView.IContextMenuService);
    const commandService = accessor.get(commands.ICommandService);
    const instaService = accessor.get(instantiation.IInstantiationService);
    const notificationService = accessor.get(notification.INotificationService);
    yield part.item.resolve(cancellation.CancellationToken.None);
    if (!part.part.location) {
      return;
    }
    const location = part.part.location;
    const menuActions = [];
    const filter = new Set(actions.MenuRegistry.getMenuItems(actions.MenuId.EditorContext).map((item) => actions.isIMenuItem(item) ? item.command.id : ""));
    for (const delegate of editorExtensions.EditorExtensionsRegistry.getEditorActions()) {
      if (delegate instanceof goToCommands.SymbolNavigationAction && filter.has(delegate.id)) {
        menuActions.push(new actions$1.Action(delegate.id, delegate.label, void 0, true, () => __awaiter(this, void 0, void 0, function* () {
          const ref = yield resolverService$1.createModelReference(location.uri);
          try {
            yield instaService.invokeFunction(delegate.run.bind(delegate), editor, new goToCommands.SymbolNavigationAnchor(ref.object.textEditorModel, range.Range.getStartPosition(location.range)));
          } finally {
            ref.dispose();
          }
        })));
      }
    }
    if (part.part.command) {
      const { command } = part.part;
      menuActions.push(new actions$1.Separator());
      menuActions.push(new actions$1.Action(command.id, command.title, void 0, true, () => __awaiter(this, void 0, void 0, function* () {
        var _b;
        try {
          yield commandService.executeCommand(command.id, ...(_b = command.arguments) !== null && _b !== void 0 ? _b : []);
        } catch (err) {
          notificationService.notify({
            severity: notification.Severity.Error,
            source: part.item.provider.displayName,
            message: err
          });
        }
      })));
    }
    const useShadowDOM = editor.getOption(
      115
      /* useShadowDOM */
    );
    contextMenuService.showContextMenu({
      domForShadowRoot: useShadowDOM ? (_a = editor.getDomNode()) !== null && _a !== void 0 ? _a : void 0 : void 0,
      getAnchor: () => {
        const box = dom.getDomNodePagePosition(anchor);
        return { x: box.left, y: box.top + box.height + 8 };
      },
      getActions: () => menuActions,
      onHide: () => {
        editor.focus();
      },
      autoSelectFirstItem: true
    });
  });
}
function goToDefinitionWithLocation(accessor, event, editor, location) {
  return __awaiter(this, void 0, void 0, function* () {
    const resolverService$1 = accessor.get(resolverService.ITextModelService);
    const ref = yield resolverService$1.createModelReference(location.uri);
    yield editor.invokeWithinContext((accessor2) => __awaiter(this, void 0, void 0, function* () {
      const openToSide = event.hasSideBySideModifier;
      const contextKeyService = accessor2.get(contextkey.IContextKeyService);
      const isInPeek = peekView.PeekContext.inPeekEditor.getValue(contextKeyService);
      const canPeek = !openToSide && editor.getOption(
        78
        /* definitionLinkOpensInPeek */
      ) && !isInPeek;
      const action = new goToCommands.DefinitionAction({ openToSide, openInPeek: canPeek, muteMessage: true }, { alias: "", label: "", id: "", precondition: void 0 });
      return action.run(accessor2, editor, { model: ref.object.textEditorModel, position: range.Range.getStartPosition(location.range) });
    }));
    ref.dispose();
  });
}
exports.goToDefinitionWithLocation = goToDefinitionWithLocation;
exports.showGoToContextMenu = showGoToContextMenu;
