"use strict";
require("./accessibilityHelp.css.js");
const dom = require("../../../../base/browser/dom.js");
const fastDomNode = require("../../../../base/browser/fastDomNode.js");
const formattedTextRenderer = require("../../../../base/browser/formattedTextRenderer.js");
const aria = require("../../../../base/browser/ui/aria/aria.js");
const widget = require("../../../../base/browser/ui/widget.js");
const lifecycle = require("../../../../base/common/lifecycle.js");
const platform = require("../../../../base/common/platform.js");
const strings = require("../../../../base/common/strings.js");
const uri = require("../../../../base/common/uri.js");
const editorExtensions = require("../../../browser/editorExtensions.js");
const editorContextKeys = require("../../../common/editorContextKeys.js");
const toggleTabFocusMode = require("../../../contrib/toggleTabFocusMode/browser/toggleTabFocusMode.js");
const contextkey = require("../../../../platform/contextkey/common/contextkey.js");
const instantiation = require("../../../../platform/instantiation/common/instantiation.js");
const keybinding = require("../../../../platform/keybinding/common/keybinding.js");
const opener = require("../../../../platform/opener/common/opener.js");
const colorRegistry = require("../../../../platform/theme/common/colorRegistry.js");
const themeService = require("../../../../platform/theme/common/themeService.js");
const standaloneStrings = require("../../../common/standaloneStrings.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);
  };
};
const CONTEXT_ACCESSIBILITY_WIDGET_VISIBLE = new contextkey.RawContextKey("accessibilityHelpWidgetVisible", false);
let AccessibilityHelpController = class AccessibilityHelpController2 extends lifecycle.Disposable {
  constructor(editor, instantiationService) {
    super();
    this._editor = editor;
    this._widget = this._register(instantiationService.createInstance(AccessibilityHelpWidget, this._editor));
  }
  static get(editor) {
    return editor.getContribution(AccessibilityHelpController2.ID);
  }
  show() {
    this._widget.show();
  }
  hide() {
    this._widget.hide();
  }
};
AccessibilityHelpController.ID = "editor.contrib.accessibilityHelpController";
AccessibilityHelpController = __decorate([
  __param(1, instantiation.IInstantiationService)
], AccessibilityHelpController);
function getSelectionLabel(selections, charactersSelected) {
  if (!selections || selections.length === 0) {
    return standaloneStrings.AccessibilityHelpNLS.noSelection;
  }
  if (selections.length === 1) {
    if (charactersSelected) {
      return strings.format(standaloneStrings.AccessibilityHelpNLS.singleSelectionRange, selections[0].positionLineNumber, selections[0].positionColumn, charactersSelected);
    }
    return strings.format(standaloneStrings.AccessibilityHelpNLS.singleSelection, selections[0].positionLineNumber, selections[0].positionColumn);
  }
  if (charactersSelected) {
    return strings.format(standaloneStrings.AccessibilityHelpNLS.multiSelectionRange, selections.length, charactersSelected);
  }
  if (selections.length > 0) {
    return strings.format(standaloneStrings.AccessibilityHelpNLS.multiSelection, selections.length);
  }
  return "";
}
let AccessibilityHelpWidget = class AccessibilityHelpWidget2 extends widget.Widget {
  constructor(editor, _contextKeyService, _keybindingService, _openerService) {
    super();
    this._contextKeyService = _contextKeyService;
    this._keybindingService = _keybindingService;
    this._openerService = _openerService;
    this._editor = editor;
    this._isVisibleKey = CONTEXT_ACCESSIBILITY_WIDGET_VISIBLE.bindTo(this._contextKeyService);
    this._domNode = fastDomNode.createFastDomNode(document.createElement("div"));
    this._domNode.setClassName("accessibilityHelpWidget");
    this._domNode.setDisplay("none");
    this._domNode.setAttribute("role", "dialog");
    this._domNode.setAttribute("aria-hidden", "true");
    this._contentDomNode = fastDomNode.createFastDomNode(document.createElement("div"));
    this._contentDomNode.setAttribute("role", "document");
    this._domNode.appendChild(this._contentDomNode);
    this._isVisible = false;
    this._register(this._editor.onDidLayoutChange(() => {
      if (this._isVisible) {
        this._layout();
      }
    }));
    this._register(dom.addStandardDisposableListener(this._contentDomNode.domNode, "keydown", (e) => {
      if (!this._isVisible) {
        return;
      }
      if (e.equals(
        2048 | 35
        /* KeyE */
      )) {
        aria.alert(standaloneStrings.AccessibilityHelpNLS.emergencyConfOn);
        this._editor.updateOptions({
          accessibilitySupport: "on"
        });
        dom.clearNode(this._contentDomNode.domNode);
        this._buildContent();
        this._contentDomNode.domNode.focus();
        e.preventDefault();
        e.stopPropagation();
      }
      if (e.equals(
        2048 | 38
        /* KeyH */
      )) {
        aria.alert(standaloneStrings.AccessibilityHelpNLS.openingDocs);
        let url = this._editor.getRawOptions().accessibilityHelpUrl;
        if (typeof url === "undefined") {
          url = "https://go.microsoft.com/fwlink/?linkid=852450";
        }
        this._openerService.open(uri.URI.parse(url));
        e.preventDefault();
        e.stopPropagation();
      }
    }));
    this.onblur(this._contentDomNode.domNode, () => {
      this.hide();
    });
    this._editor.addOverlayWidget(this);
  }
  dispose() {
    this._editor.removeOverlayWidget(this);
    super.dispose();
  }
  getId() {
    return AccessibilityHelpWidget2.ID;
  }
  getDomNode() {
    return this._domNode.domNode;
  }
  getPosition() {
    return {
      preference: null
    };
  }
  show() {
    if (this._isVisible) {
      return;
    }
    this._isVisible = true;
    this._isVisibleKey.set(true);
    this._layout();
    this._domNode.setDisplay("block");
    this._domNode.setAttribute("aria-hidden", "false");
    this._contentDomNode.domNode.tabIndex = 0;
    this._buildContent();
    this._contentDomNode.domNode.focus();
  }
  _descriptionForCommand(commandId, msg, noKbMsg) {
    const kb = this._keybindingService.lookupKeybinding(commandId);
    if (kb) {
      return strings.format(msg, kb.getAriaLabel());
    }
    return strings.format(noKbMsg, commandId);
  }
  _buildContent() {
    const options = this._editor.getOptions();
    const selections = this._editor.getSelections();
    let charactersSelected = 0;
    if (selections) {
      const model = this._editor.getModel();
      if (model) {
        selections.forEach((selection) => {
          charactersSelected += model.getValueLengthInRange(selection);
        });
      }
    }
    let text = getSelectionLabel(selections, charactersSelected);
    if (options.get(
      54
      /* inDiffEditor */
    )) {
      if (options.get(
        81
        /* readOnly */
      )) {
        text += standaloneStrings.AccessibilityHelpNLS.readonlyDiffEditor;
      } else {
        text += standaloneStrings.AccessibilityHelpNLS.editableDiffEditor;
      }
    } else {
      if (options.get(
        81
        /* readOnly */
      )) {
        text += standaloneStrings.AccessibilityHelpNLS.readonlyEditor;
      } else {
        text += standaloneStrings.AccessibilityHelpNLS.editableEditor;
      }
    }
    const turnOnMessage = platform.isMacintosh ? standaloneStrings.AccessibilityHelpNLS.changeConfigToOnMac : standaloneStrings.AccessibilityHelpNLS.changeConfigToOnWinLinux;
    switch (options.get(
      2
      /* accessibilitySupport */
    )) {
      case 0:
        text += "\n\n - " + turnOnMessage;
        break;
      case 2:
        text += "\n\n - " + standaloneStrings.AccessibilityHelpNLS.auto_on;
        break;
      case 1:
        text += "\n\n - " + standaloneStrings.AccessibilityHelpNLS.auto_off;
        text += " " + turnOnMessage;
        break;
    }
    if (options.get(
      130
      /* tabFocusMode */
    )) {
      text += "\n\n - " + this._descriptionForCommand(toggleTabFocusMode.ToggleTabFocusModeAction.ID, standaloneStrings.AccessibilityHelpNLS.tabFocusModeOnMsg, standaloneStrings.AccessibilityHelpNLS.tabFocusModeOnMsgNoKb);
    } else {
      text += "\n\n - " + this._descriptionForCommand(toggleTabFocusMode.ToggleTabFocusModeAction.ID, standaloneStrings.AccessibilityHelpNLS.tabFocusModeOffMsg, standaloneStrings.AccessibilityHelpNLS.tabFocusModeOffMsgNoKb);
    }
    const openDocMessage = platform.isMacintosh ? standaloneStrings.AccessibilityHelpNLS.openDocMac : standaloneStrings.AccessibilityHelpNLS.openDocWinLinux;
    text += "\n\n - " + openDocMessage;
    text += "\n\n" + standaloneStrings.AccessibilityHelpNLS.outroMsg;
    this._contentDomNode.domNode.appendChild(formattedTextRenderer.renderFormattedText(text));
    this._contentDomNode.domNode.setAttribute("aria-label", text);
  }
  hide() {
    if (!this._isVisible) {
      return;
    }
    this._isVisible = false;
    this._isVisibleKey.reset();
    this._domNode.setDisplay("none");
    this._domNode.setAttribute("aria-hidden", "true");
    this._contentDomNode.domNode.tabIndex = -1;
    dom.clearNode(this._contentDomNode.domNode);
    this._editor.focus();
  }
  _layout() {
    const editorLayout = this._editor.getLayoutInfo();
    const w = Math.max(5, Math.min(AccessibilityHelpWidget2.WIDTH, editorLayout.width - 40));
    const h = Math.max(5, Math.min(AccessibilityHelpWidget2.HEIGHT, editorLayout.height - 40));
    this._domNode.setWidth(w);
    this._domNode.setHeight(h);
    const top = Math.round((editorLayout.height - h) / 2);
    this._domNode.setTop(top);
    const left = Math.round((editorLayout.width - w) / 2);
    this._domNode.setLeft(left);
  }
};
AccessibilityHelpWidget.ID = "editor.contrib.accessibilityHelpWidget";
AccessibilityHelpWidget.WIDTH = 500;
AccessibilityHelpWidget.HEIGHT = 300;
AccessibilityHelpWidget = __decorate([
  __param(1, contextkey.IContextKeyService),
  __param(2, keybinding.IKeybindingService),
  __param(3, opener.IOpenerService)
], AccessibilityHelpWidget);
class ShowAccessibilityHelpAction extends editorExtensions.EditorAction {
  constructor() {
    super({
      id: "editor.action.showAccessibilityHelp",
      label: standaloneStrings.AccessibilityHelpNLS.showAccessibilityHelpAction,
      alias: "Show Accessibility Help",
      precondition: void 0,
      kbOpts: {
        primary: 512 | 59,
        weight: 100,
        linux: {
          primary: 512 | 1024 | 59,
          secondary: [
            512 | 59
            /* F1 */
          ]
        }
      }
    });
  }
  run(accessor, editor) {
    const controller = AccessibilityHelpController.get(editor);
    if (controller) {
      controller.show();
    }
  }
}
editorExtensions.registerEditorContribution(AccessibilityHelpController.ID, AccessibilityHelpController);
editorExtensions.registerEditorAction(ShowAccessibilityHelpAction);
const AccessibilityHelpCommand = editorExtensions.EditorCommand.bindToContribution(AccessibilityHelpController.get);
editorExtensions.registerEditorCommand(new AccessibilityHelpCommand({
  id: "closeAccessibilityHelp",
  precondition: CONTEXT_ACCESSIBILITY_WIDGET_VISIBLE,
  handler: (x) => x.hide(),
  kbOpts: {
    weight: 100 + 100,
    kbExpr: editorContextKeys.EditorContextKeys.focus,
    primary: 9,
    secondary: [
      1024 | 9
      /* Escape */
    ]
  }
}));
themeService.registerThemingParticipant((theme, collector) => {
  const widgetBackground = theme.getColor(colorRegistry.editorWidgetBackground);
  if (widgetBackground) {
    collector.addRule(`.monaco-editor .accessibilityHelpWidget { background-color: ${widgetBackground}; }`);
  }
  const widgetForeground = theme.getColor(colorRegistry.editorWidgetForeground);
  if (widgetForeground) {
    collector.addRule(`.monaco-editor .accessibilityHelpWidget { color: ${widgetForeground}; }`);
  }
  const widgetShadowColor = theme.getColor(colorRegistry.widgetShadow);
  if (widgetShadowColor) {
    collector.addRule(`.monaco-editor .accessibilityHelpWidget { box-shadow: 0 2px 8px ${widgetShadowColor}; }`);
  }
  const hcBorder = theme.getColor(colorRegistry.contrastBorder);
  if (hcBorder) {
    collector.addRule(`.monaco-editor .accessibilityHelpWidget { border: 2px solid ${hcBorder}; }`);
  }
});
