import { __require as requireRandomizeBlockMapKeys } from './randomizeBlockMapKeys.js';
import { __require as requireRemoveEntitiesAtEdges } from './removeEntitiesAtEdges.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 getContentStateFragment_1;
var hasRequiredGetContentStateFragment;

function requireGetContentStateFragment () {
	if (hasRequiredGetContentStateFragment) return getContentStateFragment_1;
	hasRequiredGetContentStateFragment = 1;

	var randomizeBlockMapKeys = requireRandomizeBlockMapKeys();

	var removeEntitiesAtEdges = requireRemoveEntitiesAtEdges();

	var getContentStateFragment = function getContentStateFragment(contentState, selectionState) {
	  var startKey = selectionState.getStartKey();
	  var startOffset = selectionState.getStartOffset();
	  var endKey = selectionState.getEndKey();
	  var endOffset = selectionState.getEndOffset(); // Edge entities should be stripped to ensure that we don't preserve
	  // invalid partial entities when the fragment is reused. We do, however,
	  // preserve entities that are entirely within the selection range.

	  var contentWithoutEdgeEntities = removeEntitiesAtEdges(contentState, selectionState);
	  var blockMap = contentWithoutEdgeEntities.getBlockMap();
	  var blockKeys = blockMap.keySeq();
	  var startIndex = blockKeys.indexOf(startKey);
	  var endIndex = blockKeys.indexOf(endKey) + 1;
	  return randomizeBlockMapKeys(blockMap.slice(startIndex, endIndex).map(function (block, blockKey) {
	    var text = block.getText();
	    var chars = block.getCharacterList();

	    if (startKey === endKey) {
	      return block.merge({
	        text: text.slice(startOffset, endOffset),
	        characterList: chars.slice(startOffset, endOffset)
	      });
	    }

	    if (blockKey === startKey) {
	      return block.merge({
	        text: text.slice(startOffset),
	        characterList: chars.slice(startOffset)
	      });
	    }

	    if (blockKey === endKey) {
	      return block.merge({
	        text: text.slice(0, endOffset),
	        characterList: chars.slice(0, endOffset)
	      });
	    }

	    return block;
	  }));
	};

	getContentStateFragment_1 = getContentStateFragment;
	return getContentStateFragment_1;
}

export { requireGetContentStateFragment as __require };
//# sourceMappingURL=getContentStateFragment.js.map
