"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.atTheBeginningOfBlock = atTheBeginningOfBlock;
exports.atTheBeginningOfDoc = atTheBeginningOfDoc;
exports.atTheEndOfBlock = atTheEndOfBlock;
exports.atTheEndOfDoc = atTheEndOfDoc;
exports.endPositionOfParent = endPositionOfParent;
exports.isSelectionAtStartOfNode = exports.isSelectionAtEndOfNode = void 0;
exports.startPositionOfParent = startPositionOfParent;
var _state = require("@atlaskit/editor-prosemirror/state");
var _selection = require("./gap-cursor/selection");
var isSelectionAtStartOfNode = exports.isSelectionAtStartOfNode = function isSelectionAtStartOfNode($pos, parentNode) {
  if (!parentNode) {
    return false;
  }
  for (var i = $pos.depth + 1; i > 0; i--) {
    var node = $pos.node(i);
    if (node && node.eq(parentNode.node)) {
      break;
    }
    if (i > 1 && $pos.before(i) !== $pos.before(i - 1) + 1) {
      return false;
    }
  }
  return true;
};
var isSelectionAtEndOfNode = exports.isSelectionAtEndOfNode = function isSelectionAtEndOfNode($pos, parentNode) {
  if (!parentNode) {
    return false;
  }
  for (var i = $pos.depth + 1; i > 0; i--) {
    var node = $pos.node(i);
    if (node && node.eq(parentNode.node)) {
      break;
    }
    if (i > 1 && $pos.after(i) !== $pos.after(i - 1) - 1) {
      return false;
    }
  }
  return true;
};
function atTheEndOfDoc(state) {
  var selection = state.selection,
    doc = state.doc;
  return doc.nodeSize - selection.$to.pos - 2 === selection.$to.depth;
}
function atTheBeginningOfDoc(state) {
  var selection = state.selection;
  return selection.$from.pos === selection.$from.depth;
}
function atTheEndOfBlock(state) {
  var selection = state.selection;
  var $to = selection.$to;
  if (selection instanceof _selection.GapCursorSelection) {
    return false;
  }
  if (selection instanceof _state.NodeSelection && selection.node.isBlock) {
    return true;
  }
  return endPositionOfParent($to) === $to.pos + 1;
}
function atTheBeginningOfBlock(state) {
  var selection = state.selection;
  var $from = selection.$from;
  if (selection instanceof _selection.GapCursorSelection) {
    return false;
  }
  if (selection instanceof _state.NodeSelection && selection.node.isBlock) {
    return true;
  }
  return startPositionOfParent($from) === $from.pos;
}
function startPositionOfParent(resolvedPos) {
  return resolvedPos.start(resolvedPos.depth);
}
function endPositionOfParent(resolvedPos) {
  return resolvedPos.end(resolvedPos.depth) + 1;
}