import { __require as requireFindRangesImmutable } from './findRangesImmutable.js';
import { __require as requireGetOwnObjectValues } from './getOwnObjectValues.js';
import { __require as requireImmutable } from '../../../../immutable@3.7.6/node_modules/immutable/dist/immutable.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 BlockTree_1;
var hasRequiredBlockTree;

function requireBlockTree () {
	if (hasRequiredBlockTree) return BlockTree_1;
	hasRequiredBlockTree = 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 _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

	var findRangesImmutable = requireFindRangesImmutable();

	var getOwnObjectValues = requireGetOwnObjectValues();

	var Immutable = requireImmutable();

	var List = Immutable.List,
	    Repeat = Immutable.Repeat,
	    Record = Immutable.Record;

	var returnTrue = function returnTrue() {
	  return true;
	};

	var defaultLeafRange = {
	  start: null,
	  end: null
	};
	var LeafRange = Record(defaultLeafRange);
	var defaultDecoratorRange = {
	  start: null,
	  end: null,
	  decoratorKey: null,
	  leaves: null
	};
	var DecoratorRange = Record(defaultDecoratorRange);
	var BlockTree = {
	  /**
	   * Generate a block tree for a given ContentBlock/decorator pair.
	   */
	  generate: function generate(contentState, block, decorator) {
	    var textLength = block.getLength();

	    if (!textLength) {
	      return List.of(new DecoratorRange({
	        start: 0,
	        end: 0,
	        decoratorKey: null,
	        leaves: List.of(new LeafRange({
	          start: 0,
	          end: 0
	        }))
	      }));
	    }

	    var leafSets = [];
	    var decorations = decorator ? decorator.getDecorations(block, contentState) : List(Repeat(null, textLength));
	    var chars = block.getCharacterList();
	    findRangesImmutable(decorations, areEqual, returnTrue, function (start, end) {
	      leafSets.push(new DecoratorRange({
	        start: start,
	        end: end,
	        decoratorKey: decorations.get(start),
	        leaves: generateLeaves(chars.slice(start, end).toList(), start)
	      }));
	    });
	    return List(leafSets);
	  },
	  fromJS: function fromJS(_ref) {
	    var leaves = _ref.leaves,
	        other = _objectWithoutPropertiesLoose(_ref, ["leaves"]);

	    return new DecoratorRange(_objectSpread({}, other, {
	      leaves: leaves != null ? List(Array.isArray(leaves) ? leaves : getOwnObjectValues(leaves)).map(function (leaf) {
	        return LeafRange(leaf);
	      }) : null
	    }));
	  }
	};
	/**
	 * Generate LeafRange records for a given character list.
	 */

	function generateLeaves(characters, offset) {
	  var leaves = [];
	  var inlineStyles = characters.map(function (c) {
	    return c.getStyle();
	  }).toList();
	  findRangesImmutable(inlineStyles, areEqual, returnTrue, function (start, end) {
	    leaves.push(new LeafRange({
	      start: start + offset,
	      end: end + offset
	    }));
	  });
	  return List(leaves);
	}

	function areEqual(a, b) {
	  return a === b;
	}

	BlockTree_1 = BlockTree;
	return BlockTree_1;
}

export { requireBlockTree as __require };
//# sourceMappingURL=BlockTree.js.map
