"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Side = exports.JSON_ID = exports.GapCursorSelection = exports.GapBookmark = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _model = require("@atlaskit/editor-prosemirror/model");
var _state = require("@atlaskit/editor-prosemirror/state");
var _isValidTargetNode = require("./utils/is-valid-target-node");
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var Side = exports.Side = /*#__PURE__*/function (Side) {
  Side["LEFT"] = "left";
  Side["RIGHT"] = "right";
  return Side;
}({});
var JSON_ID = exports.JSON_ID = 'gapcursor';
var GapCursorSelection = exports.GapCursorSelection = /*#__PURE__*/function (_Selection) {
  (0, _inherits2.default)(GapCursorSelection, _Selection);
  var _super = _createSuper(GapCursorSelection);
  /**
   * Construct a GapCursorSelection
   * @param {ResolvedPos} $pos resolved position
   * @param {Side} side side where the gap cursor is drawn
   */
  function GapCursorSelection($pos) {
    var _this;
    var side = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Side.LEFT;
    (0, _classCallCheck2.default)(this, GapCursorSelection);
    _this = _super.call(this, $pos, $pos);
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "visible", false);
    _this.side = side;
    return _this;
  }
  (0, _createClass2.default)(GapCursorSelection, [{
    key: "map",
    value: function map(doc, mapping) {
      var $pos = doc.resolve(mapping.map(this.head));
      return GapCursorSelection.valid($pos) ? new GapCursorSelection($pos, this.side) : _state.Selection.near($pos);
    }
  }, {
    key: "eq",
    value: function eq(other) {
      return other instanceof GapCursorSelection && other.head === this.head;
    }
  }, {
    key: "content",
    value: function content() {
      return _model.Slice.empty;
    }
  }, {
    key: "getBookmark",
    value: function getBookmark() {
      return new GapBookmark(this.anchor);
    }
  }, {
    key: "toJSON",
    value: function toJSON() {
      return {
        pos: this.head,
        type: JSON_ID,
        side: this.side
      };
    }
  }], [{
    key: "valid",
    value: function valid($pos) {
      var parent = $pos.parent,
        nodeBefore = $pos.nodeBefore,
        nodeAfter = $pos.nodeAfter;
      var targetNode = (0, _isValidTargetNode.isValidTargetNode)(nodeBefore) ? nodeBefore : (0, _isValidTargetNode.isValidTargetNode)(nodeAfter) ? nodeAfter : null;
      if (!targetNode || parent.isTextblock) {
        return false;
      }
      var deflt = parent.contentMatchAt($pos.index()).defaultType;
      return deflt && deflt.isTextblock;
    }
  }, {
    key: "findFrom",
    value: function findFrom($pos, dir) {
      var mustMove = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
      var side = dir === 1 ? Side.RIGHT : Side.LEFT;
      if (!mustMove && GapCursorSelection.valid($pos)) {
        return new GapCursorSelection($pos, side);
      }
      var pos = $pos.pos;

      // TODO: Fix any, potential issue. ED-5048
      var next = null;

      // Scan up from this position
      for (var d = $pos.depth;; d--) {
        var parent = $pos.node(d);
        if (side === Side.RIGHT ? $pos.indexAfter(d) < parent.childCount : $pos.index(d) > 0) {
          next = parent.maybeChild(side === Side.RIGHT ? $pos.indexAfter(d) : $pos.index(d) - 1);
          break;
        } else if (d === 0) {
          return null;
        }
        pos += dir;
        var $cur = $pos.doc.resolve(pos);
        if (GapCursorSelection.valid($cur)) {
          return new GapCursorSelection($cur, side);
        }
      }

      // And then down into the next node
      for (;;) {
        next = side === Side.RIGHT ? next.firstChild : next.lastChild;
        if (next === null) {
          break;
        }
        pos += dir;
        var _$cur = $pos.doc.resolve(pos);
        if (GapCursorSelection.valid(_$cur)) {
          return new GapCursorSelection(_$cur, side);
        }
      }
      return null;
    }
  }, {
    key: "fromJSON",
    value: function fromJSON(doc, json) {
      return new GapCursorSelection(doc.resolve(json.pos), json.side);
    }
  }]);
  return GapCursorSelection;
}(_state.Selection);
_state.Selection.jsonID(JSON_ID, GapCursorSelection);
var GapBookmark = exports.GapBookmark = /*#__PURE__*/function () {
  function GapBookmark(pos) {
    (0, _classCallCheck2.default)(this, GapBookmark);
    this.pos = pos;
  }
  (0, _createClass2.default)(GapBookmark, [{
    key: "map",
    value: function map(mapping) {
      return new GapBookmark(mapping.map(this.pos));
    }
  }, {
    key: "resolve",
    value: function resolve(doc) {
      var $pos = doc.resolve(this.pos);
      return GapCursorSelection.valid($pos) ? new GapCursorSelection($pos) : _state.Selection.near($pos);
    }
  }]);
  return GapBookmark;
}();