'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _reactPortal = require('react-portal');

var _reactPortal2 = _interopRequireDefault(_reactPortal);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _findDomNode = require('../utils/find-dom-node');

var _findDomNode2 = _interopRequireDefault(_findDomNode);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

/**
 * Placeholder.
 */

var Placeholder = function (_React$Component) {
  _inherits(Placeholder, _React$Component);

  function Placeholder() {
    var _Object$getPrototypeO;

    var _temp, _this, _ret;

    _classCallCheck(this, Placeholder);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(Placeholder)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.shouldComponentUpdate = function (props, state) {
      return props.children != _this.props.children || props.className != _this.props.className || props.parent != _this.props.parent || props.node != _this.props.node || props.style != _this.props.style;
    }, _this.isVisible = function () {
      var _this$props = _this.props;
      var onlyFirstChild = _this$props.onlyFirstChild;
      var node = _this$props.node;
      var parent = _this$props.parent;

      if (node.text) return false;
      if (parent.nodes.size > 1) return false;

      var isFirst = parent.nodes.first() === node;
      if (isFirst) return true;

      return false;
    }, _this.onOpen = function (portal) {
      var node = _this.props.node;

      var el = portal.firstChild;
      var nodeEl = (0, _findDomNode2.default)(node);
      var rect = nodeEl.getBoundingClientRect();
      el.style.pointerEvents = 'none';
      el.style.position = 'absolute';
      el.style.top = window.scrollY + rect.top + 'px';
      el.style.left = window.scrollX + rect.left + 'px';
      el.style.width = rect.width + 'px';
      el.style.height = rect.height + 'px';
    }, _this.render = function () {
      var isOpen = _this.isVisible();
      var _this$props2 = _this.props;
      var children = _this$props2.children;
      var className = _this$props2.className;
      var style = _this.props.style;


      if (typeof children === 'string' && style == null && className == null) {
        style = { opacity: '0.333' };
      }

      return _react2.default.createElement(
        _reactPortal2.default,
        { isOpened: isOpen, onOpen: _this.onOpen },
        _react2.default.createElement(
          'span',
          { className: className, style: style },
          children
        )
      );
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  /**
   * Properties.
   */

  /**
   * Should the component update?
   *
   * @param {Object} props
   * @param {Object} state
   * @return {Boolean}
   */

  /**
   * Is the placeholder visible?
   *
   * @return {Boolean}
   */

  /**
   * On open, update the placeholder element's position.
   *
   * @param {Element} portal
   */

  /**
   * Render.
   *
   * If the placeholder is a string, and no `className` or `style` has been
   * passed, give it a default style of lowered opacity.
   *
   * @return {Element} element
   */

  return Placeholder;
}(_react2.default.Component);

/**
 * Export.
 */

Placeholder.propTypes = {
  children: _react2.default.PropTypes.any.isRequired,
  className: _react2.default.PropTypes.string,
  node: _react2.default.PropTypes.object.isRequired,
  parent: _react2.default.PropTypes.object.isRequired,
  state: _react2.default.PropTypes.object.isRequired,
  style: _react2.default.PropTypes.object
};
exports.default = Placeholder;