/**
 *
 * @param $anchor Resolved selection anchor position
 * @param $head Resolved selection head position
 * @returns An expanded selection encompassing surrounding nodes. Hoists up to the shared depth anchor/head depths differ.
 */
export var expandSelectionBounds = function expandSelectionBounds($anchor, $head) {
  var sharedDepth = $anchor.sharedDepth($head.pos) + 1;
  var $from = $anchor.min($head);
  var $to = $anchor.max($head);
  var fromDepth = $from.depth;
  var toDepth = $to.depth;
  var selectionStart;
  var selectionEnd;
  var selectionHasGrandparent = toDepth > 1 && fromDepth > 1;
  var selectionIsAcrossDiffParents = selectionHasGrandparent && !$to.parent.isTextblock && !$to.sameParent($from);
  var selectionIsAcrossTextBlocksWithDiffParents = selectionHasGrandparent && $to.parent.isTextblock && $to.before(toDepth - 1) !== $from.before(fromDepth - 1);
  if (toDepth > fromDepth) {
    selectionStart = $from.before(sharedDepth);
    selectionEnd = $to.after(sharedDepth);
  } else if (toDepth < fromDepth) {
    selectionStart = $from.before(sharedDepth);
    selectionEnd = $to.after(sharedDepth);
  } else if (selectionIsAcrossDiffParents || selectionIsAcrossTextBlocksWithDiffParents) {
    // when selection from/to share same depth with different parents, hoist up the selection to the parent of the highest depth in the selection
    selectionStart = $from.before(sharedDepth);
    selectionEnd = $to.after(sharedDepth);
  } else if (!$from.node().inlineContent) {
    // when selection might be a Node selection, return what was passed in
    return {
      $anchor: $anchor,
      $head: $head
    };
  } else {
    selectionStart = fromDepth ? $from.before() : $from.pos;
    selectionEnd = toDepth ? $to.after() : $to.pos;
  }
  var $expandedFrom = $anchor.doc.resolve(selectionStart);
  var $expandedTo = $anchor.doc.resolve(selectionEnd);
  return {
    $anchor: $anchor === $from ? $expandedFrom : $expandedTo,
    $head: $head === $to ? $expandedTo : $expandedFrom
  };
};