import { alert } from "../../../../base/browser/ui/aria/aria.mjs";
import { TimeoutTimer } from "../../../../base/common/async.mjs";
import { MutableDisposable, DisposableStore } from "../../../../base/common/lifecycle.mjs";
import "./messageController.css.mjs";
import { EditorCommand, registerEditorCommand, registerEditorContribution } from "../../../browser/editorExtensions.mjs";
import { Range } from "../../../common/core/range.mjs";
import { localize } from "../../../../nls.mjs";
import { RawContextKey, IContextKeyService } from "../../../../platform/contextkey/common/contextkey.mjs";
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);
  };
};
let MessageController = class MessageController2 {
  constructor(editor, contextKeyService) {
    this._messageWidget = new MutableDisposable();
    this._messageListeners = new DisposableStore();
    this._editor = editor;
    this._visible = MessageController2.MESSAGE_VISIBLE.bindTo(contextKeyService);
    this._editorListener = this._editor.onDidAttemptReadOnlyEdit(() => this._onDidAttemptReadOnlyEdit());
  }
  static get(editor) {
    return editor.getContribution(MessageController2.ID);
  }
  dispose() {
    this._editorListener.dispose();
    this._messageListeners.dispose();
    this._messageWidget.dispose();
    this._visible.reset();
  }
  showMessage(message, position) {
    alert(message);
    this._visible.set(true);
    this._messageWidget.clear();
    this._messageListeners.clear();
    this._messageWidget.value = new MessageWidget(this._editor, position, message);
    this._messageListeners.add(this._editor.onDidBlurEditorText(() => this.closeMessage()));
    this._messageListeners.add(this._editor.onDidChangeCursorPosition(() => this.closeMessage()));
    this._messageListeners.add(this._editor.onDidDispose(() => this.closeMessage()));
    this._messageListeners.add(this._editor.onDidChangeModel(() => this.closeMessage()));
    this._messageListeners.add(new TimeoutTimer(() => this.closeMessage(), 3e3));
    let bounds;
    this._messageListeners.add(this._editor.onMouseMove((e) => {
      if (!e.target.position) {
        return;
      }
      if (!bounds) {
        bounds = new Range(position.lineNumber - 3, 1, e.target.position.lineNumber + 3, 1);
      } else if (!bounds.containsPosition(e.target.position)) {
        this.closeMessage();
      }
    }));
  }
  closeMessage() {
    this._visible.reset();
    this._messageListeners.clear();
    if (this._messageWidget.value) {
      this._messageListeners.add(MessageWidget.fadeOut(this._messageWidget.value));
    }
  }
  _onDidAttemptReadOnlyEdit() {
    if (this._editor.hasModel()) {
      this.showMessage(localize("editor.readonly", "Cannot edit in read-only editor"), this._editor.getPosition());
    }
  }
};
MessageController.ID = "editor.contrib.messageController";
MessageController.MESSAGE_VISIBLE = new RawContextKey("messageVisible", false, localize("messageVisible", "Whether the editor is currently showing an inline message"));
MessageController = __decorate([
  __param(1, IContextKeyService)
], MessageController);
const MessageCommand = EditorCommand.bindToContribution(MessageController.get);
registerEditorCommand(new MessageCommand({
  id: "leaveEditorMessage",
  precondition: MessageController.MESSAGE_VISIBLE,
  handler: (c) => c.closeMessage(),
  kbOpts: {
    weight: 100 + 30,
    primary: 9
    /* Escape */
  }
}));
class MessageWidget {
  constructor(editor, { lineNumber, column }, text) {
    this.allowEditorOverflow = true;
    this.suppressMouseDown = false;
    this._editor = editor;
    this._editor.revealLinesInCenterIfOutsideViewport(
      lineNumber,
      lineNumber,
      0
      /* Smooth */
    );
    this._position = { lineNumber, column: column - 1 };
    this._domNode = document.createElement("div");
    this._domNode.classList.add("monaco-editor-overlaymessage");
    const anchorTop = document.createElement("div");
    anchorTop.classList.add("anchor", "top");
    this._domNode.appendChild(anchorTop);
    const message = document.createElement("div");
    message.classList.add("message");
    message.textContent = text;
    this._domNode.appendChild(message);
    const anchorBottom = document.createElement("div");
    anchorBottom.classList.add("anchor", "below");
    this._domNode.appendChild(anchorBottom);
    this._editor.addContentWidget(this);
    this._domNode.classList.add("fadeIn");
  }
  static fadeOut(messageWidget) {
    let handle;
    const dispose = () => {
      messageWidget.dispose();
      clearTimeout(handle);
      messageWidget.getDomNode().removeEventListener("animationend", dispose);
    };
    handle = setTimeout(dispose, 110);
    messageWidget.getDomNode().addEventListener("animationend", dispose);
    messageWidget.getDomNode().classList.add("fadeOut");
    return { dispose };
  }
  dispose() {
    this._editor.removeContentWidget(this);
  }
  getId() {
    return "messageoverlay";
  }
  getDomNode() {
    return this._domNode;
  }
  getPosition() {
    return { position: this._position, preference: [
      1,
      2
      /* BELOW */
    ] };
  }
  afterRender(position) {
    this._domNode.classList.toggle(
      "below",
      position === 2
      /* BELOW */
    );
  }
}
registerEditorContribution(MessageController.ID, MessageController);
export {
  MessageController
};
