"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const aria = require("../../../../base/browser/ui/aria/aria.js");
const htmlContent = require("../../../../base/common/htmlContent.js");
const keyCodes = require("../../../../base/common/keyCodes.js");
require("./anchorSelect.css.js");
const editorExtensions = require("../../../browser/editorExtensions.js");
const selection = require("../../../common/core/selection.js");
const editorContextKeys = require("../../../common/editorContextKeys.js");
const nls = require("../../../../nls.js");
const contextkey = require("../../../../platform/contextkey/common/contextkey.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);
  };
};
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());
  });
};
const SelectionAnchorSet = new contextkey.RawContextKey("selectionAnchorSet", false);
let SelectionAnchorController = class SelectionAnchorController2 {
  constructor(editor, contextKeyService) {
    this.editor = editor;
    this.selectionAnchorSetContextKey = SelectionAnchorSet.bindTo(contextKeyService);
    this.modelChangeListener = editor.onDidChangeModel(() => this.selectionAnchorSetContextKey.reset());
  }
  static get(editor) {
    return editor.getContribution(SelectionAnchorController2.ID);
  }
  setSelectionAnchor() {
    if (this.editor.hasModel()) {
      const position = this.editor.getPosition();
      const previousDecorations = this.decorationId ? [this.decorationId] : [];
      const newDecorationId = this.editor.deltaDecorations(previousDecorations, [{
        range: selection.Selection.fromPositions(position, position),
        options: {
          description: "selection-anchor",
          stickiness: 1,
          hoverMessage: new htmlContent.MarkdownString().appendText(nls.localize("selectionAnchor", "Selection Anchor")),
          className: "selection-anchor"
        }
      }]);
      this.decorationId = newDecorationId[0];
      this.selectionAnchorSetContextKey.set(!!this.decorationId);
      aria.alert(nls.localize("anchorSet", "Anchor set at {0}:{1}", position.lineNumber, position.column));
    }
  }
  goToSelectionAnchor() {
    if (this.editor.hasModel() && this.decorationId) {
      const anchorPosition = this.editor.getModel().getDecorationRange(this.decorationId);
      if (anchorPosition) {
        this.editor.setPosition(anchorPosition.getStartPosition());
      }
    }
  }
  selectFromAnchorToCursor() {
    if (this.editor.hasModel() && this.decorationId) {
      const start = this.editor.getModel().getDecorationRange(this.decorationId);
      if (start) {
        const end = this.editor.getPosition();
        this.editor.setSelection(selection.Selection.fromPositions(start.getStartPosition(), end));
        this.cancelSelectionAnchor();
      }
    }
  }
  cancelSelectionAnchor() {
    if (this.decorationId) {
      this.editor.deltaDecorations([this.decorationId], []);
      this.decorationId = void 0;
      this.selectionAnchorSetContextKey.set(false);
    }
  }
  dispose() {
    this.cancelSelectionAnchor();
    this.modelChangeListener.dispose();
  }
};
SelectionAnchorController.ID = "editor.contrib.selectionAnchorController";
SelectionAnchorController = __decorate([
  __param(1, contextkey.IContextKeyService)
], SelectionAnchorController);
class SetSelectionAnchor extends editorExtensions.EditorAction {
  constructor() {
    super({
      id: "editor.action.setSelectionAnchor",
      label: nls.localize("setSelectionAnchor", "Set Selection Anchor"),
      alias: "Set Selection Anchor",
      precondition: void 0,
      kbOpts: {
        kbExpr: editorContextKeys.EditorContextKeys.editorTextFocus,
        primary: keyCodes.KeyChord(
          2048 | 41,
          2048 | 32
          /* KeyB */
        ),
        weight: 100
        /* EditorContrib */
      }
    });
  }
  run(_accessor, editor) {
    var _a;
    return __awaiter(this, void 0, void 0, function* () {
      (_a = SelectionAnchorController.get(editor)) === null || _a === void 0 ? void 0 : _a.setSelectionAnchor();
    });
  }
}
class GoToSelectionAnchor extends editorExtensions.EditorAction {
  constructor() {
    super({
      id: "editor.action.goToSelectionAnchor",
      label: nls.localize("goToSelectionAnchor", "Go to Selection Anchor"),
      alias: "Go to Selection Anchor",
      precondition: SelectionAnchorSet
    });
  }
  run(_accessor, editor) {
    var _a;
    return __awaiter(this, void 0, void 0, function* () {
      (_a = SelectionAnchorController.get(editor)) === null || _a === void 0 ? void 0 : _a.goToSelectionAnchor();
    });
  }
}
class SelectFromAnchorToCursor extends editorExtensions.EditorAction {
  constructor() {
    super({
      id: "editor.action.selectFromAnchorToCursor",
      label: nls.localize("selectFromAnchorToCursor", "Select from Anchor to Cursor"),
      alias: "Select from Anchor to Cursor",
      precondition: SelectionAnchorSet,
      kbOpts: {
        kbExpr: editorContextKeys.EditorContextKeys.editorTextFocus,
        primary: keyCodes.KeyChord(
          2048 | 41,
          2048 | 41
          /* KeyK */
        ),
        weight: 100
        /* EditorContrib */
      }
    });
  }
  run(_accessor, editor) {
    var _a;
    return __awaiter(this, void 0, void 0, function* () {
      (_a = SelectionAnchorController.get(editor)) === null || _a === void 0 ? void 0 : _a.selectFromAnchorToCursor();
    });
  }
}
class CancelSelectionAnchor extends editorExtensions.EditorAction {
  constructor() {
    super({
      id: "editor.action.cancelSelectionAnchor",
      label: nls.localize("cancelSelectionAnchor", "Cancel Selection Anchor"),
      alias: "Cancel Selection Anchor",
      precondition: SelectionAnchorSet,
      kbOpts: {
        kbExpr: editorContextKeys.EditorContextKeys.editorTextFocus,
        primary: 9,
        weight: 100
        /* EditorContrib */
      }
    });
  }
  run(_accessor, editor) {
    var _a;
    return __awaiter(this, void 0, void 0, function* () {
      (_a = SelectionAnchorController.get(editor)) === null || _a === void 0 ? void 0 : _a.cancelSelectionAnchor();
    });
  }
}
editorExtensions.registerEditorContribution(SelectionAnchorController.ID, SelectionAnchorController);
editorExtensions.registerEditorAction(SetSelectionAnchor);
editorExtensions.registerEditorAction(GoToSelectionAnchor);
editorExtensions.registerEditorAction(SelectFromAnchorToCursor);
editorExtensions.registerEditorAction(CancelSelectionAnchor);
exports.SelectionAnchorSet = SelectionAnchorSet;
