"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.JSON_ID = exports.GapCursorSelection = exports.GapBookmark = void 0;
Object.defineProperty(exports, "Side", {
  enumerable: true,
  get: function get() {
    return _Side.Side;
  }
});
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _model = require("@atlaskit/editor-prosemirror/model");
var _state = require("@atlaskit/editor-prosemirror/state");
var _Side = require("./Side");
var _isValidTargetNode = require("./utils/is-valid-target-node");
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var JSON_ID = exports.JSON_ID = 'gapcursor';
var GapCursorSelection = exports.GapCursorSelection = /*#__PURE__*/function (_Selection) {
  /**
   * 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.Side.LEFT;
    (0, _classCallCheck2.default)(this, GapCursorSelection);
    _this = _callSuper(this, GapCursorSelection, [$pos, $pos]);
    (0, _defineProperty2.default)(_this, "visible", false);
    _this.side = side;
    return _this;
  }
  (0, _inherits2.default)(GapCursorSelection, _Selection);
  return (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;
      }

      // Ignored via go/ees005
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      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.Side.RIGHT : _Side.Side.LEFT;
      if (!mustMove && GapCursorSelection.valid($pos)) {
        return new GapCursorSelection($pos, side);
      }
      var pos = $pos.pos;

      // TODO: ED-5048 - Fix any, potential issue.
      // Ignored via go/ees005
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      var next = null;

      // Scan up from this position
      for (var d = $pos.depth;; d--) {
        var parent = $pos.node(d);
        if (side === _Side.Side.RIGHT ? $pos.indexAfter(d) < parent.childCount : $pos.index(d) > 0) {
          next = parent.maybeChild(side === _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.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);
    }
  }]);
}(_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;
  }

  // Ignored via go/ees005
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  return (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);
    }
  }]);
}(); // eslint-disable-next-line @atlaskit/editor/no-re-export