"use strict";
const editorExtensions = require("../../../browser/editorExtensions.js");
const replaceCommand = require("../../../common/commands/replaceCommand.js");
const cursorMoveOperations = require("../../../common/cursor/cursorMoveOperations.js");
const range = require("../../../common/core/range.js");
const editorContextKeys = require("../../../common/editorContextKeys.js");
const nls = require("../../../../nls.js");
class TransposeLettersAction extends editorExtensions.EditorAction {
  constructor() {
    super({
      id: "editor.action.transposeLetters",
      label: nls.localize("transposeLetters.label", "Transpose Letters"),
      alias: "Transpose Letters",
      precondition: editorContextKeys.EditorContextKeys.writable,
      kbOpts: {
        kbExpr: editorContextKeys.EditorContextKeys.textInputFocus,
        primary: 0,
        mac: {
          primary: 256 | 50
          /* KeyT */
        },
        weight: 100
        /* EditorContrib */
      }
    });
  }
  run(accessor, editor) {
    if (!editor.hasModel()) {
      return;
    }
    let model = editor.getModel();
    let commands = [];
    let selections = editor.getSelections();
    for (let selection of selections) {
      if (!selection.isEmpty()) {
        continue;
      }
      let lineNumber = selection.startLineNumber;
      let column = selection.startColumn;
      let lastColumn = model.getLineMaxColumn(lineNumber);
      if (lineNumber === 1 && (column === 1 || column === 2 && lastColumn === 2)) {
        continue;
      }
      let endPosition = column === lastColumn ? selection.getPosition() : cursorMoveOperations.MoveOperations.rightPosition(model, selection.getPosition().lineNumber, selection.getPosition().column);
      let middlePosition = cursorMoveOperations.MoveOperations.leftPosition(model, endPosition);
      let beginPosition = cursorMoveOperations.MoveOperations.leftPosition(model, middlePosition);
      let leftChar = model.getValueInRange(range.Range.fromPositions(beginPosition, middlePosition));
      let rightChar = model.getValueInRange(range.Range.fromPositions(middlePosition, endPosition));
      let replaceRange = range.Range.fromPositions(beginPosition, endPosition);
      commands.push(new replaceCommand.ReplaceCommand(replaceRange, rightChar + leftChar));
    }
    if (commands.length > 0) {
      editor.pushUndoStop();
      editor.executeCommands(this.id, commands);
      editor.pushUndoStop();
    }
  }
}
editorExtensions.registerEditorAction(TransposeLettersAction);
