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 requireDraftEntity } from './DraftEntity.js';
import { __require as requireSelectionState } from './SelectionState.js';
import { __require as requireGenerateRandomKey } from './generateRandomKey.js';
import { __require as requireGetOwnObjectValues } from './getOwnObjectValues.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 requireSanitizeDraftText } from './sanitizeDraftText.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 ContentState_1;
var hasRequiredContentState;

function requireContentState () {
	if (hasRequiredContentState) return ContentState_1;
	hasRequiredContentState = 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; }

	function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }

	var BlockMapBuilder = requireBlockMapBuilder();

	var CharacterMetadata = requireCharacterMetadata();

	var ContentBlock = requireContentBlock();

	var ContentBlockNode = requireContentBlockNode();

	var DraftEntity = requireDraftEntity();

	var SelectionState = requireSelectionState();

	var generateRandomKey = requireGenerateRandomKey();

	var getOwnObjectValues = requireGetOwnObjectValues();

	var gkx = requireGkx();

	var Immutable = requireImmutable();

	var sanitizeDraftText = requireSanitizeDraftText();

	var List = Immutable.List,
	    Record = Immutable.Record,
	    Repeat = Immutable.Repeat,
	    ImmutableMap = Immutable.Map,
	    OrderedMap = Immutable.OrderedMap;
	var defaultRecord = {
	  entityMap: null,
	  blockMap: null,
	  selectionBefore: null,
	  selectionAfter: null
	};
	var ContentStateRecord = Record(defaultRecord);
	/* $FlowFixMe[signature-verification-failure] Supressing a `signature-
	 * verification-failure` error here. TODO: T65949050 Clean up the branch for
	 * this GK */

	var ContentBlockNodeRecord = gkx('draft_tree_data_support') ? ContentBlockNode : ContentBlock;

	var ContentState = /*#__PURE__*/function (_ContentStateRecord) {
	  _inheritsLoose(ContentState, _ContentStateRecord);

	  function ContentState() {
	    return _ContentStateRecord.apply(this, arguments) || this;
	  }

	  var _proto = ContentState.prototype;

	  _proto.getEntityMap = function getEntityMap() {
	    // TODO: update this when we fully remove DraftEntity
	    return DraftEntity;
	  };

	  _proto.getBlockMap = function getBlockMap() {
	    return this.get('blockMap');
	  };

	  _proto.getSelectionBefore = function getSelectionBefore() {
	    return this.get('selectionBefore');
	  };

	  _proto.getSelectionAfter = function getSelectionAfter() {
	    return this.get('selectionAfter');
	  };

	  _proto.getBlockForKey = function getBlockForKey(key) {
	    var block = this.getBlockMap().get(key);
	    return block;
	  };

	  _proto.getKeyBefore = function getKeyBefore(key) {
	    return this.getBlockMap().reverse().keySeq().skipUntil(function (v) {
	      return v === key;
	    }).skip(1).first();
	  };

	  _proto.getKeyAfter = function getKeyAfter(key) {
	    return this.getBlockMap().keySeq().skipUntil(function (v) {
	      return v === key;
	    }).skip(1).first();
	  };

	  _proto.getBlockAfter = function getBlockAfter(key) {
	    return this.getBlockMap().skipUntil(function (_, k) {
	      return k === key;
	    }).skip(1).first();
	  };

	  _proto.getBlockBefore = function getBlockBefore(key) {
	    return this.getBlockMap().reverse().skipUntil(function (_, k) {
	      return k === key;
	    }).skip(1).first();
	  };

	  _proto.getBlocksAsArray = function getBlocksAsArray() {
	    return this.getBlockMap().toArray();
	  };

	  _proto.getFirstBlock = function getFirstBlock() {
	    return this.getBlockMap().first();
	  };

	  _proto.getLastBlock = function getLastBlock() {
	    return this.getBlockMap().last();
	  };

	  _proto.getPlainText = function getPlainText(delimiter) {
	    return this.getBlockMap().map(function (block) {
	      return block ? block.getText() : '';
	    }).join(delimiter || '\n');
	  };

	  _proto.getLastCreatedEntityKey = function getLastCreatedEntityKey() {
	    // TODO: update this when we fully remove DraftEntity
	    return DraftEntity.__getLastCreatedEntityKey();
	  };

	  _proto.hasText = function hasText() {
	    var blockMap = this.getBlockMap();
	    return blockMap.size > 1 || // make sure that there are no zero width space chars
	    escape(blockMap.first().getText()).replace(/%u200B/g, '').length > 0;
	  };

	  _proto.createEntity = function createEntity(type, mutability, data) {
	    // TODO: update this when we fully remove DraftEntity
	    DraftEntity.__create(type, mutability, data);

	    return this;
	  };

	  _proto.mergeEntityData = function mergeEntityData(key, toMerge) {
	    // TODO: update this when we fully remove DraftEntity
	    DraftEntity.__mergeData(key, toMerge);

	    return this;
	  };

	  _proto.replaceEntityData = function replaceEntityData(key, newData) {
	    // TODO: update this when we fully remove DraftEntity
	    DraftEntity.__replaceData(key, newData);

	    return this;
	  };

	  _proto.addEntity = function addEntity(instance) {
	    // TODO: update this when we fully remove DraftEntity
	    DraftEntity.__add(instance);

	    return this;
	  };

	  _proto.getEntity = function getEntity(key) {
	    // TODO: update this when we fully remove DraftEntity
	    return DraftEntity.__get(key);
	  };

	  _proto.getAllEntities = function getAllEntities() {
	    return DraftEntity.__getAll();
	  };

	  _proto.loadWithEntities = function loadWithEntities(entities) {
	    return DraftEntity.__loadWithEntities(entities);
	  };

	  ContentState.createFromBlockArray = function createFromBlockArray( // TODO: update flow type when we completely deprecate the old entity API
	  blocks, entityMap) {
	    // TODO: remove this when we completely deprecate the old entity API
	    var theBlocks = Array.isArray(blocks) ? blocks : blocks.contentBlocks;
	    var blockMap = BlockMapBuilder.createFromArray(theBlocks);
	    var selectionState = blockMap.isEmpty() ? new SelectionState() : SelectionState.createEmpty(blockMap.first().getKey());
	    return new ContentState({
	      blockMap: blockMap,
	      entityMap: entityMap || DraftEntity,
	      selectionBefore: selectionState,
	      selectionAfter: selectionState
	    });
	  };

	  ContentState.createFromText = function createFromText(text) {
	    var delimiter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : /\r\n?|\n/g;
	    var strings = text.split(delimiter);
	    var blocks = strings.map(function (block) {
	      block = sanitizeDraftText(block);
	      return new ContentBlockNodeRecord({
	        key: generateRandomKey(),
	        text: block,
	        type: 'unstyled',
	        characterList: List(Repeat(CharacterMetadata.EMPTY, block.length))
	      });
	    });
	    return ContentState.createFromBlockArray(blocks);
	  };

	  ContentState.fromJS = function fromJS(state) {
	    return new ContentState(_objectSpread({}, state, {
	      blockMap: OrderedMap(state.blockMap).map(ContentState.createContentBlockFromJS),
	      selectionBefore: new SelectionState(state.selectionBefore),
	      selectionAfter: new SelectionState(state.selectionAfter)
	    }));
	  };

	  ContentState.createContentBlockFromJS = function createContentBlockFromJS(block) {
	    var characterList = block.characterList;
	    return new ContentBlockNodeRecord(_objectSpread({}, block, {
	      data: ImmutableMap(block.data),
	      characterList: characterList != null ? List((Array.isArray(characterList) ? characterList : getOwnObjectValues(characterList)).map(function (c) {
	        return CharacterMetadata.fromJS(c);
	      })) : undefined
	    }));
	  };

	  return ContentState;
	}(ContentStateRecord);

	ContentState_1 = ContentState;
	return ContentState_1;
}

export { requireContentState as __require };
//# sourceMappingURL=ContentState.js.map
