'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

const Plotly = require('plotly.js/dist/plotly'); /* eslint class-methods-use-this: 0 */

const MIMETYPE = 'application/vnd.plotly.v1+json';

class PlotlyTransform extends _react2.default.Component {

  constructor() {
    super();
    this.getFigure = this.getFigure.bind(this);
  }

  componentDidMount() {
    // Handle case of either string to be `JSON.parse`d or pure object
    const figure = this.getFigure();
    Plotly.newPlot(this.el, figure.data, figure.layout);
  }

  shouldComponentUpdate() {
    return false;
  }

  getFigure() {
    const figure = this.props.data;
    if (typeof figure === 'string') {
      return JSON.parse(figure);
    }
    // assume immutable.js
    return figure.toJS();
  }

  render() {
    const { layout } = this.getFigure();
    const style = {};
    if (layout && layout.height && !layout.autosize) {
      style.height = layout.height;
    }
    return _react2.default.createElement('div', { style: style, ref: el => {
        this.el = el;
      } });
  }
}

exports.PlotlyTransform = PlotlyTransform;
PlotlyTransform.propTypes = {
  data: _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.object])
};

PlotlyTransform.MIMETYPE = MIMETYPE;

exports.default = PlotlyTransform;