import { __require as requireWarning } from '../../../../fbjs@2.0.0/node_modules/fbjs/lib/warning.js';

/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @format
 * 
 * @emails oncall+draft_js
 */

var moveSelectionBackward_1;
var hasRequiredMoveSelectionBackward;

function requireMoveSelectionBackward () {
	if (hasRequiredMoveSelectionBackward) return moveSelectionBackward_1;
	hasRequiredMoveSelectionBackward = 1;

	var warning = requireWarning();
	/**
	 * Given a collapsed selection, move the focus `maxDistance` backward within
	 * the selected block. If the selection will go beyond the start of the block,
	 * move focus to the end of the previous block, but no further.
	 *
	 * This function is not Unicode-aware, so surrogate pairs will be treated
	 * as having length 2.
	 */


	function moveSelectionBackward(editorState, maxDistance) {
	  var selection = editorState.getSelection(); // Should eventually make this an invariant

	  process.env.NODE_ENV !== "production" ? warning(selection.isCollapsed(), 'moveSelectionBackward should only be called with a collapsed SelectionState') : void 0;
	  var content = editorState.getCurrentContent();
	  var key = selection.getStartKey();
	  var offset = selection.getStartOffset();
	  var focusKey = key;
	  var focusOffset = 0;

	  if (maxDistance > offset) {
	    var keyBefore = content.getKeyBefore(key);

	    if (keyBefore == null) {
	      focusKey = key;
	    } else {
	      focusKey = keyBefore;
	      var blockBefore = content.getBlockForKey(keyBefore);
	      focusOffset = blockBefore.getText().length;
	    }
	  } else {
	    focusOffset = offset - maxDistance;
	  }

	  return selection.merge({
	    focusKey: focusKey,
	    focusOffset: focusOffset,
	    isBackward: true
	  });
	}

	moveSelectionBackward_1 = moveSelectionBackward;
	return moveSelectionBackward_1;
}

export { requireMoveSelectionBackward as __require };
//# sourceMappingURL=moveSelectionBackward.js.map
