import { __require as requireObjectAssign } from '../../../../object-assign@4.1.1/node_modules/object-assign/index.js';
import { __require as requireDraftEditorLeaf_react } from './DraftEditorLeaf.react.js';
import { __require as requireDraftOffsetKey } from './DraftOffsetKey.js';
import React__default from 'react';
import { __require as requireScroll } from '../../../../fbjs@2.0.0/node_modules/fbjs/lib/Scroll.js';
import { __require as requireStyle } from '../../../../fbjs@2.0.0/node_modules/fbjs/lib/Style.js';
import { __require as requireUnicodeBidi } from '../../../../fbjs@2.0.0/node_modules/fbjs/lib/UnicodeBidi.js';
import { __require as requireUnicodeBidiDirection } from '../../../../fbjs@2.0.0/node_modules/fbjs/lib/UnicodeBidiDirection.js';
import { __require as requireCx } from '../../../../fbjs@2.0.0/node_modules/fbjs/lib/cx.js';
import { __require as requireGetElementPosition } from '../../../../fbjs@2.0.0/node_modules/fbjs/lib/getElementPosition.js';
import { __require as requireGetScrollPosition } from '../../../../fbjs@2.0.0/node_modules/fbjs/lib/getScrollPosition.js';
import { __require as requireGetViewportDimensions } from '../../../../fbjs@2.0.0/node_modules/fbjs/lib/getViewportDimensions.js';
import { __require as requireInvariant } from '../../../../fbjs@2.0.0/node_modules/fbjs/lib/invariant.js';
import { __require as requireIsHTMLElement } from './isHTMLElement.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 DraftEditorBlock_react;
var hasRequiredDraftEditorBlock_react;

function requireDraftEditorBlock_react () {
	if (hasRequiredDraftEditorBlock_react) return DraftEditorBlock_react;
	hasRequiredDraftEditorBlock_react = 1;

	var _assign = requireObjectAssign();

	function _extends() { _extends = _assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

	function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

	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 DraftEditorLeaf = requireDraftEditorLeaf_react();

	var DraftOffsetKey = requireDraftOffsetKey();

	var React = React__default;

	var Scroll = requireScroll();

	var Style = requireStyle();

	var UnicodeBidi = requireUnicodeBidi();

	var UnicodeBidiDirection = requireUnicodeBidiDirection();

	var cx = requireCx();

	var getElementPosition = requireGetElementPosition();

	var getScrollPosition = requireGetScrollPosition();

	var getViewportDimensions = requireGetViewportDimensions();

	var invariant = requireInvariant();

	var isHTMLElement = requireIsHTMLElement();

	var nullthrows = requireNullthrows();

	var SCROLL_BUFFER = 10;

	/**
	 * Return whether a block overlaps with either edge of the `SelectionState`.
	 */
	var isBlockOnSelectionEdge = function isBlockOnSelectionEdge(selection, key) {
	  return selection.getAnchorKey() === key || selection.getFocusKey() === key;
	};
	/**
	 * The default block renderer for a `DraftEditor` component.
	 *
	 * A `DraftEditorBlock` is able to render a given `ContentBlock` to its
	 * appropriate decorator and inline style components.
	 */


	var DraftEditorBlock = /*#__PURE__*/function (_React$Component) {
	  _inheritsLoose(DraftEditorBlock, _React$Component);

	  function DraftEditorBlock() {
	    var _this;

	    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
	      args[_key] = arguments[_key];
	    }

	    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;

	    _defineProperty(_assertThisInitialized(_this), "_node", void 0);

	    return _this;
	  }

	  var _proto = DraftEditorBlock.prototype;

	  _proto.shouldComponentUpdate = function shouldComponentUpdate(nextProps) {
	    return this.props.block !== nextProps.block || this.props.tree !== nextProps.tree || this.props.direction !== nextProps.direction || isBlockOnSelectionEdge(nextProps.selection, nextProps.block.getKey()) && nextProps.forceSelection;
	  }
	  /**
	   * When a block is mounted and overlaps the selection state, we need to make
	   * sure that the cursor is visible to match native behavior. This may not
	   * be the case if the user has pressed `RETURN` or pasted some content, since
	   * programmatically creating these new blocks and setting the DOM selection
	   * will miss out on the browser natively scrolling to that position.
	   *
	   * To replicate native behavior, if the block overlaps the selection state
	   * on mount, force the scroll position. Check the scroll state of the scroll
	   * parent, and adjust it to align the entire block to the bottom of the
	   * scroll parent.
	   */
	  ;

	  _proto.componentDidMount = function componentDidMount() {
	    if (this.props.preventScroll) {
	      return;
	    }

	    var selection = this.props.selection;
	    var endKey = selection.getEndKey();

	    if (!selection.getHasFocus() || endKey !== this.props.block.getKey()) {
	      return;
	    }

	    var blockNode = this._node;

	    if (blockNode == null) {
	      return;
	    }

	    var scrollParent = Style.getScrollParent(blockNode);
	    var scrollPosition = getScrollPosition(scrollParent);
	    var scrollDelta;

	    if (scrollParent === window) {
	      var nodePosition = getElementPosition(blockNode);
	      var nodeBottom = nodePosition.y + nodePosition.height;
	      var viewportHeight = getViewportDimensions().height;
	      scrollDelta = nodeBottom - viewportHeight;

	      if (scrollDelta > 0) {
	        window.scrollTo(scrollPosition.x, scrollPosition.y + scrollDelta + SCROLL_BUFFER);
	      }
	    } else {
	      !isHTMLElement(blockNode) ? process.env.NODE_ENV !== "production" ? invariant(false, 'blockNode is not an HTMLElement') : invariant(false) : void 0;
	      var blockBottom = blockNode.offsetHeight + blockNode.offsetTop;
	      var pOffset = scrollParent.offsetTop + scrollParent.offsetHeight;
	      var scrollBottom = pOffset + scrollPosition.y;
	      scrollDelta = blockBottom - scrollBottom;

	      if (scrollDelta > 0) {
	        Scroll.setTop(scrollParent, Scroll.getTop(scrollParent) + scrollDelta + SCROLL_BUFFER);
	      }
	    }
	  };

	  _proto._renderChildren = function _renderChildren() {
	    var _this2 = this;

	    var block = this.props.block;
	    var blockKey = block.getKey();
	    var text = block.getText();
	    var lastLeafSet = this.props.tree.size - 1;
	    var hasSelection = isBlockOnSelectionEdge(this.props.selection, blockKey);
	    return this.props.tree.map(function (leafSet, ii) {
	      var leavesForLeafSet = leafSet.get('leaves'); // T44088704

	      if (leavesForLeafSet.size === 0) {
	        return null;
	      }

	      var lastLeaf = leavesForLeafSet.size - 1;
	      var leaves = leavesForLeafSet.map(function (leaf, jj) {
	        var offsetKey = DraftOffsetKey.encode(blockKey, ii, jj);
	        var start = leaf.get('start');
	        var end = leaf.get('end');
	        return React.createElement(DraftEditorLeaf, {
	          key: offsetKey,
	          offsetKey: offsetKey,
	          block: block,
	          start: start,
	          selection: hasSelection ? _this2.props.selection : null,
	          forceSelection: _this2.props.forceSelection,
	          text: text.slice(start, end),
	          styleSet: block.getInlineStyleAt(start),
	          customStyleMap: _this2.props.customStyleMap,
	          customStyleFn: _this2.props.customStyleFn,
	          isLast: ii === lastLeafSet && jj === lastLeaf
	        });
	      }).toArray();
	      var decoratorKey = leafSet.get('decoratorKey');

	      if (decoratorKey == null) {
	        return leaves;
	      }

	      if (!_this2.props.decorator) {
	        return leaves;
	      }

	      var decorator = nullthrows(_this2.props.decorator);
	      var DecoratorComponent = decorator.getComponentForKey(decoratorKey);

	      if (!DecoratorComponent) {
	        return leaves;
	      }

	      var decoratorProps = decorator.getPropsForKey(decoratorKey);
	      var decoratorOffsetKey = DraftOffsetKey.encode(blockKey, ii, 0);
	      var start = leavesForLeafSet.first().get('start');
	      var end = leavesForLeafSet.last().get('end');
	      var decoratedText = text.slice(start, end);
	      var entityKey = block.getEntityAt(leafSet.get('start')); // Resetting dir to the same value on a child node makes Chrome/Firefox
	      // confused on cursor movement. See http://jsfiddle.net/d157kLck/3/

	      var dir = UnicodeBidiDirection.getHTMLDirIfDifferent(UnicodeBidi.getDirection(decoratedText), _this2.props.direction);
	      var commonProps = {
	        contentState: _this2.props.contentState,
	        decoratedText: decoratedText,
	        dir: dir,
	        start: start,
	        end: end,
	        blockKey: blockKey,
	        entityKey: entityKey,
	        offsetKey: decoratorOffsetKey
	      };
	      return React.createElement(DecoratorComponent, _extends({}, decoratorProps, commonProps, {
	        key: decoratorOffsetKey
	      }), leaves);
	    }).toArray();
	  };

	  _proto.render = function render() {
	    var _this3 = this;

	    var _this$props = this.props,
	        direction = _this$props.direction,
	        offsetKey = _this$props.offsetKey;
	    var className = cx({
	      'public/DraftStyleDefault/block': true,
	      'public/DraftStyleDefault/ltr': direction === 'LTR',
	      'public/DraftStyleDefault/rtl': direction === 'RTL'
	    });
	    return React.createElement("div", {
	      "data-offset-key": offsetKey,
	      className: className,
	      ref: function ref(_ref) {
	        return _this3._node = _ref;
	      }
	    }, this._renderChildren());
	  };

	  return DraftEditorBlock;
	}(React.Component);

	DraftEditorBlock_react = DraftEditorBlock;
	return DraftEditorBlock_react;
}

export { requireDraftEditorBlock_react as __require };
//# sourceMappingURL=DraftEditorBlock.react.js.map
