'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _bind = require('classnames/bind');

var _bind2 = _interopRequireDefault(_bind);

var _style = require('./style.scss');

var _style2 = _interopRequireDefault(_style);

var _RenderToLayer = require('../internal/RenderToLayer');

var _RenderToLayer2 = _interopRequireDefault(_RenderToLayer);

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; }

/**
 * The Tooltip component.
 */

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

  function Tooltip(props) {
    _classCallCheck(this, Tooltip);

    var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Tooltip).call(this, props));

    _this.state = {
      showing: false
    };

    _this.showTooltip = function () {
      _this.setState({ showing: true });
    };

    _this.hideTooltip = function () {
      _this.setState({ showing: false });
    };

    _this.componentWillUpdate = function () {
      var triggerRect = _this._triggerElement.getBoundingClientRect();
      _this._tooltipPlacement = {};

      switch (_this.props.tooltipPlacement) {
        case 'bottom':
          _this._tooltipPlacement.left = triggerRect.left + (triggerRect.right - triggerRect.left) / 2;
          _this._tooltipPlacement.top = triggerRect.bottom;
          break;
        case 'right':
          _this._tooltipPlacement.left = triggerRect.right;
          _this._tooltipPlacement.top = triggerRect.top + (triggerRect.bottom - triggerRect.top) / 2;
          break;
        case 'left':
          _this._tooltipPlacement.left = triggerRect.left;
          _this._tooltipPlacement.top = triggerRect.top + (triggerRect.bottom - triggerRect.top) / 2;
          break;
        default:
          _this._tooltipPlacement.left = triggerRect.left + (triggerRect.right - triggerRect.left) / 2;
          _this._tooltipPlacement.top = triggerRect.top;
      }
    };

    _this.getStyles = function () {
      var style = {};

      if (_this.state.showing) {
        style.top = _this._tooltipPlacement.top + window.pageYOffset;
        style.left = _this._tooltipPlacement.left + window.pageXOffset;
        style.opacity = 0.9;
      }

      return style;
    };

    _this.renderTooltip = function () {
      var cx = _bind2.default.bind(_style2.default);
      var tooltipShowingClass = _this.state.showing ? _style2.default['tooltip-showing'] : '';
      var tooltipClass = cx(_style2.default['tooltip-component'], _this.props.optClass, tooltipShowingClass, _style2.default[_this.props.tooltipPlacement]);
      var styles = _this.getStyles();

      return _react2.default.createElement(
        'span',
        { className: tooltipClass, style: styles },
        _this.props.content
      );
    };

    return _this;
  }

  _createClass(Tooltip, [{
    key: 'render',
    value: function render() {
      var _this2 = this;

      return _react2.default.createElement(
        'span',
        { onMouseOver: this.showTooltip, onMouseOut: this.hideTooltip, ref: function ref(c) {
            return _this2._triggerElement = c;
          } },
        this.props.children,
        this.props.appendToBody ? _react2.default.createElement(_RenderToLayer2.default, { render: this.renderTooltip, open: true }) : this.renderTooltip()
      );
    }
  }]);

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

Tooltip.defaultProps = {
  tooltipPlacement: 'top'
};
Tooltip.propTypes = {
  /**
   * The content to display inside the `Tooltip`. Could be number, string, element or an array containing these types.
   */
  content: _react2.default.PropTypes.node,
  /**
   * Optional styles to add to the tooltip.
   */
  optClass: _react2.default.PropTypes.string,
  /**
   * The placement of the tooltip.
   */
  tooltipPlacement: _react2.default.PropTypes.oneOf(['left', 'right', 'top', 'bottom']),
  /**
   * Whether to insert the tooltip element after the trigger element or append it to the document body.
   */
  appendToBody: _react2.default.PropTypes.bool
};
exports.default = Tooltip;