import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
import { GapCursorSelection } from './gap-cursor/selection';
export var 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;
};
export var 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;
};
export function atTheEndOfDoc(state) {
  var selection = state.selection,
    doc = state.doc;
  return doc.nodeSize - selection.$to.pos - 2 === selection.$to.depth;
}
export function atTheBeginningOfDoc(state) {
  var selection = state.selection;
  return selection.$from.pos === selection.$from.depth;
}
export function atTheEndOfBlock(state) {
  var selection = state.selection;
  var $to = selection.$to;
  if (selection instanceof GapCursorSelection) {
    return false;
  }
  if (selection instanceof NodeSelection && selection.node.isBlock) {
    return true;
  }
  return endPositionOfParent($to) === $to.pos + 1;
}
export function atTheBeginningOfBlock(state) {
  var selection = state.selection;
  var $from = selection.$from;
  if (selection instanceof GapCursorSelection) {
    return false;
  }
  if (selection instanceof NodeSelection && selection.node.isBlock) {
    return true;
  }
  return startPositionOfParent($from) === $from.pos;
}
export function startPositionOfParent(resolvedPos) {
  return resolvedPos.start(resolvedPos.depth);
}
export function endPositionOfParent(resolvedPos) {
  return resolvedPos.end(resolvedPos.depth) + 1;
}