import { __require as requireBlockMapBuilder } from './BlockMapBuilder.js';
import { __require as requireCharacterMetadata } from './CharacterMetadata.js';
import { __require as requireContentBlock } from './ContentBlock.js';
import { __require as requireContentBlockNode } from './ContentBlockNode.js';
import { __require as requireDraftModifier } from './DraftModifier.js';
import { __require as requireEditorState } from './EditorState.js';
import { __require as requireGenerateRandomKey } from './generateRandomKey.js';
import { __require as requireGkx } from './gkx.js';
import { __require as requireImmutable } from '../../../../immutable@3.7.6/node_modules/immutable/dist/immutable.js';
import { __require as requireMoveBlockInContentState } from './moveBlockInContentState.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 AtomicBlockUtils_1;
var hasRequiredAtomicBlockUtils;

function requireAtomicBlockUtils () {
	if (hasRequiredAtomicBlockUtils) return AtomicBlockUtils_1;
	hasRequiredAtomicBlockUtils = 1;

	function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

	function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

	var BlockMapBuilder = requireBlockMapBuilder();

	var CharacterMetadata = requireCharacterMetadata();

	var ContentBlock = requireContentBlock();

	var ContentBlockNode = requireContentBlockNode();

	var DraftModifier = requireDraftModifier();

	var EditorState = requireEditorState();

	var generateRandomKey = requireGenerateRandomKey();

	var gkx = requireGkx();

	var Immutable = requireImmutable();

	var moveBlockInContentState = requireMoveBlockInContentState();

	var experimentalTreeDataSupport = gkx('draft_tree_data_support');
	var ContentBlockRecord = experimentalTreeDataSupport ? ContentBlockNode : ContentBlock;
	var List = Immutable.List,
	    Repeat = Immutable.Repeat;
	var AtomicBlockUtils = {
	  insertAtomicBlock: function insertAtomicBlock(editorState, entityKey, character) {
	    var contentState = editorState.getCurrentContent();
	    var selectionState = editorState.getSelection();
	    var afterRemoval = DraftModifier.removeRange(contentState, selectionState, 'backward');
	    var targetSelection = afterRemoval.getSelectionAfter();
	    var afterSplit = DraftModifier.splitBlock(afterRemoval, targetSelection);
	    var insertionTarget = afterSplit.getSelectionAfter();
	    var asAtomicBlock = DraftModifier.setBlockType(afterSplit, insertionTarget, 'atomic');
	    var charData = CharacterMetadata.create({
	      entity: entityKey
	    });
	    var atomicBlockConfig = {
	      key: generateRandomKey(),
	      type: 'atomic',
	      text: character,
	      characterList: List(Repeat(charData, character.length))
	    };
	    var atomicDividerBlockConfig = {
	      key: generateRandomKey(),
	      type: 'unstyled'
	    };

	    if (experimentalTreeDataSupport) {
	      atomicBlockConfig = _objectSpread({}, atomicBlockConfig, {
	        nextSibling: atomicDividerBlockConfig.key
	      });
	      atomicDividerBlockConfig = _objectSpread({}, atomicDividerBlockConfig, {
	        prevSibling: atomicBlockConfig.key
	      });
	    }

	    var fragmentArray = [new ContentBlockRecord(atomicBlockConfig), new ContentBlockRecord(atomicDividerBlockConfig)];
	    var fragment = BlockMapBuilder.createFromArray(fragmentArray);
	    var withAtomicBlock = DraftModifier.replaceWithFragment(asAtomicBlock, insertionTarget, fragment);
	    var newContent = withAtomicBlock.merge({
	      selectionBefore: selectionState,
	      selectionAfter: withAtomicBlock.getSelectionAfter().set('hasFocus', true)
	    });
	    return EditorState.push(editorState, newContent, 'insert-fragment');
	  },
	  moveAtomicBlock: function moveAtomicBlock(editorState, atomicBlock, targetRange, insertionMode) {
	    var contentState = editorState.getCurrentContent();
	    var selectionState = editorState.getSelection();
	    var withMovedAtomicBlock;

	    if (insertionMode === 'before' || insertionMode === 'after') {
	      var targetBlock = contentState.getBlockForKey(insertionMode === 'before' ? targetRange.getStartKey() : targetRange.getEndKey());
	      withMovedAtomicBlock = moveBlockInContentState(contentState, atomicBlock, targetBlock, insertionMode);
	    } else {
	      var afterRemoval = DraftModifier.removeRange(contentState, targetRange, 'backward');
	      var selectionAfterRemoval = afterRemoval.getSelectionAfter();

	      var _targetBlock = afterRemoval.getBlockForKey(selectionAfterRemoval.getFocusKey());

	      if (selectionAfterRemoval.getStartOffset() === 0) {
	        withMovedAtomicBlock = moveBlockInContentState(afterRemoval, atomicBlock, _targetBlock, 'before');
	      } else if (selectionAfterRemoval.getEndOffset() === _targetBlock.getLength()) {
	        withMovedAtomicBlock = moveBlockInContentState(afterRemoval, atomicBlock, _targetBlock, 'after');
	      } else {
	        var afterSplit = DraftModifier.splitBlock(afterRemoval, selectionAfterRemoval);
	        var selectionAfterSplit = afterSplit.getSelectionAfter();

	        var _targetBlock2 = afterSplit.getBlockForKey(selectionAfterSplit.getFocusKey());

	        withMovedAtomicBlock = moveBlockInContentState(afterSplit, atomicBlock, _targetBlock2, 'before');
	      }
	    }

	    var newContent = withMovedAtomicBlock.merge({
	      selectionBefore: selectionState,
	      selectionAfter: withMovedAtomicBlock.getSelectionAfter().set('hasFocus', true)
	    });
	    return EditorState.push(editorState, newContent, 'move-block');
	  }
	};
	AtomicBlockUtils_1 = AtomicBlockUtils;
	return AtomicBlockUtils_1;
}

export { requireAtomicBlockUtils as __require };
//# sourceMappingURL=AtomicBlockUtils.js.map
