import { __require as requireDraftModifier } from './DraftModifier.js';
import { __require as requireEditorState } from './EditorState.js';
import { __require as requireGetContentStateFragment } from './getContentStateFragment.js';
import { __require as requireNullthrows } from '../../../../fbjs@2.0.0/node_modules/fbjs/lib/nullthrows.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 SecondaryClipboard_1;
var hasRequiredSecondaryClipboard;

function requireSecondaryClipboard () {
	if (hasRequiredSecondaryClipboard) return SecondaryClipboard_1;
	hasRequiredSecondaryClipboard = 1;

	var DraftModifier = requireDraftModifier();

	var EditorState = requireEditorState();

	var getContentStateFragment = requireGetContentStateFragment();

	var nullthrows = requireNullthrows();

	var clipboard = null;
	/**
	 * Some systems offer a "secondary" clipboard to allow quick internal cut
	 * and paste behavior. For instance, Ctrl+K (cut) and Ctrl+Y (paste).
	 */

	var SecondaryClipboard = {
	  cut: function cut(editorState) {
	    var content = editorState.getCurrentContent();
	    var selection = editorState.getSelection();
	    var targetRange = null;

	    if (selection.isCollapsed()) {
	      var anchorKey = selection.getAnchorKey();
	      var blockEnd = content.getBlockForKey(anchorKey).getLength();

	      if (blockEnd === selection.getAnchorOffset()) {
	        var keyAfter = content.getKeyAfter(anchorKey);

	        if (keyAfter == null) {
	          return editorState;
	        }

	        targetRange = selection.set('focusKey', keyAfter).set('focusOffset', 0);
	      } else {
	        targetRange = selection.set('focusOffset', blockEnd);
	      }
	    } else {
	      targetRange = selection;
	    }

	    targetRange = nullthrows(targetRange); // TODO: This should actually append to the current state when doing
	    // successive ^K commands without any other cursor movement

	    clipboard = getContentStateFragment(content, targetRange);
	    var afterRemoval = DraftModifier.removeRange(content, targetRange, 'forward');

	    if (afterRemoval === content) {
	      return editorState;
	    }

	    return EditorState.push(editorState, afterRemoval, 'remove-range');
	  },
	  paste: function paste(editorState) {
	    if (!clipboard) {
	      return editorState;
	    }

	    var newContent = DraftModifier.replaceWithFragment(editorState.getCurrentContent(), editorState.getSelection(), clipboard);
	    return EditorState.push(editorState, newContent, 'insert-fragment');
	  }
	};
	SecondaryClipboard_1 = SecondaryClipboard;
	return SecondaryClipboard_1;
}

export { requireSecondaryClipboard as __require };
//# sourceMappingURL=SecondaryClipboard.js.map
