'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactAddonsPureRenderMixin = require('react-addons-pure-render-mixin');

var _reactAddonsPureRenderMixin2 = _interopRequireDefault(_reactAddonsPureRenderMixin);

var _mathjaxElectron = require('mathjax-electron');

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

// Initialize the mathjax renderer.
// TODO: When MathJax is loaded, all components should likely re-render
// WARNING: Tech debt here. MathJax should likely be included on the page ahead
//          of time.
(0, _mathjaxElectron.loadMathJax)(document);

function isMathJaxOkYet() {
  return !window.disableMathJax && typeof MathJax !== 'undefined' && window.MathJax && window.MathJax.Hub.Queue;
}

class LatexRenderer extends _react2.default.Component {

  constructor(props) {
    super(props);
    this.shouldComponentUpdate = _reactAddonsPureRenderMixin2.default.shouldComponentUpdate.bind(this);
  }

  componentDidMount() {
    if (isMathJaxOkYet()) (0, _mathjaxElectron.typesetMath)(this.refs.rendered);
  }

  componentDidUpdate() {
    if (isMathJaxOkYet()) (0, _mathjaxElectron.typesetMath)(this.refs.rendered);
  }

  render() {
    return _react2.default.createElement(
      'div',
      { ref: 'rendered' },
      this.props.children
    );
  }
}
exports.default = LatexRenderer;
LatexRenderer.propTypes = {
  children: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.arrayOf(_react2.default.PropTypes.node), _react2.default.PropTypes.node])
};