"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const strings = require("../../../../base/common/strings.js");
const range = require("../../../common/core/range.js");
const cancellation = require("../../../../base/common/cancellation.js");
const lifecycle = require("../../../../base/common/lifecycle.js");
const keybindingCancellation = require("./keybindingCancellation.js");
class EditorState {
  constructor(editor, flags) {
    this.flags = flags;
    if ((this.flags & 1) !== 0) {
      const model = editor.getModel();
      this.modelVersionId = model ? strings.format("{0}#{1}", model.uri.toString(), model.getVersionId()) : null;
    } else {
      this.modelVersionId = null;
    }
    if ((this.flags & 4) !== 0) {
      this.position = editor.getPosition();
    } else {
      this.position = null;
    }
    if ((this.flags & 2) !== 0) {
      this.selection = editor.getSelection();
    } else {
      this.selection = null;
    }
    if ((this.flags & 8) !== 0) {
      this.scrollLeft = editor.getScrollLeft();
      this.scrollTop = editor.getScrollTop();
    } else {
      this.scrollLeft = -1;
      this.scrollTop = -1;
    }
  }
  _equals(other) {
    if (!(other instanceof EditorState)) {
      return false;
    }
    const state = other;
    if (this.modelVersionId !== state.modelVersionId) {
      return false;
    }
    if (this.scrollLeft !== state.scrollLeft || this.scrollTop !== state.scrollTop) {
      return false;
    }
    if (!this.position && state.position || this.position && !state.position || this.position && state.position && !this.position.equals(state.position)) {
      return false;
    }
    if (!this.selection && state.selection || this.selection && !state.selection || this.selection && state.selection && !this.selection.equalsRange(state.selection)) {
      return false;
    }
    return true;
  }
  validate(editor) {
    return this._equals(new EditorState(editor, this.flags));
  }
}
class EditorStateCancellationTokenSource extends keybindingCancellation.EditorKeybindingCancellationTokenSource {
  constructor(editor, flags, range$1, parent) {
    super(editor, parent);
    this._listener = new lifecycle.DisposableStore();
    if (flags & 4) {
      this._listener.add(editor.onDidChangeCursorPosition((e) => {
        if (!range$1 || !range.Range.containsPosition(range$1, e.position)) {
          this.cancel();
        }
      }));
    }
    if (flags & 2) {
      this._listener.add(editor.onDidChangeCursorSelection((e) => {
        if (!range$1 || !range.Range.containsRange(range$1, e.selection)) {
          this.cancel();
        }
      }));
    }
    if (flags & 8) {
      this._listener.add(editor.onDidScrollChange((_) => this.cancel()));
    }
    if (flags & 1) {
      this._listener.add(editor.onDidChangeModel((_) => this.cancel()));
      this._listener.add(editor.onDidChangeModelContent((_) => this.cancel()));
    }
  }
  dispose() {
    this._listener.dispose();
    super.dispose();
  }
}
class TextModelCancellationTokenSource extends cancellation.CancellationTokenSource {
  constructor(model, parent) {
    super(parent);
    this._listener = model.onDidChangeContent(() => this.cancel());
  }
  dispose() {
    this._listener.dispose();
    super.dispose();
  }
}
exports.EditorState = EditorState;
exports.EditorStateCancellationTokenSource = EditorStateCancellationTokenSource;
exports.TextModelCancellationTokenSource = TextModelCancellationTokenSource;
