'use strict';

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

var _extends = Object.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; };

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 _reactBootstrap = require('react-bootstrap');

var _uuid = require('uuid');

var _uuid2 = _interopRequireDefault(_uuid);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _TooltipTrigger = require('./TooltipTrigger.scss');

var _TooltipTrigger2 = _interopRequireDefault(_TooltipTrigger);

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

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 getTooltipClass() {
	var _classNames;

	return (0, _classnames2.default)((_classNames = {}, _defineProperty(_classNames, _TooltipTrigger2.default['tooltip-container'], true), _defineProperty(_classNames, 'tooltip-container', true), _classNames));
}

/**
 * @param {object} props react props
 * @example
const props = {
	label: 'Help content here',
	tooltipPlacement: 'top',
};
<TooltipTrigger {...props} >
	<Icon name="my-icon" />
</TooltipTrigger>
 */

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

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

		var _this = _possibleConstructorReturn(this, (TooltipTrigger.__proto__ || Object.getPrototypeOf(TooltipTrigger)).call(this, props));

		_this.onMouseOver = _this.onMouseOver.bind(_this);
		_this.state = {
			hovered: false,
			id: _uuid2.default.v4()
		};
		return _this;
	}

	_createClass(TooltipTrigger, [{
		key: 'shouldComponentUpdate',
		value: function shouldComponentUpdate(nextProps, nextState) {
			return this.state.hovered !== nextState.hovered || this.props.children !== nextProps.children || this.props.label !== nextProps.label;
		}
	}, {
		key: 'onMouseOver',
		value: function onMouseOver() {
			if (!this.state.hovered) {
				this.setState({ hovered: true });
			}
		}
	}, {
		key: 'render',
		value: function render() {
			if (!this.state.hovered) {
				var child = _react2.default.Children.only(this.props.children);
				var childProps = child.props;
				var triggerProps = _extends({
					onMouseOver: this.onMouseOver,
					onFocus: this.onMouseOver
				}, childProps);
				return (0, _react.cloneElement)(child, triggerProps);
			}
			var tooltip = _react2.default.createElement(
				_reactBootstrap.Tooltip,
				{
					className: getTooltipClass(),
					id: this.state.id
				},
				this.props.label
			);
			// TODO jmfrancois : render the Tooltip in a provider so use context for that.
			return _react2.default.createElement(
				_reactBootstrap.OverlayTrigger,
				{
					placement: this.props.tooltipPlacement,
					overlay: tooltip,
					delayShow: 400
				},
				this.props.children
			);
		}
	}]);

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

TooltipTrigger.propTypes = {
	children: _react.PropTypes.element,
	label: _react.PropTypes.string,
	tooltipPlacement: _reactBootstrap.OverlayTrigger.propTypes.placement
};

exports.default = TooltipTrigger;