"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.RemoteCursorWidget = void 0;

var _EditorContentManager = require("./EditorContentManager");

var _Validation = require("./Validation");

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

/**
 * This class creates the cursor and tooltip nodes and adds them
 * to CodeMirror as a widget.
 *
 * @internal
 */
var RemoteCursorWidget =
/*#__PURE__*/
function () {
  function RemoteCursorWidget(codeEditor, widgetId, color, label, tooltipEnabled, tooltipDuration, onDisposed) {
    var _this = this;

    _classCallCheck(this, RemoteCursorWidget);

    this._updateTooltipPosition = function () {
      var distanceFromTop = _this._domNode.offsetTop - _this._editor.getScrollInfo().top;

      if (distanceFromTop - _this._tooltipNode.offsetHeight < 5) {
        _this._tooltipNode.style.top = "".concat(_this._tooltipNode.offsetHeight - 5, "px");
      } else {
        _this._tooltipNode.style.top = "-".concat(_this._tooltipNode.offsetHeight, "px");
      }

      _this._tooltipNode.style.left = "0";
    };

    this._onInsert = function (index, text) {
      var currentIndex = _this._index;

      if (index <= currentIndex) {
        var newIndex = currentIndex + text.length;

        var position = _this._editor.posFromIndex(newIndex);

        _this._updatePosition(position);
      }
    };

    this._onReplace = function (index, length, text) {
      var currentIndex = _this._index;

      if (index <= currentIndex) {
        var newIndex = currentIndex - Math.min(currentIndex - index, length) + text.length;

        var position = _this._editor.posFromIndex(newIndex);

        _this._updatePosition(position);
      }
    };

    this._onDelete = function (index, length) {
      var currentIndex = _this._index;

      if (index <= currentIndex) {
        var newIndex = currentIndex - Math.min(currentIndex - index, length);

        var position = _this._editor.posFromIndex(newIndex);

        _this._updatePosition(position);
      }
    };

    this._editor = codeEditor;
    this._tooltipDuration = tooltipDuration;
    this._id = "codemirror-remote-cursor-".concat(widgetId); // Create the main node for the cursor element.

    this._domNode = document.createElement("div");
    this._domNode.className = "codemirror-remote-cursor";
    this._domNode.style.background = color;

    this._editor.addWidget({
      line: 0,
      ch: 0
    }, this._domNode, false); // Create the tooltip element if the tooltip is enabled.


    if (tooltipEnabled) {
      this._tooltipNode = document.createElement("div");
      this._tooltipNode.className = "codemirror-remote-cursor-tooltip";
      this._tooltipNode.style.background = color;
      this._tooltipNode.innerHTML = label;

      this._domNode.appendChild(this._tooltipNode); // we only need to listen to scroll positions to update the
      // tooltip location on scrolling.


      this._editor.on("scroll", this._updateTooltipPosition);
    } else {
      this._tooltipNode = null;
    }

    this._contentManager = new _EditorContentManager.EditorContentManager({
      editor: this._editor,
      onInsert: this._onInsert,
      onReplace: this._onReplace,
      onDelete: this._onDelete,
      id: "remote-cursor-" + widgetId
    });
    this._index = 0;
    this._position = {
      line: 0,
      ch: 0
    };
    this._hideTimer = null;
    this._onDisposed = onDisposed;
    this._disposed = false;
  }

  _createClass(RemoteCursorWidget, [{
    key: "hide",
    value: function hide() {
      this._domNode.style.display = "none";
    }
  }, {
    key: "show",
    value: function show() {
      this._domNode.style.display = "inherit";
    }
  }, {
    key: "setIndex",
    value: function setIndex(index) {
      _Validation.Validation.assertNumber(index, "index");

      var position = this._editor.posFromIndex(index);

      this.setPosition(position);
    }
  }, {
    key: "setPosition",
    value: function setPosition(position) {
      var _this2 = this;

      _Validation.Validation.assertPosition(position, "position");

      this._updatePosition(position);

      if (this._tooltipNode !== null) {
        setTimeout(function () {
          return _this2._showTooltip();
        }, 0);
      }
    }
  }, {
    key: "isDisposed",
    value: function isDisposed() {
      return this._disposed;
    }
  }, {
    key: "dispose",
    value: function dispose() {
      if (this._disposed) {
        return;
      }

      this._editor.off("scroll", this._updateTooltipPosition);

      this._contentManager.dispose();

      this._disposed = true;

      this._onDisposed();
    }
  }, {
    key: "getId",
    value: function getId() {
      return this._id;
    }
  }, {
    key: "getPosition",
    value: function getPosition() {
      return this._position;
    }
  }, {
    key: "_updatePosition",
    value: function _updatePosition(position) {
      var cursorCoords = this._editor.cursorCoords(position, "local");

      var height = cursorCoords.bottom - cursorCoords.top;
      this._domNode.style.height = height + "px";
      this._domNode.style.left = cursorCoords.left + "px";
      this._domNode.style.top = cursorCoords.top + "px";
      this._index = this._editor.indexFromPos(position);
      this._position = position;
      console.log(this._index, this._position);
    }
  }, {
    key: "_showTooltip",
    value: function _showTooltip() {
      var _this3 = this;

      this._updateTooltipPosition();

      if (this._hideTimer !== null) {
        clearTimeout(this._hideTimer);
      } else {
        this._setTooltipVisible(true);
      }

      this._hideTimer = setTimeout(function () {
        _this3._setTooltipVisible(false);

        _this3._hideTimer = null;
      }, this._tooltipDuration);
    }
  }, {
    key: "_setTooltipVisible",
    value: function _setTooltipVisible(visible) {
      if (visible) {
        this._tooltipNode.style.opacity = "1.0";
      } else {
        this._tooltipNode.style.opacity = "0";
      }
    }
  }]);

  return RemoteCursorWidget;
}();

exports.RemoteCursorWidget = RemoteCursorWidget;