"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const editOperation = require("../../../common/core/editOperation.js");
const range = require("../../../common/core/range.js");
class FormattingEdit {
  static _handleEolEdits(editor, edits) {
    let newEol = void 0;
    let singleEdits = [];
    for (let edit of edits) {
      if (typeof edit.eol === "number") {
        newEol = edit.eol;
      }
      if (edit.range && typeof edit.text === "string") {
        singleEdits.push(edit);
      }
    }
    if (typeof newEol === "number") {
      if (editor.hasModel()) {
        editor.getModel().pushEOL(newEol);
      }
    }
    return singleEdits;
  }
  static _isFullModelReplaceEdit(editor, edit) {
    if (!editor.hasModel()) {
      return false;
    }
    const model = editor.getModel();
    const editRange = model.validateRange(edit.range);
    const fullModelRange = model.getFullModelRange();
    return fullModelRange.equalsRange(editRange);
  }
  static execute(editor, _edits, addUndoStops) {
    if (addUndoStops) {
      editor.pushUndoStop();
    }
    const edits = FormattingEdit._handleEolEdits(editor, _edits);
    if (edits.length === 1 && FormattingEdit._isFullModelReplaceEdit(editor, edits[0])) {
      editor.executeEdits("formatEditsCommand", edits.map((edit) => editOperation.EditOperation.replace(range.Range.lift(edit.range), edit.text)));
    } else {
      editor.executeEdits("formatEditsCommand", edits.map((edit) => editOperation.EditOperation.replaceMove(range.Range.lift(edit.range), edit.text)));
    }
    if (addUndoStops) {
      editor.pushUndoStop();
    }
  }
}
exports.FormattingEdit = FormattingEdit;
