import { __require as requireContentBlockNode } from './ContentBlockNode.js';
import { __require as requireGenerateRandomKey } from './generateRandomKey.js';
import { __require as requireImmutable } from '../../../../immutable@3.7.6/node_modules/immutable/dist/immutable.js';
import { __require as requireInvariant } from '../../../../fbjs@2.0.0/node_modules/fbjs/lib/invariant.js';
import { __require as requireModifyBlockForContentState } from './modifyBlockForContentState.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 splitBlockInContentState_1;
var hasRequiredSplitBlockInContentState;

function requireSplitBlockInContentState () {
	if (hasRequiredSplitBlockInContentState) return splitBlockInContentState_1;
	hasRequiredSplitBlockInContentState = 1;

	var ContentBlockNode = requireContentBlockNode();

	var generateRandomKey = requireGenerateRandomKey();

	var Immutable = requireImmutable();

	var invariant = requireInvariant();

	var modifyBlockForContentState = requireModifyBlockForContentState();

	var List = Immutable.List,
	    Map = Immutable.Map;

	var transformBlock = function transformBlock(key, blockMap, func) {
	  if (!key) {
	    return;
	  }

	  var block = blockMap.get(key);

	  if (!block) {
	    return;
	  }

	  blockMap.set(key, func(block));
	};

	var updateBlockMapLinks = function updateBlockMapLinks(blockMap, originalBlock, belowBlock) {
	  return blockMap.withMutations(function (blocks) {
	    var originalBlockKey = originalBlock.getKey();
	    var belowBlockKey = belowBlock.getKey(); // update block parent

	    transformBlock(originalBlock.getParentKey(), blocks, function (block) {
	      var parentChildrenList = block.getChildKeys();
	      var insertionIndex = parentChildrenList.indexOf(originalBlockKey) + 1;
	      var newChildrenArray = parentChildrenList.toArray();
	      newChildrenArray.splice(insertionIndex, 0, belowBlockKey);
	      return block.merge({
	        children: List(newChildrenArray)
	      });
	    }); // update original next block

	    transformBlock(originalBlock.getNextSiblingKey(), blocks, function (block) {
	      return block.merge({
	        prevSibling: belowBlockKey
	      });
	    }); // update original block

	    transformBlock(originalBlockKey, blocks, function (block) {
	      return block.merge({
	        nextSibling: belowBlockKey
	      });
	    }); // update below block

	    transformBlock(belowBlockKey, blocks, function (block) {
	      return block.merge({
	        prevSibling: originalBlockKey
	      });
	    });
	  });
	};

	var splitBlockInContentState = function splitBlockInContentState(contentState, selectionState) {
	  !selectionState.isCollapsed() ? process.env.NODE_ENV !== "production" ? invariant(false, 'Selection range must be collapsed.') : invariant(false) : void 0;
	  var key = selectionState.getAnchorKey();
	  var blockMap = contentState.getBlockMap();
	  var blockToSplit = blockMap.get(key);
	  var text = blockToSplit.getText();

	  if (!text) {
	    var blockType = blockToSplit.getType();

	    if (blockType === 'unordered-list-item' || blockType === 'ordered-list-item') {
	      return modifyBlockForContentState(contentState, selectionState, function (block) {
	        return block.merge({
	          type: 'unstyled',
	          depth: 0
	        });
	      });
	    }
	  }

	  var offset = selectionState.getAnchorOffset();
	  var chars = blockToSplit.getCharacterList();
	  var keyBelow = generateRandomKey();
	  var isExperimentalTreeBlock = blockToSplit instanceof ContentBlockNode;
	  var blockAbove = blockToSplit.merge({
	    text: text.slice(0, offset),
	    characterList: chars.slice(0, offset)
	  });
	  var blockBelow = blockAbove.merge({
	    key: keyBelow,
	    text: text.slice(offset),
	    characterList: chars.slice(offset),
	    data: Map()
	  });
	  var blocksBefore = blockMap.toSeq().takeUntil(function (v) {
	    return v === blockToSplit;
	  });
	  var blocksAfter = blockMap.toSeq().skipUntil(function (v) {
	    return v === blockToSplit;
	  }).rest();
	  var newBlocks = blocksBefore.concat([[key, blockAbove], [keyBelow, blockBelow]], blocksAfter).toOrderedMap();

	  if (isExperimentalTreeBlock) {
	    !blockToSplit.getChildKeys().isEmpty() ? process.env.NODE_ENV !== "production" ? invariant(false, 'ContentBlockNode must not have children') : invariant(false) : void 0;
	    newBlocks = updateBlockMapLinks(newBlocks, blockAbove, blockBelow);
	  }

	  return contentState.merge({
	    blockMap: newBlocks,
	    selectionBefore: selectionState,
	    selectionAfter: selectionState.merge({
	      anchorKey: keyBelow,
	      anchorOffset: 0,
	      focusKey: keyBelow,
	      focusOffset: 0,
	      isBackward: false
	    })
	  });
	};

	splitBlockInContentState_1 = splitBlockInContentState;
	return splitBlockInContentState_1;
}

export { requireSplitBlockInContentState as __require };
//# sourceMappingURL=splitBlockInContentState.js.map
