import { registerEditorAction, EditorAction } from "../../../browser/editorExtensions.mjs";
import { ReplaceCommand } from "../../../common/commands/replaceCommand.mjs";
import { MoveOperations } from "../../../common/cursor/cursorMoveOperations.mjs";
import { Range } from "../../../common/core/range.mjs";
import { EditorContextKeys } from "../../../common/editorContextKeys.mjs";
import { localize } from "../../../../nls.mjs";
class TransposeLettersAction extends EditorAction {
  constructor() {
    super({
      id: "editor.action.transposeLetters",
      label: localize("transposeLetters.label", "Transpose Letters"),
      alias: "Transpose Letters",
      precondition: EditorContextKeys.writable,
      kbOpts: {
        kbExpr: 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() : MoveOperations.rightPosition(model, selection.getPosition().lineNumber, selection.getPosition().column);
      let middlePosition = MoveOperations.leftPosition(model, endPosition);
      let beginPosition = MoveOperations.leftPosition(model, middlePosition);
      let leftChar = model.getValueInRange(Range.fromPositions(beginPosition, middlePosition));
      let rightChar = model.getValueInRange(Range.fromPositions(middlePosition, endPosition));
      let replaceRange = Range.fromPositions(beginPosition, endPosition);
      commands.push(new ReplaceCommand(replaceRange, rightChar + leftChar));
    }
    if (commands.length > 0) {
      editor.pushUndoStop();
      editor.executeCommands(this.id, commands);
      editor.pushUndoStop();
    }
  }
}
registerEditorAction(TransposeLettersAction);
