'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 _class, _desc, _value, _class2, _descriptor, _descriptor2;

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _mobx = require('mobx');

var _mobxReact = require('mobx-react');

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _popper = require('popper.js');

var _popper2 = _interopRequireDefault(_popper);

var _portal = require('../lib/portal');

var _portal2 = _interopRequireDefault(_portal);

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

function _initDefineProp(target, property, descriptor, context) {
  if (!descriptor) return;
  Object.defineProperty(target, property, {
    enumerable: descriptor.enumerable,
    configurable: descriptor.configurable,
    writable: descriptor.writable,
    value: descriptor.initializer ? descriptor.initializer.call(context) : void 0
  });
}

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 _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
  var desc = {};
  Object['ke' + 'ys'](descriptor).forEach(function (key) {
    desc[key] = descriptor[key];
  });
  desc.enumerable = !!desc.enumerable;
  desc.configurable = !!desc.configurable;

  if ('value' in desc || desc.initializer) {
    desc.writable = true;
  }

  desc = decorators.slice().reverse().reduce(function (desc, decorator) {
    return decorator(target, property, desc) || desc;
  }, desc);

  if (context && desc.initializer !== void 0) {
    desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
    desc.initializer = undefined;
  }

  if (desc.initializer === void 0) {
    Object['define' + 'Property'](target, property, desc);
    desc = null;
  }

  return desc;
}

function _initializerWarningHelper(descriptor, context) {
  throw new Error('Decorating class property failed. Please ensure that transform-class-properties is enabled.');
}

var initialArrowProps = {
  left: 0,
  top: 0
};

var initialPopperProps = {
  left: 0,
  position: 'absolute',
  top: 0
};

var PortalPopper = (0, _mobxReact.observer)(_class = (_class2 = function (_Component) {
  _inherits(PortalPopper, _Component);

  function PortalPopper() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, PortalPopper);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = PortalPopper.__proto__ || Object.getPrototypeOf(PortalPopper)).call.apply(_ref, [this].concat(args))), _this), _initDefineProp(_this, 'arrowProps', _descriptor, _this), _initDefineProp(_this, 'popperProps', _descriptor2, _this), _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(PortalPopper, [{
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        _portal2.default,
        {
          ref: 'popper',
          className: 'tooltip tooltip-' + this.props.placement,
          style: this._getPopperStyle()
        },
        _react2.default.createElement(
          'span',
          null,
          this.props.title
        ),
        _react2.default.createElement('div', { ref: 'arrow', className: 'tooltip-arrow', style: this._getArrowStyle() })
      );
    }
  }, {
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _this2 = this;

      this.popper = new this.props.Popper(this.props.getTargetNode(), this.refs.popper.domNode, {
        content: this.props.title,
        placement: this.props.placement,
        modifiersIgnored: ['applyStyle'],
        arrowElement: this.refs.arrow
      });

      this.popper.onUpdate((0, _mobx.action)('popper:updated', function (data) {
        if (data.offsets.arrow) _this2.arrowProps = data.offsets.arrow;
        if (data.offsets.popper) _this2.popperProps = data.offsets.popper;
      }));

      this.popper.update();
    }
  }, {
    key: '_getPopperStyle',
    value: function _getPopperStyle() {
      var left = Math.round(this.popperProps.left);
      var top = Math.round(this.popperProps.top);
      var transform = 'translate3d(' + left + 'px, ' + top + 'px, 0)';

      return {
        position: this.popperProps.position,
        transform: transform,
        WebkitTransform: transform
      };
    }
  }, {
    key: '_getArrowStyle',
    value: function _getArrowStyle() {
      var left = _lodash2.default.isNumber(this.arrowProps.left) ? Math.round(this.arrowProps.left) : null;
      var top = _lodash2.default.isNumber(this.arrowProps.top) ? Math.round(this.arrowProps.top) : null;

      return {
        left: left,
        top: top
      };
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      this.popper && this.popper.destroy();
    }
  }]);

  return PortalPopper;
}(_react.Component), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, 'arrowProps', [_mobx.observable], {
  enumerable: true,
  initializer: function initializer() {
    return (0, _mobx.asReference)(initialArrowProps);
  }
}), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, 'popperProps', [_mobx.observable], {
  enumerable: true,
  initializer: function initializer() {
    return (0, _mobx.asReference)(initialPopperProps);
  }
})), _class2)) || _class;

PortalPopper.propTypes = {
  placement: _react.PropTypes.string.isRequired,
  getTargetNode: _react.PropTypes.func.isRequired,
  title: _react.PropTypes.string.isRequired
};

PortalPopper.defaultProps = {
  Popper: _popper2.default
};

exports.default = PortalPopper;
module.exports = exports['default'];